2016-10-14 11 views
0

私は非常に単純なサービスプロバイダとコンシューマを持っています。何らかの理由で私の消費者がプロバイダのサービスを利用するという問題を解決できません。ここ プロバイダのバンドルソースコード:カラフでのサービスに不満足な(参考) - 間違いはどこですか? (OSGi、宣言型サービス、注釈)

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

は、消費者である:

Iがサービスを参照しない場合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のフェリックス、純粋なOSGiのR6の宣言型サービス

答えて

0

。 OSGiでは、同じパッケージで2つのバンドルを持つべきではありません。以下のためのインタフェースサービスIMPLため

  • test.implまたはtest.provider
  • some.other.packageため

    • test.api:

      のOSGiでの典型的なセットアップでは、3つのパッケージを持っていることですあなたのコンシューマクラス

    test.apiとtest.implの両方を同じバンドルに入れることも、別々のapiバンドルを持つこともできます。

    いずれの場合でも、コンシューマはインターフェイスパッケージを独自のバンドルに埋め込むべきではありません。その代わりに、パッケージのマニフェストにImport-Packageがなければなりません。

  • +0

    今、私はそれを理解しました。また、ほとんどの人が3つのパッケージアプローチを使用していることに気づいた。それは確かに意味があります。 – neodix

    0

    それはわかりませんでした。コンシューマーのパッケージ名をtest2に変更した後も、test2.SimpleMathIでサービスを探していたので解決できませんでした。この問題を解決するには

    私は消費者のプロジェクトにパッケージテストとプロバイダの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

    あなたが再び私を助け、あなたのクリスチャンをありがとうございます。

    関連する問題