Loading Resources Once in Java

2019-07-23 23:48发布

问题:

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.