3

是否有任何类型的事件侦听器,例如 BundleEvent 类中的事件侦听器,用于检测捆绑包是否已完全加载并可用于请求?

我进行了搜索,我能找到的只有这个

它不使用事件侦听器,这意味着我需要手动或定期检查(顺便说一下,我没有测试那段代码。

是否有任何事件,如 BundleEvent.STARTING 用于加载操作?或者我们需要自己实现一个(如果可能的话)?

4

3 回答 3

3

In OSGi, there are two kinds of dependencies. The first kind is basically to setup an environment in which your bundle can run in a safe way. 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. 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).

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? As long as the registrar of service X follows these rules as well you have a very robust and resilient application model. 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.

Short example, Service Y depends on service X:

@Component 
public class YImpl implements Y {

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

}

于 2012-09-03T08:17:23.013 回答
3

正如 BJ 提到的,一种选择是使用 OSGi 服务。例如,捆绑 B 将等待服务出现。当包 A 准备好(例如,激活完成)时,它会注册一个服务。这将通知捆绑包 B。您可以ServiceTracker为此使用 a。

另一种选择是使用BundleTracker. 在捆绑 B 的激活器中,您可以注册一个BundleTracker收到STARTED捆绑状态通知的通知。但是,您还希望监视其他状态,以便您发现捆绑包何时消失。

于 2012-08-31T05:45:30.773 回答
3

OSGi 框架无法知道您的捆绑软件何时“准备好”用于业务。该框架当然可以确保您的包的代码依赖关系被解析以允许加载类。但是框架不知道任何其他依赖项。只有您的捆绑包才能知道它何时可以投入使用。您可以通过注册一个表示这一点的服务来让它宣传这一点。

于 2012-08-31T03:26:52.870 回答