config - 在 Apache Commons Configuration2 中检测文件重新加载

标签 config apache-commons apache-commons-config

使用 org.apache.commons:commons-configuration2:2.1我的应用程序需要知道何时重新加载配置文件,以便它可以更新程序中的某些内容。 Apache Commons Configuration2 使用了很多策略和工厂,通常这很好,但是这里变得如此复杂,我无法弄清楚我需要在哪里安装监听器。

该应用程序有这个:

configConfigurationBuilder = new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(
    PropertiesConfiguration.class)
        .configure(new Parameters().properties().setFile(getConfigFile()));
final PeriodicReloadingTrigger configReloadingTrigger = new PeriodicReloadingTrigger(
    configConfigurationBuilder.getReloadingController(), null, 1, TimeUnit.MINUTES);
configReloadingTrigger.start();

我可以在哪些不同的东西上安装监听器?我只想在重新加载配置文件时收到通知。

我注意到 ReloadingDetector接口(interface)有 reloadingPerformed()方法,这听起来像我想要的。但是我如何添加自己的ReloadingDetector ?好像是 ReloadingController只留一个ReloadingDetector大约。当然我不必继承 ReloadingDetector并安装一个自定义的,我可以吗?我不想专门化重新加载检测,所以子类化不合适——我只想在发生某些事情时得到通知。此外,对我来说,我什至会在哪里挂上 ReloadingFileBasedConfigurationBuilder 并不明显。它使用一些内部因素来创建检测器的事件链。

那么如何让 Apache Commons Configuration2 在重新加载配置时通知我呢?

最佳答案

我将此问题发布到 Apache Commons mailing list ,奥利弗·黑格回答说:

You can add an event listener at the ReloadingController, then you receive notifications of type ReloadingEvent.

Alternatively, you could also register a more generic listener for events on the configuration builder itself. When the builder's managed configuration is reset, a corresponding event is produced. This also happens during a reload operation.



起初我对这些事件是在检测到需要重新加载时还是在重新加载发生后发出的感到困惑,但他提醒我:

Maybe a misunderstanding of the reloading mechanism: New data is not automatically pushed into a Configuration object you might have a reference to. Rather, the configuration builder is reset. So the next time you query the builder for a configuration you get a new one with updated data.

When you receive one of the mentioned events the builder has been reset and thus you know that new data is available.



这是一个有用的答案,我正在分享以帮助他人。 (我希望 Apache Commons 开发人员能够监控并积极参与 Stack Overflow。)

关于config - 在 Apache Commons Configuration2 中检测文件重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917757/

相关文章:

perl - 映射硬编码配置文件

java - apache commons smtp 更改/删除 Message-Id header

java - 寻找 RandomStringUtils 的简洁替代品

JBoss 6 : what's the difference between the directories common/lib and client?

java - 是否可以在运行时提供新的 PropertiesConfiguration 文件?

java - apache DatabaseConfiguration 中的动态重新加载

django - 使用 SSL (https) 的网络服务的 Nginx 设置

sas - 有没有办法根据运行的程序动态指定配置规范?

java - 如何使用 apache.commons 获取属性列表

java - 如何使用 java 将 prop.getProperty() 返回的字符串转换为整数