在「OSGi 宣告式服務」中宣告您的服務

您可以利用個別 XML 檔來宣告軟體組內的每個服務。

關於這項作業

「宣告式服務 (DS)」支援操作宣告式元件,每個宣告式元件都由軟體組中的一個 XML 檔來定義。 當包含元件宣告的軟體組新增到架構中,DS 會讀取各項元件宣告,將提供的服務登錄在服務登錄中。 之後,DS 會管理元件的生命週期:根據宣告的屬性及符合的相依關係之組合來控制它的生命週期。

元件的 XML 說明使 DS 無需元件的實例化或載入其實作類別,就能解析服務相依關係。 這有利於延遲而緩慢的資源載入,有助於改進伺服器的啟動及縮減執行時期的記憶體覆蓋區。

說明元件的 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. 將元件命名。 元件名稱也充當服務的「持續身分」或 PID,用來將配置內容與服務關聯起來。 啟動之時,以及每當更新內容之時,都會將含有相符 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,您新增一個標頭到軟體組資訊清單檔 MANIFEST.MF 中,讓 DS 能夠找到這個檔案:
    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");
    	}
    }

指示主題類型的圖示 作業主題

檔名:twlp_declare_services_ds.html