【问题标题】:Services not listed in Karaf, what might be the reason?服务未在 Karaf 中列出,可能是什么原因?
【发布时间】:2017-02-22 09:45:43
【问题描述】:

我正在使用来自 OSGi 联盟的 R6 OSGi 注释ma​​ven 和 Apache felix ma​​ven-scr-plugin 的组合。

编写一个简单的包后,我看不到其中的任何服务(使用 Karaf webconsole 或 service:list )

这同样适用于通过 BundleContext 手动注册服务的低级 API。

据我了解 ma​​ven-scr-plugin 在运行时为我生成清单和组件 XML 文件。

在下面的代码中,我希望服务 SimpleMathI 将在 Karaf Service Registry 中注册: 我错过了什么吗?

package test;

//notice i don't use apache.felix, since:
//"Starting with the R6 release of the OSGi Declarative Services and Metatype specification, the official annotations support the same
//features as the Apache Felix SCR annotations in a more elegant manner and even provide additional functionality."

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
    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");
    }

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

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

    }
}

这是我的 pom 文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com</groupId>
  <artifactId>DStestDS</artifactId>
  <version>0.0.5</version>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-scr-plugin</artifactId>
        <version>1.20.0</version>
        <executions>
          <execution>
            <id>generate-scr-scrdescriptor</id>
            <goals>
              <goal>scr</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>   
  </build>

   <dependencies>      

    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.core</artifactId>
         <version>4.3.0</version>
    </dependency>

<!--  official R6 osgi annotations  -->
    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.service.component.annotations</artifactId>
        <version>1.3.0</version>
    </dependency>

    <dependency>
        <groupId>org.osgi</groupId>
        <artifactId>org.osgi.compendium</artifactId>
        <version>5.0.0</version>
    </dependency>           

    <dependency>
         <groupId>org.apache.felix</groupId>
         <artifactId>org.apache.felix.scr.annotations</artifactId>
         <version>1.9.6</version>
         <scope>provided</scope>
    </dependency>           

   </dependencies>
</project>

【问题讨论】:

    标签: java maven osgi apache-felix


    【解决方案1】:

    您是否忘记安装 scr 功能?

    feature:install scr

    你的 pom 似乎也坏了。您需要使用 maven-bundle-plugin 或 bnd-maven-plugin。如果您使用 OSGi 规范 DS 注释,则不需要 maven scr 插件。

    这是我在构建中使用的: https://github.com/cschneider/Karaf-Tutorial/blob/master/tasklist-ds/pom.xml#L107-L118

    它创建捆绑包并处理 DS 规范注释。

    【讨论】:

    • 感谢小伙伴的回复。
    • 按照你的建议,我执行了“feature:install scr”并重新启动了 Karaf。没有消息,重新启动后没有任何变化。我还导入了 maven-bundle-plugin 并删除了 maven-scr-plugin (实际上我使用了最后一个,因为我需要自动生成描述符),但仍然没有变化。
    • 后续问题:我是否还需要将下面的代码添加到maven-bundle-plugin中?:&lt;configuration&gt; &lt;instructions&gt; &lt;!-- Enable processing of OSGI DS component annotations --&gt; &lt;_dsannotations&gt;*&lt;/_dsannotations&gt; &lt;!-- Enable processing of OSGI metatype annotations --&gt; &lt;_metatypeannotations&gt;*&lt;/_metatypeannotations&gt; &lt;/instructions&gt; &lt;/configuration&gt;
    • maven-bundle-plugin 的最新版本默认正确处理 OSGi 规范 DS 注释。要验证您的构建,请检查您的 jar 是否在您的 DS 组件的 Manifest 和 xml 文件中包含捆绑道具
    • 感谢克里斯蒂安支持我。我没听懂你的最后一句话。如果 Manifest 和 xml 文件是自动生成的并且我无法直接控制此过程,我该如何验证它们。我什至在我的 Eclipse 项目结构中都没有看到它们。似乎这些文件在运行时生成然后被删除。我还查看了您的 pom.xml 文件示例并添加了 maven 编译器插件,但仍然没有运气。 (我已经更新了上面最新的 pom.xml 文件)
    【解决方案2】:

    根据 Christian 的建议,我在 pom.xml 文件中添加了 maven-bundle-plugin 并删除了 maven-scr-plugin,现在 pom.xml 如下所示:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com</groupId>
      <artifactId>DStestDS</artifactId>
      <version>0.0.10</version>
    
       <dependencies>   
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.core</artifactId>
            <version>4.3.0</version>
        </dependency>    
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.service.component.annotations</artifactId>
            <version>1.3.0</version>
        </dependency> 
        <dependency>
            <groupId>org.osgi</groupId>
            <artifactId>org.osgi.compendium</artifactId>
            <version>5.0.0</version>
        </dependency>     
        <dependency>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>3.5.1</version>
        </dependency>    
       </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.1</version>
                    <configuration>
                        <source>1.7</source>
                        <target>1.7</target>
                    </configuration>                
                </plugin>
                <plugin>
                    <groupId>org.apache.felix</groupId>
                    <artifactId>maven-bundle-plugin</artifactId>
                    <version>3.0.0</version>
                    <extensions>true</extensions>
                    <configuration>
                        <obrRepository>NONE</obrRepository>
                        <instructions>
                            <_include>-bnd.bnd</_include>
                        </instructions>
                    </configuration>
                </plugin>
            </plugins>
        </build>   
    </project>
    

    这是控制台的输出: no services listed

    所以我不明白何时以及为何注册服务?为什么不调用@Activate 方法?

    顺便说一句,我没有收到任何编译错误,也没有做“导出->可部署插件”项目,我只是简单地做 mvn clean install 获取输出 jar 文件并放入 Karaf 的 delploy 文件夹中。

    生成捆绑文件后,我查看了它,发现 META-INF\MANIFEST.MF 看起来像:

    Manifest-Version: 1.0
    Built-By: username
    Build-Jdk: 1.7.0_79
    Created-By: Apache Maven 3.3.9
    Archiver-Version: Plexus Archiver
    

    好像少了点什么,不是吗?

    我没有看到通过 karaf webconsole 列出的任何服务:

    karaf webconsole

    【讨论】:

    • 你必须使用打包包而不是jar。
    • 是的,刚刚添加了一秒钟(bundle),现在我通过 Karaf Web 控制台看到了该服务。奇怪的是,我看不到通过 Karaf service:list 提供的服务。还需要了解为什么不调用@Activate 方法(可能是第一次使用时会调用它)。再次感谢你的帮助。我已经接受了你上面的回答。
    • 您是否也将 immediate=true 添加到 @Component?
    • 你是绝对正确的。不知怎的,虽然一直在玩,但还是错过了它。现在一切都整理好了,谢谢大佬。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-25
    • 1970-01-01
    • 2013-02-16
    • 2021-09-01
    • 2012-01-01
    • 2020-09-09
    • 2016-01-04
    相关资源
    最近更新 更多