寻找在动态加载Jar文件中使用Apache Felix并在Java中在运行时实例化类的基本示例 [英] Looking for basic example of using Apache Felix in dynamic loading of Jar file and instancing a class at runtime in Java

查看:122
本文介绍了寻找在动态加载Jar文件中使用Apache Felix并在Java中在运行时实例化类的基本示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试基于一些示例实现自己的类加载器。但是,我认为它没有按预期工作(无法重新加载Jar文件等。我看到很少引用推荐使用OSGI或Apache Felix来处理Jar文件加载。是否有任何加载Jar的例子,从Jar实例化一个类?

I tried implementing my own class loader based on some examples. However, I think it is not working as expected ( unable to reload Jar file etc. I see few references of recommending using OSGI or Apache Felix for handling Jar file loading. Is there any examples of loading Jar, instancing a class from the Jar?

我想要完成的更多细节......我有一个基本上连续运行的Java命令行应用程序。我希望它能够动态地引用JAR文件运行时和运行时实例这些jar中的一个类。这些jar可能包含一个或多个支持类。这些jar本质上是自定义的工作单元,由来自连续运行的主应用程序的某些事件条件执行。是一个多客户端,我想让jar是一个可插入类型的工作单元。

More details on what I am trying to accomplish..I have a Java command line application that essentially continuously runs. I want it to be able to reference JAR files dynamically at runtime and at run-time instance a class in these jars. These jars may contain 1 or more supporting classes. These Jars are essentially customized work units that get executed by certain event conditions from the main application that is continuously running. .. Since this is a multi-client, I wanted to have the jars be a pluginable type work units.

我目前的方向是为'客户端'提供一个代码接口和然后让他们将他们的类打包在一个jar文件中。然后应用程序将执行配置(数据库)jar路径并从Jar运行类。这适用于加载jar和类,但是,我希望能够基本上热部署这些jar。

My current direction has been providing the 'client' an interface to code to and then having them package their classes in a jar file. The application will then execute the configured ( database ) jar path and run class from the Jar. This works with loading of a jar and class however, I want to be able to essentially hot deploy these jars.

总之,我希望有一个包含支持函数的类的JAR文件。一个定义的类,将在运行时从Jar(来自主循环应用程序)引用。在主应用程序运行时更改JAR文件的能力。

In summary I would like to have a JAR file that contains classes to support a function. A defined class that will be referenced at run-time from the Jar ( from the main looping application ). The ability to change out JAR files while the main application is running.

如果我要使用第三方库,我最好使用Apache Felix。

If I am to use a third party library, it is preferred that I use Apache Felix.

谢谢

我想我用Apache Felix 4来解决它。这是加载jar /类的最佳方法吗?或者是否有更好的更有效的方式。到目前为止,我的研究并没有指出一个解决方案。谢谢。

I think I figured it out using Apache Felix 4. Is this the best way to load jars/classes ? Or is there a better more efficient way. My research so far did not point to one solution. thanks.

    FrameworkFactory ff = new FrameworkFactory ();
     Map<String,Object> config = new HashMap<String,Object>();
      config.put("org.osgi.framework.storage", "c:\\temp\\myclientBundles");
       config.put(Constants.FRAMEWORK_SYSTEMPACKAGES_EXTRA,
   "packages needed,more packages needed");
  config.put(Constants.FRAMEWORK_STORAGE_CLEAN, "true");

  Framework fwk = ff.newFramework(config);
  fwk.start();


  BundleContext bc = fwk.getBundleContext();

  Bundle bundle =   bc.installBundle("file:C:\\my_client.jar");


      bundle.start();
      bundle.update();
      Class clazz = bundle.loadClass("client.work.process");
      Job pc = (Job) clazz.newInstance();
      pc.startWork(someConfigObject);
      bundle.stop();
      fwk.stop();


推荐答案

如果现在,简单说,我拒绝相信你的问题是加载一个类... :-)我认为你有另一个问题,你认为可以解决加载一个类?

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?

在大多数情况下人们天真地开始加载类,问题是可扩展性。他们有一个系统,并希望有选择地扩展它的新功能,我假设你有一个类似的问题,因为你想更新提供者jar?

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?

如果所以,下载bndtools并查看OSGi服务,他们通常很适合这个账单

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

好的,更新后。如果我理解你,那么Apache Felix和Apache Felix文件安装将会很好。 File Install监视目录并在框架中的该目录中安装任何bundle。从目录中删除该jar,卸载该捆绑包。 (我在12年前编写了文件安装的原型!)

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!)

对于你的主JAR,让它看起来像:

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
}

对于每个客户端JAR:

For each client JAR:

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

这几乎是你使用bndtools时必须写的全部内容。只需创建一个组件项目,为主代码添加一个Bundle Descriptor,为客户端代码示例添加任意数量的描述符,然后运行为 - > OSGi Run。然后,您可以下载Apache Felix文件安装,将其添加到运行选项卡,并创建一些其他项目,将jar(在生成的文件夹中连续生成)放入文件安装文件夹...瞧。没有比这简单得多: - )

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 :-)

这篇关于寻找在动态加载Jar文件中使用Apache Felix并在Java中在运行时实例化类的基本示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆