ManagedService 인터페이스를 사용하여 구성 데이터 수신

Liberty 구성은 OSGi 구성 관리 서비스에 의해 관리되며, OSGi 구성 관리 서비스 스펙에 따라 액세스가 가능합니다. server.xml 파일의 요소를 연관시키는 데 사용되는 PID(Persisted Identity)를 사용하여 구성 특성 세트가 식별되며, PID는 특성을 수신하기 위해 등록하는 컴포넌트에 요소 이름으로 사용됩니다.

이 태스크 정보

BundleActivator 인터페이스를 사용하여 라이프사이클을 관리하는 OSGi 번들의 경우 구성 특성을 수신하는 간편한 방법은 PID를 특성으로 지정하는 org.osgi.service.cm.ManagedService 인터페이스를 구현하는 것입니다.

예제

알아두기:
  1. Eclipse에서, > 환경 설정 > 플러그인 개발 > 대상 플랫폼에서 SPI 대상 런타임을 선택해야 합니다.

  2. 다음 명령문을 MANIFEST.MF 파일에 추가하십시오.
    Import-Package: org.osgi.service.cm;version="1.5.0"
  3. Ctrl + Shift + O을 눌러서 번들 활성기를 업데이트하십시오.

이 예제에서 Activator 클래스는 ManagedService 인터페이스와 BundleActivator 인터페이스를 구현하며 updated 메소드를 사용하여 구성 특성을 수신합니다. 사용자 구성에서 지정해야 하는 내용을 단순화하기 위해 기본 특성 값을 제공할 수 있습니다.

public class Activator implements BundleActivator, ManagedService {

  public void start(BundleContext context) throws Exception {
    System.out.println("Sample bundle starting");
    // register to receive configuration
    ServiceRegistration<ManagedService> configRef = context.registerService(
      ManagedService.class,
      this,
      getDefaults()
      );
  }

  public void stop(BundleContext context) throws Exception {
    System.out.println("Sample bundle stopping");
    configRef.unregister();
  }

  Hashtable getDefaults() {
    Hashtable defaults = new Hashtable();
    defaults.put(org.osgi.framework.Constants.SERVICE_PID, "simpleBundle");
    return defaults;
  }

  public void updated(Dictionary<String, ?> properties) throws ConfigurationException {
    if (properties != null)
    {
      String configColor = (String) properties.get("color");
      String configFlavor = (String) properties.get("flavor");
    }
  }

}
번들에 대한 사용자 구성은 server.xml 파일 또는 포함 파일에서 다음 항목을 사용하여 선택적으로 제공할 수 있습니다.
<simpleBundle color="red" flavor="raspberry" />
참고: 사용자 구성의 요소 이름 simpleBundleManagedService 등록에 사용되는 org.osgi.framework.Constants.SERVICE_PID 특성의 값과 일치됩니다.

고급 구성에 대해서는 OSGi Metatype Service를 사용하여 구성 설명의 내용을 참조하십시오.


주제의 유형을 표시하는 아이콘 태스크 주제

파일 이름: twlp_receive_config_data_managedservice.html