2016-10-13 47 views
0

我使用的是從OSGi聯盟,行家和Apache R6 OSGi的註解組合FELIX 的maven-SCR-插件服務未在卡拉夫列出,可能是什麼原因?

寫一個簡單的包後,我看不出它裏面的任何服務(使用Karaf Web控制檯或服務:列表)

同樣的作品通過BundleContext的底層API,在那裏我手動註冊服務。

據我瞭解maven-scr-plugin在運行時爲我生成清單和組件XML文件。

在下面的代碼中,我希望服務SimpleMathI將在卡拉夫服務註冊表中註冊: 我錯過了什麼嗎?

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> 

回答

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規範的註釋。

+0

的答覆感謝夥伴。 – neodix

+0

正如你建議我執行「功能:安裝scr」並重新啓動Karaf。沒有消息,重新啓動後沒有任何改變。我還導入了maven-bundle-plugin並刪除了maven-scr-plugin(實際上我使用了最後一個,因爲我需要自動生成描述符),但仍然沒有改變。 – neodix

+0

跟進問題:做我還需要添加以下代碼到行家束-插件?: '<配置> <指令> << - - 啓用OSGI DS組分註釋處理!> _dsannotations> * <! - 啓用OSGI元類型的註釋的處理 - > <_metatypeannotations> * ' – neodix

0

後基督教的建議,我已經加入Maven的捆綁,插件的pom.xml文件和刪除的maven-SCR-插件,現在的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方法?

BTW,我沒有得到任何編譯錯誤,也是我不做「導出 - >部署插件」的項目,我根本就MVN全新安裝採取輸出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 Web控制檯中列出的所有服務:

karaf webconsole

+0

您必須使用包裝袋而不是罐子。 –

+0

是的,剛剛添加了第二個前(捆綁),現在我通過Karaf Web控制檯看到該服務。奇怪的是,我沒有看到服務通過卡拉夫**服務:列表**。還需要了解爲什麼@Activate方法沒有被調用(可能會在第一次使用時調用它)。再次感謝你的幫助。我已經接受了上面的答案。 – neodix

+0

您是否也向@Component添加了immediate = true? –

相關問題