Question

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. So, can can I do that?

Était-ce utile?

La solution

It does not make sense to register a BundleListener to catch events that happen with the same bundle. When you implement a BundleListener / BundleTracker, you normally want to catch the events of bundles with special attributes.

With a BundleListener you can catch the events that happen in the framework. With a BundleTracker, you first catch the last events that happened every bundle in the framework than you can catch the new events. Often it is better to use a BundleTracker as you want to pick up bundles with those special attributes that are already active.

The BundleListener / BundleTracker should be used with the help of the BundleContext of the bundle that implements the listener / tracker. 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.

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.

Autres conseils

I would suggest using a BundleTracker to track and respond to bundle state changes. Also, you had to use a BundleContext to install the bundle. So you can use your BundleContext to create the 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? In the second case, a Bundle Activator minght fit your needs...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top