OSGi Declarative Services に対するサービスの宣言

別個の XML ファイルを使用して、バンドル内の各サービスを宣言できます。

このタスクについて

Declarative Services (DS) サポートは、宣言されたコンポーネントで動作します。各コンポーネントは、バンドル内の XML ファイルで定義されます。 コンポーネントの宣言が入ったバンドルがフレームワークに追加されると、DS は各コンポーネント宣言を読み取って、提供サービスをサービス・レジストリーに登録します。 その後、DS はコンポーネントのライフサイクルを管理します。 宣言された属性と満たされた依存関係の組み合わせに基づいて、ライフサイクルを制御します。

コンポーネントの XML 記述によって、DS は、コンポーネントのインスタンス化やその実装クラスのロードを必要とせずに、サービス依存関係を解決することができます。これによって、late (ランタイム・エレメントを必要時のみにロード) および lazy (最小数のエレメントをロード) のリソース・ロードが容易になり、サーバー始動の改善とランタイムのメモリー占有スペースの削減を図ることができます。

コンポーネントについて記述した XML ファイルは、Service-Component ヘッダーを使用してバンドルの MANIFEST.MF ファイルにリストされ、規則により、バンドルの /OSGI-INF ディレクトリーに配置されます。

必要な XML の生成に使用できるツールは多数あります。以下の例は、XML 自体を示しています。

このトピックでは、XML を使用してコンポーネントを DS に宣言する単純な OSGi バンドルについて説明します。

手順

  1. コンポーネントを実装クラス名で識別します。
    <component>
      <implementation class="com.example.bundle.HelloComponent"/>
    </component>
  2. 提供するインターフェースの名前を参照することで、サービスを宣言します。この名前が、バンドルの開始時に DS によってサービス・レジストリーに公開されます。
    <component>
      <implementation class="com.example.bundle.HelloComponent"/>
      <service>
         <provide interface="com.example.HelloService"/>
      </service>
    </component>
  3. コンポーネントに名前を付けます。このコンポーネント名は、サービスの「パーシスト ID」(PID) としても機能します。この ID を使用して、構成プロパティーをサービスと関連付けます。アクティベーション時、およびプロパティーの更新時は常に、一致する PID を持つ構成プロパティーがコンポーネントに注入されます。
    <component name="HelloService">
      <implementation class="com.example.bundle.HelloComponent"/>
      <service>
        <provide interface="com.example.HelloService"/>
      </service>
    </component>
    注: Liberty で、ユーザーは以下のエレメントを server.xml 構成ファイルに追加できます。プロパティーは HelloComponent クラスに注入されます。
    <HelloService firstKey="firstValue" secondKey="secondValue" />
  4. XML ファイルをバンドルにパッケージ化します。
    例えば、XML ファイルがロケーション OSGI-INF/HelloService.xml にあるとき、DS がそのファイルを見つけることができるよう、ヘッダーをバンドル・マニフェスト MANIFEST.MF ファイルに追加します。
    Service-Component: OSGI-INF/HelloService.xml
    複数のコンポーネントを同じバンドルにパッケージ化する場合、対応する XML ファイルをコンマ区切りのリストとして入力する必要があります。以下に例を示します。
    Service-Component: OSGI-INF/HelloService.xml, OSGI-INF/GoodbyeService
  5. HelloService コンポーネントの Java™ 実装は以下のようになります。
    package com.example.bundle;
    
    import com.example;
    import org.osgi.service.component.ComponentContext;
    
    /*
     * This class must be public and have a public default constructor for it to be 
     * usable by DS. This class is not required to be exported from the bundle.
     */
    public class HelloComponent implements HelloService {
    	/**
    	 * Optional: DS method to activate this component. If this method exists, it
    	 * will be invoked when the component is activated. Best practice: this
    	 * should be a protected method, not public or private
    	 * 
    	 * @param properties
    	 *            : Map containing service & config properties
    	 *            populated/provided by config admin
    	 */
    	protected void activate(ComponentContext cContext,
    			Map<String, Object> properties) {
    		modified(properties);
    	}
    
    	/**
    	 * Optional: DS method to deactivate this component. If this method exists,
    	 * it will be invoked when the component is deactivated. Best practice: this
    	 * should be a protected method, not public or private
    	 * 
    	 * @param reason
    	 *            int representation of reason the component is stopping
    	 */
    	protected void deactivate(ComponentContext cContext, int reason) {
    	}
    
    	/**
    	 * Optional: DS method to modify the configuration properties. This may be
    	 * called by multiple threads: configuration admin updates may be processed
    	 * asynchronously. This is called by the activate method, and otherwise when
    	 * the configuration properties are modified while the component is
    	 * activated.
    	 * 
    	 * @param properties
    	 */
    	public synchronized void modified(Map<String, Object> properties) {
    		// process configuration properties here
    	}
    
    	/**
    	 * Service method defined by com.example.HelloService interface
    	 */
    	public void sayHello() {
    		System.out.println("Hello");
    	}
    }

トピックのタイプを示すアイコン タスク・トピック



タイム・スタンプ・アイコン 最終更新: Monday, 5 December 2016
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=cord&product=was-libcore-mp&topic=twlp_declare_services_ds
ファイル名: twlp_declare_services_ds.html