1

can anyone guide me on how to perform a reload of an apache commons configuration2 properties. I'm unable to find any implementation of this anywhere. The apache docs are a bit too abstract. This is what I have so far but it's not working.

    CombinedConfiguration cc = new CombinedConfiguration();

    Parameters params = new Parameters();
    File configFile = new File("config.properties");
    File emsFile = new File("anotherconfig.properties");

    ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration> configBuilder =
        new ReloadingFileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
        .configure(params.fileBased()
            .setFile(configFile));
    PeriodicReloadingTrigger reloadTrg = new PeriodicReloadingTrigger(configBuilder.getReloadingController(), null, 5, TimeUnit.SECONDS);
    reloadTrg.start();

    cc.addConfiguration(configBuilder.getConfiguration());

    FileBasedConfigurationBuilder<FileBasedConfiguration> emsBuilder =
            new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
            .configure(params.properties()
                .setFile(emsFile));
    cc.addConfiguration(emsBuilder.getConfiguration());

    DataSource ds = EmsDataSource.getInstance().getDatasource(this);

     BasicConfigurationBuilder<DatabaseConfiguration> dbBuilder =
         new BasicConfigurationBuilder<DatabaseConfiguration>(DatabaseConfiguration.class);
     dbBuilder.configure(
         params.database()
             .setDataSource(ds)
             .setTable("EMS_CONFIG")
             .setKeyColumn("KEY")
             .setValueColumn("VALUE")
     );
    cc.addConfiguration(dbBuilder.getConfiguration());
Ben Isaac
  • 69
  • 1
  • 10

2 Answers2

1

The configuration obtained from a builder is not updated automatically. You need to get the configuration from the builder every time you read it.

From Automatic Reloading of Configuration Sources:

One important point to keep in mind when using this approach to reloading is that reloads are only functional if the builder is used as central component for accessing configuration data. The configuration instance obtained from the builder will not change automagically! So if an application fetches a configuration object from the builder at startup and then uses it throughout its life time, changes on the external configuration file become never visible. The correct approach is to keep a reference to the builder centrally and obtain the configuration from there every time configuration data is needed.

Jason Braucht
  • 2,358
  • 19
  • 31
R. Herger
  • 26
  • 4
-1

use following code:

@Component
public class ApplicationProperties {
    private PropertiesConfiguration configuration;

    @PostConstruct
    private void init() {
        try {
            String filePath = PropertiesConstants.PROPERTIES_FILE_PATH;
            System.out.println("Loading the properties file: " + filePath);
            configuration = new PropertiesConfiguration(filePath);

            //Create new FileChangedReloadingStrategy to reload the properties file based on the given time interval
            FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
           fileChangedReloadingStrategy.setRefreshDelay(PropertiesConstants.REFRESH_DELAY);
            configuration.setReloadingStrategy(fileChangedReloadingStrategy);
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }

    public String getProperty(String key) {
        return (String) configuration.getProperty(key);
    }

    public void setProperty(String key, Object value) {
        configuration.setProperty(key, value);
    }

    public void save() {
        try {
            configuration.save();
        } catch (ConfigurationException e) {
            e.printStackTrace();
        }
    }
}
M2E67
  • 937
  • 7
  • 23