1

I am using the Apache Felix Service Component Runtime (SCR) on an Eclipse Equinox OSGi environment.

There are several components declared that implement the interface org.example.Producer like:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerA">
    <implementation class="org.example.ProducerA"/>
    <service>
        <provide interface="org.example.Producer"/>
    </service>
</scr:component>

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ProducerB">
    <implementation class="org.example.ProducerB"/>
    <service>
        <provide interface="org.example.Producer"/>
    </service>
</scr:component>

Now in an other component I like to reference all those components that implement the interface org.example.Producer dynamically:

<?xml version="1.0" encoding="UTF-8"?>
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="org.example.ConsumerA">
    <implementation class="org.example.ConsumerA"/>
    <reference bind="bindProducer" cardinality="0..n" interface="org.example.Producer" policy="dynamic" unbind="unbindProducer"/>
    <service>
        <provide interface="org.example.Consumer"/>
    </service>
</scr:component>

But this gives an error at runtime. It seems that the SCR includes a component name into its search filter:

!ENTRY org.eclipse.equinox.ds 1 0 2015-06-22 11:31:31.781
!MESSAGE Could not bind a reference of component org.example.ConsumerA. The reference is: Reference[name = org.example.Producer, interface = org.example.Producer, policy = dynamic, cardinality = 0..n, target = null, bind = bindProducer, unbind = unbindProducer]

As you see in the error message it is searching explicitly for components with the name org.example.Producer. But none of the above listed components has that name (org.example.ProducerA, org.example.ProducerB).

So the question is how can I reference components dynamically that provide implementations for a given interface by ignoring their names?

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59
  • 1
    It is not searching for components with the name `org.example.Producer`. It doesn't say that anywhere. It's searching for services of **type** `org.example.Producer`. – Neil Bartlett Jun 22 '15 at 10:31
  • 1
    There is nothing obviously wrong with your example, and the message you quoted isn't necessarily an error (it just says "MESSAGE" after all). What actual effects can you see? Are any of the `org.example.Producer` services published? When DS is unable to bind to a service the most common cause is that the service doesn't exist! – Neil Bartlett Jun 22 '15 at 10:36
  • Try with an implementation similar to the sample provided [here](https://github.com/Adobe-Consulting-Services/acs-aem-samples/blob/master/bundle/src/main/java/com/adobe/acs/samples/services/impl/SampleMultiReferenceServiceImpl.java). – d33t Jun 22 '15 at 14:14
  • @d33t Thanks, but I got it already working as you can read in my answer. – Henrik Sachse Jun 22 '15 at 14:17

1 Answers1

0

I was mislead by the mentioned log message as Neil Bartlett pointed out. The corresponding services just took very long to start up but at the end they were bound correctly.

Henrik Sachse
  • 51,228
  • 7
  • 46
  • 59