如果更新了另一个提供服务的包 Java,为什么依赖包不是 notified/refreshed?

Why the dependent bundles are not notified/refreshed if another bundle providing service is updated Java?

我有 2 个 OSGI 包 TestCommons,它是服务提供者包和使用该服务的 TestMyBundle。 现在我在 testMyBundle 中使用了声明式服务,并且基于此我在 TestMyBundle 的 Activator class 中有 setter 和 unsetter。因此,只要找到 TestCommons 服务,就会调用 setter 方法,如果从 OSGI 中注销了 TestCommons 服务,则会调用 unsetter 方法。

现在我想以编程方式更新 TestCommons 包,我使用了 org.osgi.framework.Bundle 接口的 update() 方法来更新现有包。 现在,如果我更新包,则不会调用 Bundle TestMyBundle 的 setter 和 unsetter 方法,并且不会通知包。如何以编程方式通知依赖包更新? 一种方法是刷新,但我无法手动刷新捆绑包。

这是我写的代码

Bundle[] bundle = context.getBundles();
    String symbolicName = "TestCommons";
    try {
        FrameworkWiring frameworkWiring = null;
        for (Bundle b : bundle) {
            if (b.getSymbolicName().equalsIgnoreCase(symbolicName)) {
                b.update(new FileInputStream(new File("/home/temp/TestCommons-0.0.1-SNAPSHOT.jar")));
                frameworkWiring = context.getBundle().adapt(FrameworkWiring.class);
                break;
            }

        }
        frameworkWiring.refreshBundles(null);
    } catch (Exception e) {
        System.out.println("Exception occured while starting...");
        e.printStackTrace();
    }

}

现在,adapt() 方法返回 null。所以无法调用刷新。请告诉我这里的问题是什么以及可以采取哪些其他方法来实现捆绑包更新。

如有任何线索,我们将不胜感激。谢谢...

您的TestMyBundle没有随着服务的接口包更新而更新。所以您已经在正确的轨道上,您需要刷新 TestMyBundle 以获取更改。

在实践中,您通常可以通过为 api 使用单独的包来避免这种情况。只要您不更新服务接口(这种情况应该很少见),您就可以简单地更新服务包,客户端中的声明性服务组件将获取新服务。

现在关于刷新捆绑包。你已经是正确的,你需要 FrameworkWiring,但只有系统包可以适应 FrameworkWiring。所以这应该可以解决问题:

Bundle systemBundle = context.getBundle(0);
systemBundle.adapt(FrameworkWiring.class).refresh(null);

这将刷新所有包。