tags:

views:

66

answers:

1

As I understand, OSGi services can be unregistered anytime, including when they are in use.

Consider an OSGi service which internally makes a long-running JNI call. And while that JNI call is executing, the service is unregistered by OSGi.

Will the JNI call be allowed to finish or terminated mid-way? What if it was just a normal non-jni long running Java call? Will that call be allowed to finish execution or will OSGi terminate everything immediately and unregister?

What is the expected behavior in such a case? Does the expected behavior depend on if the service was loaded using a 'tracker' or not?

SG

+2  A: 

I think even with "normal" (non-JNI) services, OSGi will not stop any processing that is already going on. In fact, if a bundle somehow keeps a reference to a service instance after it has been unregistered (which it is not supposed to do), it can still call methods on that object (whether those will still work or not is entirely dependent on the service implementation).

There is no "magic" going on with OSGi services. The bundle lifecycle just controls what gets registered and unregistered in the service registry. Once you have a reference to a service implementation, it is just plain old Java method calls.

So, to answer you question, unless the bundle (or service) itself does anything to terminate ongoing activity, the long-running call will finish.

Thilo
Thanks, my own tests seem to confirm all this. Long running jni and non-jni calls keep running and calls to service methods can still be made after service has been unregistered.
schngrg
It is very dangerous to rely on being able to call service methods after the service instance has been unregistered. If the service method causes any class loading to occur(for instance by going down a code path it hasn't been in before) the class load will fail, often taking down the calling thread with it. The 'right' way to handle this is to cause the system to wait on your long running call to finish before continuing with the unregistering of the services.
James Branigan
@James Branigan: Totally agree. I wrote "not supposed to do that". I just wanted to point out that there is no magic behind the scenes to enforce following the rules. You have to show some discipline (or even better use tools like Service Trackers and Dependency Injection instead of manually looking up and referencing services)
Thilo