在 Apache Commons Configuration2 中检测文件重新加载
Detecting file reload in Apache Commons Configuration2
使用 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
接口有一个 reloadingPerformed()
方法,这听起来像我想要的。但是如何添加我自己的 ReloadingDetector
? ReloadingController
似乎只保留了一个 ReloadingDetector
。当然,我不必继承 ReloadingDetector
并安装一个自定义的,对吗?我不想专注于重新加载检测,因此子类化不合适——我只想在发生某些事情时得到通知。此外,我什至不明白我什至会在 ReloadingFileBasedConfigurationBuilder
事件链中使用一些内部因素来创建检测器。
那么我怎样才能轻松地让 Apache Commons Configuration2 在重新加载配置时通知我?
我把这个问题发到了 Apache Commons mailing list,Oliver Heger 回复了:
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 上受到监控并保持活跃。)
使用 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
接口有一个 reloadingPerformed()
方法,这听起来像我想要的。但是如何添加我自己的 ReloadingDetector
? ReloadingController
似乎只保留了一个 ReloadingDetector
。当然,我不必继承 ReloadingDetector
并安装一个自定义的,对吗?我不想专注于重新加载检测,因此子类化不合适——我只想在发生某些事情时得到通知。此外,我什至不明白我什至会在 ReloadingFileBasedConfigurationBuilder
事件链中使用一些内部因素来创建检测器。
那么我怎样才能轻松地让 Apache Commons Configuration2 在重新加载配置时通知我?
我把这个问题发到了 Apache Commons mailing list,Oliver Heger 回复了:
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 上受到监控并保持活跃。)