Auto reload configuration when any change happen : part 2

This article solves a very common problem of configuration reloading on run time, which is usually required in most of the today’s applications.

In auto reload configuration using WatchService, we saw the use of JDK 7’s new API WatchService. In this post, i will use a rather easy approach i.e. apache commons configuration.

.Using commons configuration is as easy as writing two lines of code and that’s it.

Let jump into code directly:

package corejava.reloadTest;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;

public class MyApplicationProperties
{
	private static PropertiesConfiguration configuration = null;

	static
	{
		try {
			configuration = new PropertiesConfiguration("test.properties");
			configuration.setReloadingStrategy(new FileChangedReloadingStrategy());
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
	}

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

Above code loads “test.properties” file from class path and add FileChangedReloadingStrategy instance to it. This strategy enforces that every time you read the configuration, and if configuration has been updated since last read, in first step configuration will be reloaded and then updated value will be returned.

The decision that configuration has been updated, is taken on the basis of last configuration read timestamp and current timestamp. If both timestamps differ then surely configuration in cache is stale and need to be refreshed.

Let test our code:

package corejava.reloadTest;

public class TestReloading {
public static void main(String[] args)
{
	while(true)
	{
		try
		{
			Thread.sleep(2000);
		}
		catch (InterruptedException e)
		{
			e.printStackTrace();
		}
		System.out.println(MyApplicationProperties.getProperty("key"));
	}
}
}

When you run this code and update the test.properties file, you get output something like this:

value
value
value
Oct 19, 2012 11:33:06 PM org.apache.commons.configuration.AbstractFileConfiguration reload
INFO: Reloading configuration. URL is file://eclipse_juno_workspace/corejavaTest/test.properties
updatedValue
updatedValue

Looks good. Isn’t it??

Note: Please remember reloading in this way is effective, only if you are reading configuration frequently. But if you are reading configuration once on application start up and store it in cache, then you need to read my previous post about reloading using watch service.

Happy Learning !!

Ads by Google

One thought on “Auto reload configuration when any change happen : part 2”

  1. Hello Lokesh,
    What if I do propsconfig.configure(pathToProps) every time I log some thing ? Does it have any sideeffects? Like Appender closing and never reopen etc?

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Want to ask any question? Or suggest anything?