简体   繁体   English

如何检测bundle是否在osgi容器中完全加载?

[英]how to detect if a bundle is completely loaded in osgi container?

is there any kind of event listeners like the ones in BundleEvent Class for detecting if a bundle is completely loaded and available for requests? 是否有任何类型的事件侦听器,如BundleEvent类中的事件侦听器,用于检测bundle是否已完全加载并可用于请求?

I made a search and all i could find is this 我做了一个搜索,我能找到的就是这个

which doesnt use event listeners and means that i need to check manually or periodically (i didnt test that piece of code by the way. 不使用事件监听器,意味着我需要手动或定期检查(我没有测试那段代码。

are there any events like BundleEvent.STARTING for a load operation? 有没有像BundleEvent.STARTING这样的事件来进行加载操作? or we need to implement one by ourselves (if possible) ? 或者我们需要自己实施(如果可能的话)?

There is no way for the OSGi framework to know when your bundle is "ready" for business. OSGi框架无法知道您的捆绑包何时“准备好”用于业务。 The framework can of course make sure your bundle's code dependencies are resolved for allowing classes to be loaded. 框架当然可以确保解析bundle的代码依赖性以允许加载类。 But any other dependencies are unknowable by the framework. 但是框架无法知道任何其他依赖项。 Only your bundle can know when it is ready for business. 只有您的捆绑包可以知道何时可以开展业务。 You can have it advertise this perhaps by having register a service which denotes this. 您可以通过注册表示此功能的服务来宣传这一点。

As BJ mentioned, one option is to use OSGi services. 正如BJ所提到的,一种选择是使用OSGi服务。 For example, bundle B will wait for a service to appear. 例如,捆绑包B将等待服务出现。 When bundle A is ready (eg., activation finished) it registers a service. 当捆绑包A准备好(例如,激活完成)时,它注册服务。 This will notify bundle B. You can use a ServiceTracker for that. 这将通知捆绑包B.您可以使用ServiceTracker

Another option is to use a BundleTracker . 另一种选择是使用BundleTracker In the activator of bundle B you can register a BundleTracker that gets notified for STARTED state of bundles. 在包B的激活,你可以注册一个BundleTracker是获取通知STARTED捆绑的状态。 However, you also want to monitor other states so that you'll discover when bundles go away. 但是,您还希望监视其他状态,以便在捆绑包消失时发现。

In OSGi, there are two kinds of dependencies. 在OSGi中,有两种依赖关系。 The first kind is basically to setup an environment in which your bundle can run in a safe way. 第一种基本上是设置一个环境,在这种环境中你的bundle可以安全地运行。 This includes code dependencies and other dependencies that you can express in your manifest. 这包括您可以在清单中表达的代码依赖性和其他依赖项。 The framework ensures that these dependencies are met before it resolves you bundles. 该框架确保在解析捆绑包之前满足这些依赖关系。 If those dependencies are not met, you won't be able to run a single instruction. 如果不满足这些依赖关系,您将无法运行单个指令。

The second kind of dependencies are more dynamic, your code should be able to handle them in runtime when they change. 第二种依赖关系更具动态性,您的代码应该能够在运行时更改时处理它们。 In OSGi, these dependencies are best expressed services. 在OSGi中,这些依赖关系是表达最好的服务。 With Declarative Services (specifically with the annotations) it is trivial to depend on others (Please, please don't use Service Tracker for this, DS is far, far, far superior). 使用声明性服务(特别是注释),依赖其他人是微不足道的(请不要使用服务跟踪器,DS远远超出优势)。

So as the other responders said, ready is in the eye of the beholder. 因此,正如其他响应者所说,准备就在旁观者的眼中。 In OSGi, when you express your dependencies on services the problem shifts away from the bundle is ready, to: is there a service X? 在OSGi中,当您表达对服务的依赖关系时,问题从捆绑包中移开已经准备就绪,以便:是否有服务X? As long as the registrar of service X follows these rules as well you have a very robust and resilient application model. 只要服务X的注册商遵循这些规则,您就拥有一个非常强大且有弹性的应用程序模型。 Since the framework and DS strictly follow the life cycle rules many different kinds of reasons why a bundle is ready or not collapse in a single model: the service. 由于框架和DS严格遵循生命周期规则,因此在单个模型中捆绑包准备好或不崩溃的原因有很多种:服务。

Short example, Service Y depends on service X: 简短的例子,服务Y取决于服务X:

@Component 
public class YImpl implements Y {

   @Activate
   void activate() { /* only called when X is registered */ }
   @Reference
   void setX( X x ) {
      this.x = x;
   }

} }

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

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