tags:

views:

23

answers:

2

I am playing with OSGI and have a few bundles. Bundle A and B both contain a registered service which implement a single interface. The third bundle C includes code to lookup a service implementing the previously mentioned interface. The A and B bundles have different version numbers however it appears that C picks up the service from the first started bundle. I have changed the start level to do the right thing but version is only used to accept rather than order which service is returned.

   A version 1.0 start level 1
   B version 1.1 start level 2
   C requires both bundles, start level 3

In theee above example C always gets the service from A even though B has a higher bundle version. However if i switch the start level so B starts before A then C seens the B service.

I have searched the OSGI website and there is no clear explaination of whether versioning of a bundle is used to prioritise a service over another. My understanding seems to indicate that start level is supposed to be used to order bundle startup so that dependencies can be satisified correctly. However it appears to be overloaded so that it also prioritises service priority. Given allt he above i guess it makes sense not to use the bundle version in selection because the versionnumber is just a number relative to nothing.

What is the best way to prioritise one service over another, besides start level ?

A: 

In org.osgi.framework.BundleContext.registerService(String[], Object, Dictionary) you can specify arbitrary properties in the Dictionary. To find a service, you can specify a filter in org.osgi.framework.BundleContext.getServiceReferences(String, String). And if that's not enough, you can check org.osgi.framework.ServiceReference.getProperty(String), for example for a priority value.

FelixM
+1  A: 

The best way of prioritizing OSGi services to use SERVICE_RANKING service property. This property may be supplied in the properties object passed to the BundleContext.registerService() method.

According to the documentation of the BundleContext.getServiceReference() method:

If multiple such services exist, the service with the highest ranking (as specified in its Constants.SERVICE_RANKING property) is returned.

If there is a tie in ranking, the service with the lowest service ID (as specified in its Constants.SERVICE_ID property); that is, the service that was registered first is returned.

Pavol Juhos