質問

It is posible to syncronize an osgi service from outside it,from an other bundle ?

OsgiService oS = retrieveService(BundleContext);

synchronized(oS){
...
}

I dont want to alow the calls from outside a bundle to do changes during a specific method execution inside.

The bundle is registered and active. I am working under Equinox framework.

役に立ちましたか?

解決

No. When you publish a service you declare it as being generally available, without constraints on the way in which it is used. For example you cannot limit other bundles to call methods in a particular order, or require them to use synchronization, or to always call from a particular thread. Therefore you must assume that you will be called from any thread, without synchronization, etc.

If the internal state of your service implementation is fragile and requires synchronization, then it is your responsibility to do it inside your implementation. You cannot force this work onto somebody else.

他のヒント

Under specific constraints, yes, as the Equinox framework does not create proxies for services. However, that is a very brittle solution, as other parts of the system may or may not synchronize these calls...

Did you know you can replace services in OSGi? Create a class that supports all the proper interfaces for your use case, but which is published as a service with a higher SERVICE_RANKING property than the existing service; this will cause this instance to be preferred when looking up services. Now, this class delegates all the method calls to the original service, which it obtains either using Declarative Services, a ServiceTracker or the BundleContext; in all cases using a Filter for SERVICE_RANKING. This allows you to intercept all method calls and f.e. log or synchronize them.

Hope this helps...

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top