OSGi 선언 서비스에 대해 서비스 선언

별도의 XML 파일을 사용하여 번들 내에서 각 서비스를 선언할 수 있습니다.

이 태스크 정보

DS(Declarative Services) 지원은 선언된 컴포넌트에서 작동하며 컴포넌트는 각각 번들의 XML 파일에 의해 정의됩니다. 컴포넌트 선언을 포함하는 번들이 프레임워크에 추가되는 경우, DS는 서비스 레지스트리에서 각 컴포넌트 선언과 레지스터 제공 서비스를 읽습니다. 그러면 DS가 컴포넌트의 라이프사이클을 관리합니다. 즉, 선언된 속성과 충족되는 종속성의 조합에 따라 라이프사이클을 제어합니다.

컴포넌트의 XML 설명은 DS가 컴포넌트 인스턴스화 없이, 또는 해당되는 구현 클래스를 로드하지 않고 서비스 종속 항목 분석을 허용합니다. 이로서 시간 소모적인 자원 로딩을 원활하게 하며 서버 시작 및 런타임 메모리 공간을 개선하는 데 도움이 됩니다.

컴포넌트를 설명하는 XML 파일은 Service-Component 헤더를 사용하여 번들의 MANIFEST.MF 파일에 나열되고, 편리성을 위해 번들의 /OSGI-INF 디렉토리에 위치됩니다.

필요한 XML을 생성하는데 사용될 수 있는 많은 도구가 있습니다. 다음 예제는 XML 자체를 보여줍니다.

이 주제는 DS에 대해 해당 컴포넌트를 선언하기 위해 XML을 사용하여 단순한 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)로도 작동합니다. 일치하는 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 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) {
    			img_in.writeTo(fos);
    	}
    
    	/**
    	 	 * 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