OSGi felix获得服务

时间:2017-03-20 01:37:20

标签: java osgi

我已经用两个felix示例注册了两个服务:

first 捆绑包中,我使用代码注册了一项服务。

Hashtable<String, String> props = new Hashtable<String, String>();
    props.put("Language", "English");
    context.registerService(DictionaryService.class.getName(), new DictionaryImpl1(), props);

second 捆绑包中,我使用代码注册了一项服务。

Hashtable<String, String> props = new Hashtable<String, String>();
props.put("Language", "Chinese");
context.registerService(DictionaryService.class.getName(), new DictionaryImpl2(), props);

但是当我获得带代码的服务引用时

ServiceReference[] refs = 
        context.getServiceReferences(DictionaryService.class.getName(), "(Language=*)");

参考的长度为一个而不是两个

当我在一个包中使用代码

注册两个实现一个接口的服务时
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("Language", "English");
context.registerService(DictionaryService.class.getName(), new DictionaryImpl1(), props);
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("Language", "Chinese");
context.registerService(DictionaryService.class.getName(), new DictionaryImpl2(), props);

这种方法

context.getServiceReferences(DictionaryService.class.getName(), "(Language=*)");

可以获得两个参考。

添加信息。

<dependency>
    <groupId>org.apache.felix</groupId>
    <artifactId>org.apache.felix.framework</artifactId>
    <version>5.6.1</version>
</dependency>

我使用5.6.1 felix版本。我在 org.apache.felix.framework.Felix 中调试源代码, 在第3584行

final Collection refList = m_registry.getServiceReferences(className, filter);

refList大小为2。

第3595行

if (!Util.isServiceAssignable(bundle, ref))
{
    refIter.remove();
}

它将删除最后一个bundle注册的服务引用。

0 个答案:

没有答案