使用commons配置2,我希望在特定的基於文件的屬性更改時收到通知。爲此,我正在使用ReloadingFileBasedConfigurationBuilder,PeriodicReloadingTrigger。apache commons configuration2:ConfigurationEvent沒有爲ReloadingFileBasedConfigurationBuilder生成
根據文檔,構建器應該用作中央組件,並在基礎文件更改時通過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"));
}
}
}