Anusha Sajeendran
Anusha Sajeendran

Reputation: 181

Auto reloading properties file using commons configuration is not working

I have a properties file which I update often. To reload the file automatically I used commons configuration but the change is not getting reflected in the properties file immediately. I can see the changes only after restarting the server which means auto reload is not working. I have included all the needed jar files.

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

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

Upvotes: 1

Views: 3881

Answers (1)

Yoandy Terradas
Yoandy Terradas

Reputation: 153

You have provide the full path of the file, otherwise it will not detect any changes. if the file exists in your classpath and you provide just the file name, it will be loaded but no changes will be detected; you can even delete the file and the application will continue to run as if nothing happened.

  • Does not detect changes. Delete the file without stopping the application

    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);
        }
      }
    }
    
  • Detects changes and reloads the file

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

Upvotes: 2

Related Questions