java - 使用公共(public)配置自动重新加载属性文件不起作用

标签 java apache

我有一个经常更新的属性文件。为了自动重新加载文件,我使用了公共(public)配置,但更改没有立即反射(reflect)在属性文件中。只有重新启动服务器后我才能看到更改,这意味着自动重新加载不起作用。我已经包含了所有需要的 jar 文件。

PropertiesConfiguration property = null;
    try {
        property = new PropertiesConfiguration(PROPERTY_FILENAME);
        property.setReloadingStrategy(new FileChangedReloadingStrategy());

    } catch (ConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

最佳答案

您必须提供文件的完整路径,否则它将无法检测到任何更改。如果该文件存在于您的类路径中并且您仅提供文件名,则会加载该文件,但不会检测到任何更改;您甚至可以删除该文件,应用程序将继续运行,就像什么都没发生一样。

  • 不检测更改。删除文件而不停止应用程序

    public class PropStandaloneConfigurationTest {
      public static void main(String[] args) throws ConfigurationException, InterruptedException {
        final PropertiesConfiguration config = new PropertiesConfiguration("app-project.properties");
        config.setListDelimiter(',');
        config.setAutoSave(true);
        FileChangedReloadingStrategy reload = new FileChangedReloadingStrategy();
        reload.setRefreshDelay(2000L);
        config.setReloadingStrategy(reload);
    
        Provider<Integer> cacheRetriesProvider = new Provider<Integer>() {
          @Override
          public Integer get() {
            return config.getInt("cache.retries");
          }
        };
    
        while (true) {
          System.out.println(cacheRetriesProvider.get());
          Thread.sleep(3000L);
        }
      }
    }
    
  • 检测更改并重新加载文件

        ... 
        final PropertiesConfiguration config = new PropertiesConfiguration("/usr/local/ws/app-project/src/test/resources/app-project.properties");
        ...
    

关于java - 使用公共(public)配置自动重新加载属性文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28870379/

相关文章:

java - 生成不带 namespace 前缀的 x509 证书 android (ds :)

apache - Apache Helix、Zookeeper、Mesos 和 Erlang 的真实示例?

java - Jaas 基本身份验证 tomcat 7

java - 如何使用 Java API 执行 AQL 查询?

java - java中在另一个类的构造函数中调用一个类

java - 有什么方法可以从文件系统获取文件 CRC?

apache - 探索 Hadoop 代码

apache - 带有白屏和消息 "redirecting to..."的 laravel 重定向中断帖子

java - 如何将 javascript 添加到我的 InDesign Server Java Web 应用程序?

java - 当调用 writeToDirectory 并且目标文件夹中存在同名文件时,Mule 入站端点会保留记录错误