I am creating some OSGi bundles. They register services and also get (and of course use) each other's services.
I decided to use ServiceTracker instead of Declarative Services.
As I was searching for information about this I found two approach of tracking services.
The first one is creating an own tracker class for each service, which extends the ServiceTracker class and overrides the methods that need to be overridden. Then in the activator class creating a new instance of this tracker class giving the bundle context to it and open it for tracking.
The other approach is creating a tracker class for each service, which implements the ServiceTrackerCustomizer interface and overrides the methods that need to be overridden. Then in the activator class creating a new instance of the ServiceTracker class giving to it the bundle context, the name of the service that needs to be tracked and a new instance of our customizer class. Then open it for tracking.
Are there any differences between the two approaches? I would say no. In the ServiceTracker javadoc I can see that the ServiceTracker class also implements the ServiceTrackerCustomizer interface.
Could you please tell me the pros and cons on both the approaches? Thanks in advance.