OSGI Service components With same interface

920 views Asked by At

I need to create service components with same interface. That mean i have different implementations for same interface. I tried to create two Components with same interface either one only active.

i am using equinox declarative. Do you have any better design to solve this problem? please find my configuration below.

Component1.xml

 <?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="comp1">
   <implementation class="com.demo.impl.CompOneImpl"/>
   <service>
      <provide interface="com.demo.IComponent"/>
   </service>
 </scr:component>

Component2.xml

    <?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="comp2">
   <implementation class="com.demo.impl.CompTwoImpl"/>
   <service>
      <provide interface="com.demo.IComponent"/>
   </service>
 </scr:component>

Accessing component from consumer

Consume component

comp1.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp1">
   <implementation class="com.demo.service.ConsumeCompOne"/>
   <reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp1" policy="static" unbind="unsetComp"/>
</scr:component>

comp2.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp2">
   <implementation class="com.demo.service.ConsumeCompTwo"/>
   <reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp2" policy="static" unbind="unsetComp"/>
</scr:component>

when i try to access comp1 and comp2 respectively through ConsumeCompOne and ConsumeCompTwo class i am always getting same component for both, either comp1 or comp2. Please help me to solve this.

Thankyou in advance

gopy

3

There are 3 answers

4
Neil Bartlett On

DS is working correctly, so there is nothing to solve here.

As you said, both consumer components get provided with an instance of the IComponent service, which is exactly what they are requesting as references.

1
Christian Schneider On

If you want to be able to select a specific instance of a service then publish it with properties to identitfy it. On the client you can then use an OSGi service filter to select the one that matches e.g. a special key/value combination.

1
BJ Hargrave On

You want to use the target attribute on the reference element to select the specific service to bind.

comp1.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp1">
   <implementation class="com.demo.service.ConsumeCompOne"/>
   <reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp1" policy="static" unbind="unsetComp"
     target="(component.name=comp1)"/>
</scr:component>

comp2.xml

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="consumeComp2">
   <implementation class="com.demo.service.ConsumeCompTwo"/>
   <reference bind="setComp" cardinality="1..1" interface="com.demo.IComponent" name="comp2" policy="static" unbind="unsetComp"
     target="(component.name=comp2)"/>
</scr:component>