Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Managing Configurations with Apache Commons Configuration

DZone's Guide to

Managing Configurations with Apache Commons Configuration

· Integration Zone
Free Resource

Modernize your application architectures with microservices and APIs with best practices from this free virtual summit series. Brought to you in partnership with CA Technologies.

When 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"));
}

The Integration Zone is proudly sponsored by CA Technologies. Learn from expert microservices and API presentations at the Modernizing Application Architectures Virtual Summit Series.

Topics:

Published at DZone with permission of Faheem Sohail, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}