1

I've read in many articles that it is necessary to unget service as soon as service was used. I mean

//1. get service
ServiceReference reference = bundleContext.getServiceReference(Foo.class.getName());
 Foo foo=(Foo) bundleContext.getService(reference);
//2. use service
 foo.foo();
//3. unget service
bunldeContext.ungetService(reference)

Besides from here

The OSGi framework maintains a use count of each service for your bundle.

This is from one side. From another side osgi services come and go, so in dynamic world it is bad practice to keep references to osgi services but it is good practice to get services when they are required.

If this statement correct then what about ServiceTracker? As I understand it keeps both reference to service and ServiceReference as service comes and keeps them till service goes. But what about that ServiceReference must be unget as soos as service was used? Or in case of ServiceTracker we mean that service using time= time during which tracker is open?

So main question is - when does ServiceTracker unget ServiceReference and why?

Community
  • 1
  • 1
Pavel_K
  • 10,748
  • 13
  • 73
  • 186

1 Answers1

2

You should really be using OSGi's Declarative Services rather than using the raw service API or even Service Tracker. That said, Service Tracker listens to the Service Events for the services it is tracking. So it will unget the service when the the service is unregistered or the tracker is closed.

BJ Hargrave
  • 9,324
  • 1
  • 19
  • 27