3

commons configuration2를 사용하여 특정 파일 기반 속성이 변경되면 알림을 받으려합니다. 그 때문에 ReloadingFileBasedConfigurationBuilder, PeriodicReloadingTrigger를 사용하고 있습니다.아파치 commons configuration2 : ReloadingFileBasedConfigurationBuilder에 대한 ConfigurationEvent가 생성되지 않았습니다

문서에 따라 기본 파일이 변경 될 때 builder를 중앙 구성 요소로 사용하고 builder.getConfiguration()을 사용하여 구성을 재생성해야합니다. 파일이 변경되면 ConfigurationBuilderEvent.RESET 알림을 받고 새 구성으로 구성을 새로 고칠 수 있습니다.

그러나 ConfigurationEvent.ANY에 대한 이벤트 수신기를 추가하려고하면 변경된 파일의 실제 속성에 대한 알림을 받지만 알림을받지 못합니다. 어떤 도움을 주셔서 감사합니다.

import java.io.File; 
import java.util.concurrent.TimeUnit; 

import org.apache.commons.configuration2.PropertiesConfiguration; 
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent; 
import org.apache.commons.configuration2.builder.EventListenerParameters; 
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder; 
import org.apache.commons.configuration2.builder.fluent.Parameters; 
import org.apache.commons.configuration2.event.ConfigurationEvent; 
import org.apache.commons.configuration2.event.EventListener; 
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger; 

public class ReloadingConfigEventTest { 
    public static void main(String[] args) throws Exception { 
     Parameters params = new Parameters(); 
     EventListenerParameters listenerParams = new EventListenerParameters(); 
     listenerParams.addEventListener(ConfigurationEvent.ANY, new EventListener<ConfigurationEvent>() { 
      public void onEvent(ConfigurationEvent event) { 
       System.out.println(event.getEventType().getName() +" "+event.getPropertyName()); 
      } 
     }).addEventListener(ConfigurationBuilderEvent.RESET, new EventListener<ConfigurationBuilderEvent>() { 
      public void onEvent(ConfigurationBuilderEvent event) { 
       System.out.println("Event:" + event.getEventType().getName()); 
      } 
     }); 

     ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder = new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(
       PropertiesConfiguration.class) 
         .configure(params.fileBased().setFile(new File("src/main/resource/override.conf")), listenerParams); 
     PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(), null, 1, 
       TimeUnit.SECONDS); 
     trigger.start(); 

     //modify the property file during the infinite loop, the new property is picked, but the SET_PROPERTY notification is missing 
     while (true) { 
      Thread.sleep(1000); 
      System.out.println(builder.getConfiguration().getString("test.property1")); 
     } 
    } 
} 

답변

0

불행하게도, 나는 적어도 최신 commons-configuration2 라이브러리 소스 (2.1.1)와이 가능 믿지 않는다 :

다음은이 보여 내 샘플 프로그램입니다. ReloadingFileBasedConfigurationBuilder은 (예를 들어, PropertiesConfiguration)을 FileBasedConfiguration에서 파생 구성 인스턴스를 채우고 FileBasedConfiguration 객체는 모두 등 ADD_PROPERTY, SET_PROPERTY 같은 이벤트를 해고 사용 내부 방법없이 의해로드

내가 관심을했습니다 이 자기 자신은 과거에, 그러나 commons-configuration2 그냥 이런 식으로 설정되지 않습니다.