dynamic - apache 公共(public)配置 2 :ConfigurationEvent is not generated for ReloadingFileBasedConfigurationBuilder

标签 dynamic configuration-management apache-commons-config

使用 commons configuration2,我希望在基于特定文件的属性更改时收到通知。为此,我正在使用 ReloadingFileBasedConfigurationBuilder、PeriodicReloadingTrigger。

根据文档,构建器应用作中央组件,并在基础文件更改时通过 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"));
        }
    }
}

最佳答案

不幸的是,我不相信这是可能的,至少对于最新的 commons-configuration2 库源 (2.1.1)。 ReloadingFileBasedConfigurationBuilder 仅填充派生自 FileBasedConfiguration 的配置实例(例如,PropertiesConfiguration),FileBasedConfiguration 对象都是在不使用内部方法的情况下加载,这些方法会触发 ADD_PROPERTYSET_PROPERTY 等事件

过去我自己对此很感兴趣,但是 commons-configuration2 并不是这样设置的。

关于dynamic - apache 公共(public)配置 2 :ConfigurationEvent is not generated for ReloadingFileBasedConfigurationBuilder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40824538/

相关文章:

c - 如何在 Visual Studio 2005 中释放动态分配的内存

gcc 6.2.0 试图创建共享对象,但它不应该创建?

drupal - 如何在 Drupal 8 功能测试中导入配置同步文件?

java - 解析自定义 xml 配置文件

c# - 在 WPF 中以编程方式创建图像按钮

sql - sql server中具有多列的动态数据透视表

maven - 为整个团队共享相同的 settings.xml maven 配置

java - 在java中将文件导入到zookeeper

java - 使用 Commons Configuration 在数据库中存储属性

java - 为什么代码不更改 .properties 文件