Dynamically loadable and unloadable application modules in Java - how?

后端 未结 5 1997
醉酒成梦
醉酒成梦 2021-02-10 17:47

I\'m writing a server application which makes use of external modules. I would like to make them to be upgradeable without requiring server restart. How do I do that? I\'ve foun

相关标签:
5条回答
  • 2021-02-10 18:27

    What you're going for is definitely possible. I believe that you can unload classes from memory by loading them in a separate ClassLoader and then disposing that ClassLoader. If you're not wanting to go all out and use OSGI, I'd recommend something like JBoss Microcontainer (http://www.jboss.org/jbossmc) or ClassWorlds (http://classworlds.codehaus.org/). It's not too terribly difficult to write something like this from scratch if your needs are specialized enough.

    Hope this helps, Nate

    0 讨论(0)
  • 2021-02-10 18:34

    It would at least require you to define your custom classloader... I don't see how can this be simpler than just using Felix, Equinox, Knoplerfish or any open source Osgi runtime to do the task. Maybe SpringDM is simpler...

    0 讨论(0)
  • 2021-02-10 18:36

    It seems like OSGi is exactly what you're asking for. It can be complex, but there are ways to deal with that. Some of the complexity can be mitigated by using SpringDM or something similar to handle the boilerplate tasks of registering and consuming services in the runtime. Annotation-driven service registration and dependency injection really reduces the amount of code that needs to be written.

    Another way to reduce complexity is to deploy the bulk of your application in a single bundle and only deploy the parts that need to be modular into their own bundles. This reduces your exposure to registering and using services from other bundles in the runtime as well as reducing the complexity of deployment. Code running within a bundle can use other code in the same bundle just as in a standard Java app - no need to interact with the OSGi runtime. The opposite of this approach is to break up your application into lots of discrete bundles that export well-defined services to other bundles in the system. While this is a very modular approach, it does come with extra complexity of managing all those bundles and more interaction with the OSGi runtime.

    I would suggest taking a look at the book "OSGi in Action" to get a sense of the issues and to see some decent samples.

    0 讨论(0)
  • 2021-02-10 18:42

    OSGi is not so complicated - using PAX runner with maven worked as a breeze.

    Or implement your own ClassLoader and set it to JVM : java -Djava.system.class.loader=com.test.YourClassLoader App.class

    0 讨论(0)
  • 2021-02-10 18:44

    If you follow the ClassLoader route (is not that difficult, really), I suggest each module to be packaged in its own jar, and use a different ClassLoader to read each jar. that way, unloading a module is the same as "discarding" the ClassLoader.

    0 讨论(0)
提交回复
热议问题