在 Java 中加载一次资源
我有一个 Java 项目,需要每次执行时加载大文件(NLP 语料库)。我们将其称为方法 load(),这只调用一次,而方法 process(String text) 可以调用多次,这些方法属于 Tagger 类。
我的问题是,每当我需要调整除类标记器之外的一些代码并重新编译/运行项目(项目调用 process() 方法)时,我都必须等待大约 10 秒或更长时间才能加载这些资源。我是否可以维护这些资源或 Tagger 类本身,以便我可以编辑任何代码并重新编译它们,而无需卸载 Tagger 类或卸载资源?
我的选择是使用 Web 服务或一些 servlet,但是还有更多替代解决方案吗?或者图书馆更容易创建这些服务?由于该方案只在开发阶段需要,部署后不需要,用户在程序执行过程中真正加载一次资源,关闭时终止。
谢谢,
I have a project in Java requiring to load large files (corpus for NLP) for every execution. Let's call it method load(), and this is only called once, and the method process(String text) can be called multiple times where these belong to the class Tagger.
My problem is that whenever I need to tweak some code except for class Tagger and recompile/run the project, where the project calls the method process(), I have to wait for some 10 or more seconds just to have these resources loaded. Can I maintain these resources or the Tagger class itself, such that I can edit any code and recompile them, without unloading the Tagger class or unloading the resources?
My option is to use web services or some servlets, but are there more alternative solutions? Or libraries to create these services easier? Since the solution is only required during development stage, but not required after deployed, when users really load the resources once during the program execution and terminates when closed.
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可能会考虑热交换,它允许您重新编译一个或多个类并将它们合并到已经运行的应用程序中。
http://java.sun.com/j2se/1.4.2/docs/guide/jpda/enhancements.html#hotswap
You might consider hot swapping, which allows you to recompile one or more classes and merge them into an already-running application.
http://java.sun.com/j2se/1.4.2/docs/guide/jpda/enhancements.html#hotswap
这是使用动态类加载器重新加载类的指南,这可能是您最好的选择:
http://www.zeroturnaround.com/blog/reloading-objects-classes -类加载器/
Here's a guide to reloading classes with a dynamic class loader, which probably is your best bet:
http://www.zeroturnaround.com/blog/reloading-objects-classes-classloaders/
如果处理数据速度变慢,可以尝试序列化标记器类并将其保存到磁盘。程序启动时加载类。
If the slow down is do to processing the data maybe try serializing the tagger class and saving it to disk. Loading the class when the program starts.
好吧,如果您使用像 Groovy 这样的动态语言,您可以发出要在运行时编译的语句,而这些资源已经加载。
另外,如果您正在编辑 JSP,我知道 servlet 容器通常会侦听文件的更改,如果发现任何更改,则会重新编译页面。
Well, if you use a dynamic language like Groovy, you could issue statements to be compiled at runtime, while these resources are already loaded.
Also if you're editing a JSP I know the servlet container typically listens for changes to the file and will re-compile the page if it finds any.
我认为 JRebel 可以轻松地为我完成这项工作。但上述方法可以替代这种商业工具。谢谢。
I think JRebel can do the work for me easily. But the approaches above can be a substitute for this commercial tool. Thanks.