4

我偶然发现了一个问题,可以总结如下:

我有一个嵌入 OSGI 的应用程序。在这个 OSGI 容器中,我安装了一个 v1 版本的包 A,它注册了该服务对象。此服务对象由主机应用程序使用。在使用服务对象时,我卸载了捆绑包 A 并安装了捆绑包 A 的新版本 (v2)。这是我的发现。

  • 即使我们卸载捆绑包 A(v1),旧服务对象也能正常工作。
  • 新的服务对象提供了 bundle A(v2) 的新功能。

我的问题是当我们使用刷新捆绑包时

List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle local; // points to the bundle
refreshbundles.add(local);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)

当我们尝试获取新版本的bundle A注册的服务对象时,它返回null。我尝试查看源代码,但没有帮助。你能帮我解决这个问题吗?

编辑:

主机应用程序

public Felix m_felix = null;
m_felix = new Felix(config); //sending some config to felix.
m_felix.start();

//installing bundle
Bundle bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.0.jar")    
bundle.start()

//getting the service object registered by bundle A.
ServiceReference sr = m_felix.getBundleContext()
                        .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//now uninstalling the bundle installing the new version of it say version 1.1
bundle.uninstall()
bundle = m_felix.getBundleContext().installBundle("file:/bundleA-1.1.jar")    
bundle.start()

//getting the service object registered by bundleA1.1

ServiceReference sr = m_felix.getBundleContext()
                    .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//the above line is working fine but after refreshing the packages, Service object is returned as null    

List<Bundle> refreshbundles = new ArrayList<Bundle>();
Bundle bundle; // points to the bundle
refreshbundles.add(bundle);
m_felix.adapt(FrameworkWiring.class).refreshBundles(refreshbundles)

//refresh done
ServiceReference sr = m_felix.getBundleContext()
                    .getServiceReference(SampleInterface.class.getName()))
(SampleInterface) m_felix.getBundleContext(sr).getService().sayHI();

//throwing null pointer exception because getService() is returning null.

当我们刷新捆绑包时到底发生了什么?

BundleA_Activator.java

public class BundleA_Activator extends BundleActivator{
   public class BundleActivatorInterfaces implements  BundleActivator{
    ServiceRegistration SR;
    @Override
    public void start(BundleContext bundleContext) {

        SR = bundleContext.registerService(SampleInterface.class.getName(), new ExposedClass(), null);
    }

    @Override
    public void stop(BundleContext bundleContext) {
            SR.unregister();
    }
}
4

0 回答 0