Looking for basic example of using Apache Felix in dynamic loading of Jar file and instancing a class at runtime in Java

丶灬走出姿态 提交于 2019-12-03 15:34:21

If Now, simple said, I refuse to believe your problem is loading a class ... :-) I think you have quite another problem that you think can be solved to load a class?

In the majority of cases where people naively start loading classes the problem is extensibility. They have a system and want to selectively extend it with new functionality, I am making the assumption that you have a similar problem since you want to update the provider jar?

If so, download bndtools and look at OSGi services, they usually fit the bill very well

Ok, after your update. If I understand you well, you would be very well served with Apache Felix and Apache Felix File Install. File Install watches a directory and installs any bundle in that directory in the framework. Removing that jar from the directory, uninstalls the bundle. (I wrote the archetype of File Install over 12 years ago!)

For you main JAR, make it look like:

@Component(immediate=true)
public void Main {  
    @Reference
    void setClient( Client client) { ... } // called whenever a client gets started
}

For each client JAR:

@Component
public void ClientImpl implements Client {
  ... whatever
}

This is virtually all you have to write when you use bndtools. Just create a component project, add a Bundle Descriptor for the main code and any number of descriptors for the client code examples, then Run As -> OSGi Run. You can then download Apache Felix File Install, add this to the run tab, and create some other projecs, drop the jars (continuously made in the generated folder) into the file install folder ... voila. Does not get much simpler than this :-)

If I understand you right, you are asking for an example how to dynamically load a jar in OSGi, is that right?

In OSGi the modules that are dynamically loaded are called bundles. So basically the most usual approach to solve your problem will be to turn your jars in OSGi bundles. An OSGi bundle is a normal Java jar with some enhancements - it has a manifest file (a txt file) where dependencies on other java packages are declared; and an Activator class which is called when the bundle is started and stopped. This Activator is the central piece of the bundle, it knows how to instantiate the other parts of the bundle.

Now, if your dynamic jar is simply a library jar and you only want to provide its java packages in the system, you can even go without an activator, by only adding a simple manifest file. There are even tools that can do the conversion automatically for you - check BND.

Then, when you have your bundle ready, you can install it dynamically on any OSGi framework, by using the install command. Reloading is done by calling the update command etc. The lifecycle is very well defined and I would definitely recommend using this instead of reinventing the wheel ;)

For some examples and demos see here.

Hope this helps.

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