0

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?

3 Answers3

2

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.

Balazs Zsoldos
  • 6,036
  • 2
  • 23
  • 31
1

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.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27
  • Thank you for your help. I will read what is BundleTracker and how to use it. You know, you always help me with my osgi questions. Thank you very much for it. And I ask you again, can you take a look at one question - the last most important question which I must solve to build architecture for my current project. This question http://stackoverflow.com/questions/23174582/rmi-classcastexception-in-osgi-client-accessing-ejb-from-javaee-server What is the problem? Maybe I am a stupid idiot asking quite wrong question? Because nobody answered it. –  May 04 '14 at 16:46
0

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...

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40