【问题标题】:How can I reference all components that implement a given interface?如何引用实现给定接口的所有组件?
【发布时间】:2015-06-22 10:15:32
【问题描述】:

我在 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)。

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

【问题讨论】:

  • 它没有搜索名称为org.example.Producer 的组件。它没有在任何地方这么说。它正在搜索类型 org.example.Producer的服务。
  • 您的示例没有任何明显错误,您引用的消息不一定是错误(毕竟它只是说“MESSAGE”)。你能看到什么实际效果?是否发布了任何org.example.Producer 服务?当 DS 无法绑定到某个服务时,最常见的原因是该服务不存在!
  • 尝试使用类似于here 提供的示例的实现。
  • @d33t 谢谢,但正如您在我的回答中看到的那样,我已经开始工作了。

标签: osgi apache-felix equinox declarative-services


【解决方案1】:

正如 Neil Bartlett 指出的那样,我被提到的日志消息误导了。相应的服务刚刚启动了很长时间,但最终它们都被正确绑定了。

【讨论】:

    猜你喜欢
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2019-06-13
    • 2015-10-09
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多