如何引用实现给定接口的所有组件?

时间:2015-06-22 10:15:32

标签: osgi apache-felix equinox declarative-services

我在Eclipse Equinox OSGi环境中使用Apache Felix服务组件运行时(SCR)。

声明了几个实现接口org.example.Producer的组件,如:

<?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>

现在在另一个组件中,我想引用动态实现接口org.example.Producer的所有组件:

<?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>

但这会在运行时出错。似乎SCR在其搜索过滤器中包含一个组件名称:

!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]

正如您在错误消息中看到的那样,它显式搜索名称为org.example.Producer的组件。但上面列出的组件都没有该名称(org.example.ProducerAorg.example.ProducerB)。

所以问题是如何通过忽略名称来动态引用为给定接口提供实现的组件?

1 个答案:

答案 0 :(得分:0)

Neil Bartlett指出,我被上述日志消息误导了。相应的服务需要很长时间才能启动,但最后它们被正确绑定。