【问题标题】:OSGi caches leaves newly installed bundles in INSTALLED stateOSGi 缓存使新安装的包处于 INSTALLED 状态
【发布时间】:2013-09-23 10:21:06
【问题描述】:

我有一个加载 JVM 并启动 OSGi 框架的 C++ 程序。 OSGi 框架是 Equinox,更准确地说是 org.eclipse.osgi_3.8.1.v20120830-144521.jar

启动器

OSGi 启动器是使用 JNI 从 C++ 调用的,它是这样的(为简洁起见,省略了详细信息):

// Create OSGi framework.
final ServiceLoader<FrameworkFactory> frameworkFactoryLoader =
        ServiceLoader.load(FrameworkFactory.class);
final FrameworkFactory frameworkFactory =
        getFrameworkFactory(frameworkFactoryLoader);
final Map<String, String> osgiConfig = ...
final Framework osgiFramework = frameworkFactory.newFramework(osgiConfig);

// Start the framework.
osgiFramework.start();

// Install some bundles.
final BundleContext frameworkBundleContext = osgiFramework.getBundleContext();
final Bundle bundle1 =
    installBundle(frameworkBundleContext, "reference:" + bundle1URI, null);
installBundle(frameworkBundleContext, "reference:" + bundle2URI, 5);
installBundle(frameworkBundleContext, "reference:" + bundle3URI, 10);
...

// Explicitly starting a particular bundle.
bundle1.start();
...

// Raise the framework start level so bundles are started
// at the desired start levels.
final FrameworkStartLevel frameworkStartLevelObject =
        bundleAdapt(systemBundle, FrameworkStartLevel.class);
frameworkStartLevelObject.setStartLevel(10, ...left-out...);

安装包的辅助函数如下:

private Bundle installBundle(final BundleContext frameworkBundleContext,
                             final String bundleURI,
                             final Integer desiredStartLevel) {
    final Bundle bundle = frameworkBundleContext.installBundle(bundleURI);
    if (desiredStartLevel != null) {
        // Set the level at which the bundle should start.
        // (Otherwise, it will start at the default level.)
        final BundleStartLevel bundleStartLevel =
            bundleAdapt(bundle, BundleStartLevel.class);
        bundleStartLevel.setStartLevel(desiredStartLevel);
    }
}

现在,所有捆绑包都已解析并且处于RESOLVEDACTIVESTARTED 状态。

osgi.clean=true开头

如果我在 osgiConfig 映射中使用选项 osgi.clean=true 启动 OSGi 框架,并将安装的包从运行更改为运行,这很好地反映在框架中。

如果我例如使用 bundleX 和 bundleY 启动框架并调用

frameworkBundleContext.getBundles();

那我看清楚了

  • 系统捆绑包 (ACTIVE)
  • bundleX (RESOLVED)
  • bundleY (RESOLVED)

如果我关闭程序并这次使用 bundleX 和 bundleZ 重试,那么我会看到(不足为奇)

  • 系统捆绑包 (ACTIVE)
  • bundleX (RESOLVED)
  • bundleZ (RESOLVED)

开始时没有osgi.clean

如果我启动 OSGi 框架 osgiConfig 映射中设置 osgi.clean,则安装的包会在运行之间持续存在,并且不会解析新的包。

所以假设我运行一次 osgi.clean=true 加载 bundleX 和 bundleY 然后关闭程序。

现在,当我在没有 osgi.clean 的情况下重新启动并仅安装 bundleZ 时,我看到了:

  • 系统捆绑包 (ACTIVE)
  • bundleX (RESOLVED)
  • bundleY (RESOLVED)
  • bundleZ (INSTALLED)(即尚未解决)

因此,bundleX 和 bundleY 从一次运行到另一次运行都幸存下来,而无需第二次安装。

另一方面,bundleZ 不会自动解析。要使其达到RESOLVED 状态,我需要这样做:

  final FrameworkWiring frameworkWiring = systemBundle.adapt(FrameworkWiring.class);
  frameworkWiring.resolveBundles(null);

问题:我应该使用osgi.clean吗?

似乎使用osgi.clean=true 每次都给我一个新的开始,而不使用它意味着捆绑状态从运行到运行都存在。我猜缓存使 OSGi 启动得更快,但对我来说似乎没什么大不了的(因为我使用“reference:”前缀安装捆绑包,这意味着 jar 不会复制到缓存中,只是对原始文件的引用文件位置保留)。

但是,我的应用程序是针对同一 OSGi 配置区域运行的多进程应用程序。在这种情况下一直使用osgi.clean=true 是否有问题?

另外,如果有人能指出我对 osgi.clean 的确切含义以及 OSGi (Equinox) 中的缓存如何工作的一个很好的解释,我将不胜感激。

【问题讨论】:

  • 我用关于开始级别的详细信息更新了问题。

标签: osgi


【解决方案1】:

忽略osgi.clean。也请忽略开始级别。您缺少的步骤实际上是启动捆绑包!

每次调用installBundle 都会给你一个Bundle 对象。安装完所有的包之后,你应该在每个返回的包对象上调用start()

您用来启动捆绑软件的任何其他方式都完全靠运气,即因为这些捆绑软件可能曾经在该状态下启动并缓存过。如果您将应用程序安装在另一台机器上,则可能无法重复相同的状态。所以实际控制你的包并自己启动它们。

您通常无需担心 INSTALLED 和 RESOLVED 之间的区别。已安装可以简单地表示“未解决尚未”,例如因为不需要捆绑包的导出。如果有任何解决问题,例如缺少依赖项,那么在调用start 方法时会发现它们为BundleException

【讨论】:

  • 如果我忽略osgi.clean,以前安装的旧包将自动安装。所以我需要osgi.clean 至少在我将配置更改为不加载特定捆绑包时,以使 Equinox 忘记它们。关于“纯运气”声明,我遗漏的是我为每个捆绑包仔细设置了所需的startlevel(以控制启动顺序),并明确启动一个不会自行启动的捆绑包/org.eclipse.equinox.console)。 AFAIK,这正是 Eclipse 启动器和org.eclipse.equinox.simpleonfigurator 所做的。示例中的 bundle3 是一个片段。
  • 我更新了原始问题,详细介绍了开始级别。
  • 但是,设置启动级别实际上并不会导致捆绑包启动。它们仍然需要显式启动——只是如果您启动一个高于当前启动级别的包,那么它实际上还不会被激活。但是……你为什么要搞乱起始级别?您几乎不需要这样做!
  • 另外...请不要将 p2 中的 Eclipse 启动器和 simpleconfigurator 视为 OSGi 中的任何“良好实践”示例。
猜你喜欢
  • 2011-03-09
  • 2015-08-16
  • 1970-01-01
  • 2014-12-01
  • 2015-08-10
  • 1970-01-01
  • 2020-10-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多