0
votes

I have 10 bundles (A,B,C..). And in bundle A I want to create service tracker to track those services which implement some interface. These osgi services can be in any bundle (A,B,C..). I thought that I should create one service tracker and start it. However, what confuses me is that it's necessary to pass a reference to bundleContext to constructor (of what bundle???)

 public class MyServiceTracker extends ServiceTracker{
 public MyServiceTracker(BundleContext context){
        super(context, SomeInterface.class.getName(), null);
        open();
    }
  ....
 }

And in javadoc it says (about constructor)

context - The BundleContext against which the tracking is done.

The question - why should I pass bundleContext, what bundleContext should I pass or should I create instance of tracker for every bundle (A,B,C..)?

3

3 Answers

2
votes

Each bundle that uses a service will need its own ServiceTracker. The ServiceTracker will use the bundle's context to find and bind to services. You do not want to use the context of another bundle to track to services for your bundle since many things will not work properly such as Service Hooks since all the services will appear to be used by the one bundle whose context is used for the ServiceTracker.

Better yet, don't use ServiceTrackers at all. Use Declarative Services!

0
votes

It's the BundleContext of the bundle that will contain the ServiceTracker. So in your case, bundle A.

0
votes

You can implement a service component which listens when other services with specific interface (your requirement) come and go (dynamic service reference). You can find one possible sample implementation in the acs example project.