簡體   English   中英

使用Commons配置自動重新加載屬性文件不起作用

[英]Auto reloading properties file using commons configuration is not working

我有一個經常更新的屬性文件。 為了自動重新加載文件,我使用了commons配置,但是更改沒有立即反映在屬性文件中。 我只有在重新啟動服務器后才能看到更改,這意味着自動重新加載無法正常工作。 我已經包含了所有需要的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"); ... 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM