Managing Configurations with Apache Commons Configuration
Using Apache Commons Configuration to configure long-running applications.
Join the DZone community and get the full member experience.
Join For FreeWhen building long running applications you are likely to run into a number of requirements related to configurations. Lets look at what those might be.
1. Managing multiple configuration sources
2. Ability to reload configurations when they change on disk
3. Ability to persist changes to configuration files
Instead of writing your own code it is a much better idea to use Apache Commons Configuration which provides this capability.
1. Managing multiple configuration sources
Commons Configuration provides a way for you to combine multiple configuration sources such as properties files, XML configurations, system properties and database tables. This allows your application to talk to a single interface for retrieving all sort of configurations. This is done by building a composite configuration.
In the following code snippet, we are combining configurations from a properties files as well as System properties. This of course could be extended to include XML based configurations, database table based configurations etc.
private void compositeConfigurations() throws ConfigurationException{ CompositeConfiguration config = new CompositeConfiguration(); config.addConfiguration(new SystemConfiguration()); config.addConfiguration(new PropertiesConfiguration("configurations.properties")); System.out.println(config.getString("configuration.first")); System.out.println(config.getString("java.home")); }
2. Ability to reload configurations automatically when file is changed
One obviously wants to avoid bouncing the server each time a configuration change is made therefore this is an extremely useful functionality. Properties are reloaded (based on modified date of the configuration file) whenever the file is changed on disk.
private void automaticReloadingOfConfigurations() throws Exception { PropertiesConfiguration configs = new PropertiesConfiguration("configurations.properties"); configs.setReloadingStrategy(new FileChangedReloadingStrategy()); System.out.println(configs.getString("configuration.first")); Thread.sleep(10000); //change file on disk System.out.println(configs.getString("configuration.first")); }
3. Ability to persist changes to the configurations
Another important functionality is to be able to persist changes to the configurations back to the source of the configuration. For example, if a configuration is read from a properties file and that configuration is changed, commons config allows you to persist it back to the file. This allows you to for example, build user interfaces that change configurations that can survive server restarts.
By calling .setAutoSave(true), you can configure autosaving that persists changes to the disk as soon as you make them. If you want to have more control, you can turn autosave off and explicitly save changes by calling the save method on the configuration object.
private void autoSaving() throws Exception { PropertiesConfiguration configs = new PropertiesConfiguration("configurations.properties"); configs.setAutoSave(true); System.out.println(configs.getString("configuration.first")); configs.setProperty("configuration.first", "1st"); System.out.println(configs.getString("configuration.first")); }
Published at DZone with permission of Faheem Sohail, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments