【问题标题】:cannot startup a Declarative Services (DS) component using bndtools无法使用 bndtools 启动声明式服务 (DS) 组件
【发布时间】:2015-12-05 22:38:51
【问题描述】:

我已经为 Eclipse 安装了标准的 bndtools 插件,启动并运行 apache felix osgi 运行时。我正在尝试了解声明性服务 (DS) 组件。显然,在没有注释之前(bndtools 教程中给出了一个示例),组件是使用 xml 数据编写的。这就是我想要做的。

这是一个简单的类(将作为 DS 组件发布):“HelloComponent.java”

package org.osgi.book.ds.minimal;

public class HelloComponent {
    public HelloComponent(){
        System.out.println("HelloComponent created.");
    }
}

这里是进行组件声明的 xml 文件:“minimal.xml”

<?xml version="1.0" encoding="UTF-8"?>
<!-- minimal.xml -->

<scr:component xmlns:scr="http://www.osgi.org/xlmns/scr/v1.1.0" immediate="true">
    <implementation class="org.osgi.book.ds.minimal.HelloComponent"/>
</scr:component>

这里是应该被 bndtools 用来生成最终将发布到 OSGi 运行时的 jar 文件的 .bnd 文件:“minimal_ds.bnd”

Private-Package: org.osgi.book.ds.minimal

Include-Resource: minimal.xml
Service-Component: minimal.xml

请注意,我在主机运行时 OSGi 容器中启动并运行了以下捆绑包:

   ID|State      |Level|Name
    0|Active     |    0|System Bundle (4.4.1)
    2|Active     |    1|Apache Felix Gogo Command (0.14.0)
    3|Active     |    1|Apache Felix Gogo Runtime (0.12.1)
    4|Active     |    1|Apache Felix Gogo Shell (0.10.0)
    5|Active     |    1|BookReaderOSGiInPractice.minimal_ds (0.0.0.201509091856)
   15|Active     |    1|Apache Felix Configuration Admin Service (1.8.0)
   16|Active     |    1|Apache Felix Declarative Services (1.8.2)
   17|Active     |    1|osgi.enterprise (4.2.0.201003190513)
   18|Active     |    1|osgi.residential (4.3.0.201111022239)

尽管一切都在积极运行,但我无法弄清楚为什么 DS 组件没有被初始化(我应该看到控制台输出:“HelloComponent created.”)。任何帮助表示赞赏。

最后是项目目录结构:

BookReaderInPractice
|
|- src
|   |- org.osgi.book.ds.minimal
|           |_ HelloComponent.java
|
|- minimal_ds.bnd
|
|- minimal.xml

【问题讨论】:

    标签: osgi declarative-services bnd bndtools


    【解决方案1】:

    更新(已编辑)

    按照 Neil Bartlett 的建议更新:原来答案更简单:正如我在评论中所写,DS xml 文件的 xml 命名空间中有一个错字:“xlm”而不是“xml”。

    原答案

    我猜这里有两个问题:

    1. 文件 minimum.xml 未复制到生成的捆绑包 jar (位于“生成”文件夹中)
    2. 框架不知道 minimum.xml

    要解决此问题,请将以下几行放入 minimum_ds.bnd:

    Include-Resource: minimal.xml
    Service-Component: minimal.xml
    

    此外,不要使用 HelloComponent 的构造函数,而是创建一个这样的方法,该方法将在激活组件时调用:

    public void activate() {...}
    

    【讨论】:

    • 您推荐的行已经包含在 minimum_ds.bnd 中。此外,当我查看生成的 jar 时,还包含了 minimum.xml,以及 java src 包。使用 activate 方法也没有输出任何控制台消息。感谢您尝试...
    • 糟糕...完全错过了,抱歉。但是我现在在您的 minimum.xml 的 xml 命名空间中发现了一个错字:尝试“osgi.org/xmlns/scr/v1.1.0”,它会起作用;)
    • @evandor 我建议编辑您的答案以包含此实际解决方案,以帮助人们将来阅读此页面。
    猜你喜欢
    • 2012-10-01
    • 2010-11-09
    • 2016-08-26
    • 2014-04-16
    • 2015-12-11
    • 2012-04-27
    • 2014-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多