Loading Resources Once in Java

。_饼干妹妹 提交于 2019-12-24 03:24:13

问题


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,


回答1:


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




回答2:


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/




回答3:


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.




回答4:


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.




回答5:


I think JRebel can do the work for me easily. But the approaches above can be a substitute for this commercial tool. Thanks.



来源:https://stackoverflow.com/questions/2148161/loading-resources-once-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!