简体   繁体   English

如何定义OSGi包的起始级别?

[英]How is the OSGi bundle start level defined?

How is the OSGi bundle start level defined? 如何定义OSGi包的起始级别?

I am using Apache felix and would like to persist the start level across framework executions. 我正在使用Apache felix,并希望在框架执行中保持启动级别。 I do not anticipate a need to change the start level of a bundle very frequently at all an an entry in Manifest.MF seems the most sensible. 我预计不需要在Manifest.MF的所有条目中非常频繁地更改捆绑的起始级别.MF似乎是最明智的。 I've to org.osgi.framework.startlevel but have not seen a practical example. 我要去org.osgi.framework.startlevel,但还没有看到一个实际的例子。

I am also using maven with the maven-bundle-plugin, if there is an elegant way to incorporate the start level in the POM that would be brilliant. 我也在使用maven和maven-bundle-plugin,如果有一种优雅的方式将POM中的起始级别合并到一起就很棒。

Bundles don't define their own start level at build-time; Bundle在构建时没有定义自己的起始级别; the administrator or agent that installs the bundle into the framework defines it. bundle 安装到框架中的管理员或代理定义它。

The core framework defines a Start Level interface in section 8. Quoting: 核心框架在第8节中定义了一个Start Level接口。引用:

The Start Level API provides the following functions: Start Level API提供以下功能:

  • Controls the beginning start level of the OSGi Framework. 控制OSGi框架的起始启动级别。

  • Is used to modify the active start level of the Framework. 用于修改Framework的活动启动级别。

  • Can be used to assign a specific start level to a bundle. 可用于为捆绑包指定特定的起始级别。

  • Can set the initial start level for newly installed bundles. 可以为新安装的软件包设置初始启动级别。

The last two are relevant to your inquiry here. 最后两个与您的询问相关。 Section 8.3.4— Changing a Bundle's Start Level —indicates that the framework will store an assigned start level persistently. 第8.3.4节 - 更改Bundle的Start Level - 表示框架将持久存储指定的启动级别。

If you're using Apache Felix, there are several ways you can install bundles and assign their start level, whether explicitly or by allowing them to inherit a default start level for installed bundles: 如果您使用的是Apache Felix,有几种方法可以安装捆绑包并分配其启动级别,无论是显式还是允许它们继承已安装捆绑包的默认启动级别:

Also, see the felix.startlevel.bundle property , which controls bundles installed through means other than those above. 另请参阅felix.startlevel.bundle属性 ,该属性控制通过上述方式以外的方式安装的软件包。

As for setting a manifest property (such as with Maven at build time), there used to be a way to do this in Equinox—now deprecated—but there is no standard means for a bundle to indicate to the framework what its proper start level should be. 至于设置清单属性(例如在构建时使用Maven),过去常常有一种在Equinox中执行此操作的方法 - 现在已弃用 - 但是没有标准方法可以让bundle向框架指示其正确的启动级别应该。

X, X,

I think there is an easier way to do what you're talking about. 我认为有一种更简单的方法来做你正在谈论的事情。 Currently, you are using the Felix OSGi implementation directly, which is very powerful. 目前,您正在直接使用Felix OSGi实现,这非常强大。 However, if you'd like the kind of granular control over bundle deployment, that's built into an OSGi container called Karaf. 但是,如果您希望对bundle部署进行精细控制,那么它将内置于名为Karaf的OSGi容器中。 Think of Karaf as a car whose engine can be either Felix or Equinox. 将Karaf想象成一款可以是Felix或Equinox的发动机。 It something that rides on top of OSGi framework implementations and offers additional functionality. 它可以在OSGi框架实现之上运行,并提供其他功能。 For example, Karaf provides a Provisioning mechanism. 例如,Karaf提供了供应机制。 Deploying multiple bundles is called Provisioning. 部署多个捆绑包称为“配置”。 Because Provisioning isn't part of the OSGi spec, different OSGi containers implement provisioning in different ways. 由于Provisioning不是OSGi规范的一部分,因此不同的OSGi容器以不同的方式实现配置。 In Karaf, we do this through something called a features.xml file. 在Karaf中,我们通过名为features.xml的文件来完成此操作。

In a features.xml file, you identify a specific set of bundles you want deployed together. 在features.xml文件中,您可以标识要一起部署的一组特定包。 Then you name that group. 然后你命名该组。 In this file you can also identify the specific start order you'd like Karaf to deploy the bundes. 在此文件中,您还可以识别您希望Karaf部署Bundes的特定开始顺序。

A word on OSGi start-orders. 关于OSGi启动订单的一个词。 A bundle cannot be started until all mandatory wiring has occured. 在发生所有强制接线之前,无法启动捆绑。 This means that you can define a start order, but OSGi takes this as guidances, not mandatory. 这意味着您可以定义一个起始顺序,但OSGi将其视为指导,而非强制性。 For example, if you have a bundle A that requires an import of bundle b's "foo" package, you can tell the container to start A before B all you want. 例如,如果您有一个捆绑A需要导入捆绑包b的“foo”包,您可以告诉容器在B之前启动A所需的全部内容。 But it won't respect that order because in reality B needs to be started in order to A to start. 但是它不会尊重这个顺序,因为实际上B需要启动以便A开始。 Its ok though, the container knows (usually) what order to start bundles in. 没问题,容器知道(通常)启动捆绑的顺序。

The rub comes in the use of optional versus mandatory imports in a bundle. 在捆绑中使用可选与强制导入是相互影响的。 If your bundle imports b.foo, but that import is optional, the container will respect the bundle start order (A then B). 如果您的包导入b.foo,但该导入是可选的,则容器将遵循包启动顺序(A然后B)。 But watch out, if A actually needs to import b.foo, but you've marked it as optional, A will start without Wiring to b.foo, and A will throw a ClassNotFoundException. 但请注意,如果A实际上需要导入b.foo,但你已将其标记为可选,A将在没有连接到b.foo的情况下启动,并且A将抛出ClassNotFoundException。 This nasty little bug can occur when using the various packages in Spring. 在Spring中使用各种包时,可能会出现这个令人讨厌的小错误。

To make your life easy, Spring does the majority of its imports as "optional". 为了让您的生活更轻松,Spring将其大部分进口作为“可选”。 If a given spring bundle actually needs to resolve a dependancy before working and that import is marked as optional, your bundles will deploy sparodically. 如果给定的spring bundle实际上需要在工作之前解析依赖性并且导入被标记为可选,那么您的bundle将以sparodically部署。 Of course,the fix for this is to create a bundle fragment that modifies the bundle imports to "mandatory", but that's really beyond the scope of your question. 当然,对此的修复是创建一个捆绑片段,将捆绑导入修改为“强制”,但这实际上超出了您的问题的范围。

I hope this clarifies things for you. 我希望这能为你澄清一些事情。

bundle.adapt(BundleStartLevel.class).setStartLevel(startlevel);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM