【问题标题】:State unsatisfied (reference) for service in Karaf - where is the mistake? (OSGi, declarative service, annotations)状态不满意(参考)在卡拉夫的服务 - 错误在哪里? (OSGi、声明式服务、注释)
【发布时间】:2017-02-24 00:34:23
【问题描述】:

我有一个非常简单的服务提供者和消费者。出于某种原因,我无法解决我的消费者会使用提供者服务的问题。 以下是提供者的捆绑源代码:

package test;

import org.osgi.framework.BundleContext;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Deactivate;

@Component (name = "MyProvider", immediate = true)
public class TestClass implements SimpleMathI {

    public TestClass() {
        System.out.println("contructing TestClass");
    }

    @Activate
    protected void activate(ComponentContext c, BundleContext b) {
        System.out.println("activate testClass ");
    }

    @Deactivate
    protected void deactivate() {
        System.out.println("de-activate testClass");
    }

    @Override
    public void doSimpleAdd(int x, int y) {
        System.out.println("Result(TestClass): " + (x + y));
    }

    @Override
    public void doSimpleSubstract(int x, int y) {
        System.out.println("Result(TestClass): " + (x - y));
    }
}

它注册了组件 MyProvider 和服务 test.SimpleMathI (listed in karaf)

这里是消费者:

如果我不引用服务 SimpleMathI,而只引用 ConfigurationAdmin,它可以正常工作!

package test;

import org.osgi.framework.BundleContext;
import org.osgi.service.cm.ConfigurationAdmin;
import org.osgi.service.component.ComponentContext;
import org.osgi.service.component.annotations.Activate;
import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.ConfigurationPolicy;
import org.osgi.service.component.annotations.Deactivate;
import org.osgi.service.component.annotations.Reference;

@Component (name = "MyConsumer", immediate = true, configurationPolicy = ConfigurationPolicy.OPTIONAL)
public class TestClass2 {

    public TestClass2() {
        System.out.println("contructing TestClass2");
    }

    @Reference (bind = "bind", unbind = "unbind")
    ConfigurationAdmin cm; // works

    @Reference (bind = "bindSimpleMathI", unbind = "unbindSimpleMathI")
    SimpleMathI simpleMath; // does not work, see screenshot

    @Activate
    protected void activate(ComponentContext c, BundleContext b) {
        System.out.println("activate testClass2");
        // simpleMath.doSimpleAdd(20, 25);
    }

    @Deactivate
    protected void deactivate() {
        System.out.println("de-activate testClass2");
    }

    protected void bind(ConfigurationAdmin a) {
        System.out.println("binding");
    }

    protected void unbind(ConfigurationAdmin a) {
        System.out.println("un-binding");
    }

    protected void bindSimpleMathI(SimpleMathI a) {
        System.out.println("binding!!");
    }

    protected void unbindSimpleMathI(SimpleMathI a) {
        System.out.println("un-binding!!");
    }
}

这是Karaf webconsole中的输出。

我用谷歌搜索了很多,但仍然无法弄清楚我错过了什么。奇怪,因为代码非常简单和透明。那么,我实现了什么错误的提供者或消费者?

Karaf 4.0.7,不使用 Apache felix,纯 OSGi R6 声明式服务

【问题讨论】:

    标签: java osgi karaf declarative-services


    【解决方案1】:

    我认为你犯了一个简单的错误。在 OSGi 中,您不应该有两个具有相同包的捆绑包。

    OSGi 中的典型设置是包含三个包:

    • 接口的test.api
    • 服务实现的 test.impl 或 test.provider
    • some.other.package 用于您的消费类

    您可以将 test.api 和 test.impl 放在同一个包中,也可以使用单独的 api 包。

    在任何情况下,消费者都不应该将接口包嵌入到自己的包中。相反,它的清单中应该有一个用于 api 包的 Import-Package。

    【讨论】:

    • 好的,现在我明白了。还注意到大多数人使用三个包的方法。确实有道理。
    【解决方案2】:

    这是一个很好的观点,不知道。在我将消费者的包名称更改为 test2 后,它仍然无法解析,因为它正在寻找带有 test2.SimpleMathI 的服务。

    为了解决这个问题,我将带有 test 包的提供者的 Eclipse 项目导入到消费者的项目中,并在 pom.xml 中指定了对提供者包的依赖项:

    <dependency>
        <groupId>com</groupId>
        <artifactId>DStestDS</artifactId>
        <version>0.0.18</version>
        <type>bundle</type>
    </dependency>
    

    并修改消费者代码如下:

    @Reference (bind = "bindSimpleMathI", unbind = "unbindSimpleMathI")
    test.SimpleMathI simpleMath; 
    

    但我不确定这是否正确......它以某种方式将我连接到这个特定版本 (18)。在manifest.mf中我曾经指定版本的范围,在pom.xml中怎么做?

    wokring screenshot

    谢谢克里斯蒂安,你又帮助了我。

    【讨论】:

      猜你喜欢
      • 2013-10-27
      • 2013-10-09
      • 2018-12-25
      • 2012-04-27
      • 1970-01-01
      • 2018-05-20
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多