简体   繁体   English

OSGi将捆绑包侦听器添加到已安装的捆绑包

[英]OSGi add bundle listener to installed bundle

I have an installed bundle. 我已经安装了捆绑包。 Now I want to add listener to it (and later to start) to find when it comes to active state. 现在,我想向其添加侦听器(稍后再启动)以查找其进入活动状态的时间。 The only way I found is 我发现的唯一方法是

bundle.getBundleContext().addBundleListener(new ...);

However getBundleContext() works only if bundle is in starting, stopping, active state. 但是,仅当bundle处于启动,停止,活动状态时,getBundleContext()才起作用。 So, can can I do that? 那么,我可以这样做吗?

It does not make sense to register a BundleListener to catch events that happen with the same bundle. 注册BundleListener来捕获同一包中发生的事件是没有意义的。 When you implement a BundleListener / BundleTracker, you normally want to catch the events of bundles with special attributes. 在实现BundleListener / BundleTracker时,通常希望捕获具有特殊属性的包事件。

With a BundleListener you can catch the events that happen in the framework. 使用BundleListener,您可以捕获框架中发生的事件。 With a BundleTracker, you first catch the last events that happened every bundle in the framework than you can catch the new events. 使用BundleTracker,您首先要捕获框架中每个捆绑软件发生的最后事件,然后才能捕获新事件。 Often it is better to use a BundleTracker as you want to pick up bundles with those special attributes that are already active. 通常,使用BundleTracker更好,因为您想获取具有已激活的特殊属性的捆绑包。

The BundleListener / BundleTracker should be used with the help of the BundleContext of the bundle that implements the listener / tracker. BundleListener / BundleTracker应该在实现侦听器/跟踪器的包的BundleContext的帮助下使用。 As code should not run in your bundle before it is "starting", the BundleContext should be always available when you want to register the listener / tracker. 由于代码在“启动”之前不应该在包中运行,因此当您要注册侦听器/跟踪器时,BundleContext应该始终可用。

It would be useful to know more about the use-case that you wanted to implement. 了解更多有关您要实现的用例的信息将很有用。 Maybe you do not even need to implement a BundleListener / BundleTracker at all, just re-design the code in your bundle a bit. 也许您甚至根本不需要实现BundleListener / BundleTracker,只需稍微重新设计包中的代码即可。

I would suggest using a BundleTracker to track and respond to bundle state changes. 我建议使用BundleTracker来跟踪和响应捆绑状态更改。 Also, you had to use a BundleContext to install the bundle. 另外,您必须使用BundleContext来安装捆绑软件。 So you can use your BundleContext to create the BundleTracker. 因此,您可以使用BundleContext创建BundleTracker。

Do you want Bundle A to know when Bundle B gets activated, or do you want to execute some code when bundle B gets activated? 您是否要让捆绑软件A知道捆绑软件B何时激活,还是要在捆绑软件B激活时执行一些代码? In the second case, a Bundle Activator minght fit your needs... 在第二种情况下,Bundle Activator可以满足您的需求...

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

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