Dynamically loaded and unloaded application modules in Java - how?

I am writing a server application that uses external modules. I would like to make them available for updating without rebooting the server. How can I do it? I found OSGi , but it looks very complex and great for my task.

Simple * .jar files are fine, but once they are downloaded, I suppose I cannot unload them from the VM and replace them with another version on the fly.

What approach can you offer?

+4
source share
5 answers

OSGi seems to be exactly what you are asking for. It can be tricky, but there are ways to handle it. Some complexity can be mitigated using SpringDM or something similar to handle the template tasks of registering and using services at runtime. Annotated service registration and dependency injection really reduce the amount of code that needs to be written.

Another way to reduce complexity is to deploy the main part of your application in one bundle and only deploy the parts, which must be modular, into your own packages. This reduces the likelihood of registering and using services from other packages at run time, and also reduces deployment complexity. The code running inside the package may use different code in the same package as in the standard Java application - there is no need to interact with the OSGi runtime. The opposite of this approach is to split your application into many discrete packages that export well-defined services to other packages in the system. Although this is a very modular approach, it comes with the added complexity of managing all of these packages and great interaction with the OSGi runtime.

I would suggest taking a look at the OSGi in Action book to sort out the problems and see some decent examples.

+4
source

At the very least, you need to define your custom classloader ... I don’t understand how easier it is than just using Felix, Equinox, Knoplerfish or any open source Osgi free time to complete the task. Maybe SpringDM is easier ...

+2
source

What you are planning is definitely possible. I believe that you can unload classes from memory by loading them into a separate ClassLoader and then deleting this ClassLoader. If you don't want to use OSGI at all costs, I would recommend something like JBoss Microcontainer (http://www.jboss.org/jbossmc) or ClassWorlds (http://classworlds.codehaus.org/). It's not too hard to write something like that from scratch if your needs are specialized enough.

Hope this helps, Nat

+1
source

If you are following the ClassLoader route (indeed, it is not so difficult), I suggest that each module be packaged in its own jar and use a different ClassLoader to read each jar. Thus, unloading the module is the same as dropping the ClassLoader class.

+1
source

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

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

0
source

Source: https://habr.com/ru/post/1345884/


All Articles