Friday, March 13, 2009

Filter and Ranking attributes in OSGi

In a dynamic system we can have multiple services of same bean type. In that case a consumer of a service can specify a filter to qualify the required bean as follows:

In one bundle publish the service as:

<bean id="helloService" class="com.ivy.example.service.factory.HelloServiceFactory" factory-method="getHelloService" />

<osgi:service ref="helloService" interface="com.ivy.example.service.HelloService">
<entry key="product" value="account" />

To consume the service in another bundle:

<osgi:reference id="helloService" filter="(product=account)" interface="com.ivy.example.service.HelloService" />

<bean id="testData" class="" init-method="start" destroy-method="stop">
<property name="helloService" ref="helloService" /> <- dependency injection of the referenced bean

Ranking attribute can be specified while advertising a bean:

the one with the higest rank if obtained i case of two similar services

<osgi:service ref="helloService" interface="com.ivy.example.service.HelloService" ranking=”1” >

The ranking attribute is useful for determining the 'best matching' OSGi service in case multiple candidates are available.
If one needs to pin point a service then a filter should be used.

No comments:

Free Domain Names @!