2014-04-21 1 views
0

로 받기 내 ConfigUpdater 클래스구성 데이터는 관리 서비스 여기서

private final class ConfigUpdater implements ManagedService { 

    @SuppressWarnings("rawtypes") 
    @Override 
    public void updated(Dictionary config) throws ConfigurationException { 
     if (config == null) { 
      return; 
     } 

     String title = ((String)config.get("title")); 

    } 
} 

내 질문은 내가 다른 클래스에서 문자열 제목에 액세스 할 수있는 방법인가? 또는 다른 클래스에서 config 사전을 가져 오는 방법 ... 업데이트 된 메소드는 구성 파일이 변경 될 때만 호출됩니다 ... 일단 변경되면 다른 클래스의 데이터에 액세스 할 수 있습니까?

답변

1

일반적으로 이러한 속성을 다른 구성 요소에 제공하는 서비스를 만듭니다.

예를 들어, ConfigUpdater에 두 번째 인터페이스를 제공 할 수 있습니다. 또 다른 구성 요소는 서비스 레지스트리에서이 인터페이스를 검색/삽입하고 속성에 액세스하는 메소드를 사용할 수 있습니다.

@Component([email protected](name=Constants.SERVICE_PID, value="example.configurationservice")) 
public class ConfigurationUpdater implements ManagedService, MyConfiguration{ 

private volatile String message; 

@Override 
public void updated(@SuppressWarnings("rawtypes") Dictionary properties) throws ConfigurationException { 
    message = (String)properties.get("message"); 
} 

@Override 
public String getMessage() { 
    return message; 
} 
} 

이 구성은 다음과 같이 사용할 수 있습니다 :

https://github.com/paulbakker/configuration-example

가장 중요한 부분은 ManagedService 및 사용자 정의 인터페이스를 모두 구현하는 서비스입니다 :

나는 GitHub의에 대한 예제 프로젝트를 생성
@Component(provides=ExampleConsumer.class, 
    properties= { 
    @Property(name = CommandProcessor.COMMAND_SCOPE, value = "example"), 
    @Property(name = CommandProcessor.COMMAND_FUNCTION, values = {"showMessage"}) }) 
public class ExampleConsumer { 
    @ServiceDependency 
    private volatile MyConfiguration config; 

    public void showMessage() { 
    String message = config.getMessage(); 
    System.out.println(message); 
    } 
} 
+0

미안 OSGI를 처음 사용했습니다. 더 자세히 설명해 줄 수 있니? –

+0

예제 GitHub 프로젝트를 만들었습니다 : https://github.com/paulbakker/configuration-example –