0

OSGi でソケットを使用してクライアント サーバー モデルを作成しました。サーバー側にバンドルがあり、アクティベーター クラスがスレッドを呼び出してソケットを作成し、クライアント側から文字列データを取得します。ここで、サーバー側からサービスを呼び出して、この文字列を処理のために送信できるようにします。どうすればいいですか?

これは、サーバー側の私の Activator クラスです

int serverport=5000;
    Thread t;
    public void start(BundleContext bundleContext) throws Exception {
        Activator.context = bundleContext;
        t = new StdServer(serverport,this);
        t.start();

StdServer クラスは、ソケットの作成を処理するスレッドを拡張します。アクティベーターの開始関数でサービスを呼び出したい。どんな助けでも大歓迎です。

4

2 に答える 2

1

私があなたを正しく読んでいるなら、あなたはまだサーバー側の OSGi 環境にいて、同じコンテナー (Karaf など) で実行されているサービスを使用する必要があります。アクティベーターを使用すると、コンテキストでそれを取得できます。それを試しましたか?

Bnd アノテーションを使用する別のアプローチでは、宣言型サービスもコンテナーにインストールする必要があります。次に、Bnd アノテーションを使用して、「@Reference」がコンテナーから必要なサービスを取得する、次のようなクラスにアノテーションを付けることができます。

import java.util.Map;

import org.osgi.framework.BundleContext;

import aQute.bnd.annotation.component.Activate;
import aQute.bnd.annotation.component.Component;
import aQute.bnd.annotation.component.Deactivate;
import aQute.bnd.annotation.component.Reference;

//Doesn't have to be called Activator
@Component
public class Activator {
    private BundleContext context;
    private TheServiceINeed theServiceINeed;

 @Activate
 public void Activate(BundleContext context, Map<String, Object> props) {
 this.context = context;

 }

@Deactivate
public void Deactivate() {
    this.context = null;
}

public TheServiceINeed getTheServiceINeed() {
    return theServiceINeed;
}

    //The Service to process my String
@Reference
public void setTheServiceINeed(TheServiceINeed theServiceINeed) {
    this.theServiceINeed = theServiceINeed;
    }

}

BndToolsを使用して作業を行っていますか? あなたが私に尋ねれば、OSGi開発にはかなり便利です。

于 2013-11-09T10:48:29.413 に答える
0

でサービス参照を取得しgetService()、新しいスレッドのコンストラクターパラメーターとして配置できます。

于 2013-11-09T09:45:16.260 に答える