Say I am using a service A
which is imported in another service B
. While B
is running normally(ofcourse A
is Active), what will happen is service A
is uninstalled while service B
is still running?
Service A -> Service B
What will be the different scenarios in case I am using ServiceReference
, ServiceTracker
& DS
?
When a service is unpublished in OSGi, an event is sent to all bundles currently using that service to tell them that they should stop using it.
If you are using DS, your unbind method will be called. When it is called, you should make best efforts to stop using the service as soon as possible. But ultimately OSGi is a cooperative system, it cannot force you to release the service. However if you don't then you can cause problems, for example the service publisher will not be fully garbage-collected. You end up sabotaging the dynamics of the OSGi platform, possibly creating memory leaks and so on.
If you are using
ServiceTracker
then theremovedService
method will be called, and you need to respond in the same way. But didn't I tell you in the other question not to useServiceTracker
?? ;-)If you are using
ServiceReference
then you need to explicitly register aServiceListener
in order to receive these events. This is why you really really shouldn't use this low-level API until you have gained a lot more experience (and once you do have that experience, you won't want to use it anyway!).