0

I am using the OSGi framework Knopflerfish, since I can now call the bundle to do something by register a service, do I need to remove the service?

Because I cannot find any way to remove the service. By checking the services in BundleContext with the length of "getAllService()" and ServiceTracker.getServices(), I can see the total number of service is increasing, even I call "ungetService(ServiceReference)".

So is it necessary to remove the service or "ungetService" is enough for that? How can I remove the service if this is necessary?

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
dicky.lee
  • 86
  • 1
  • 6
  • I'm removing the references to Android since this is a pure OSGi question, Android does not seem relevant. – Neil Bartlett Jan 28 '16 at 09:55
  • @NeilBartlett I am using the Knopflerfish to develop a android app, but you are right, seems this is not Android relevant, thank you. – dicky.lee Jan 28 '16 at 10:02

1 Answers1

2

ungetService is not the opposite of registerService, it is the opposite of getService, i.e. the call that a consumer makes.

If you register a service with the registerService method, it gives you a ServiceRegistration object back. You should call the unregister method on that object when you no longer want your service to be available to consumers. Note that if your bundle stops, any services that it registered will be automatically unregistered.

However, I wonder if you should really be digging this deep into the lowest level OSGi APIs. For 99% of use cases you should be using a higher-level framework like Declarative Services. This will save you a hell of a lot of pain, and also make your code more reliable and readable.

Neil Bartlett
  • 23,743
  • 4
  • 44
  • 77
  • So I can only unregister the service in the bundle which register the service? Is there any way to get the register object? Thanks anyway since I am still new to OSGi, I will move to Declarative Services later. – dicky.lee Jan 28 '16 at 10:05
  • Yes. The intention is that only the actor who registers the service can unregister it. To get the registered object, call getService - any bundle can do this. – Neil Bartlett Jan 28 '16 at 11:05