Java FileChangedReloadingStrategy Example

This article solves a very common problem of refresh .properties file on runtime on every file change using FileChangedReloadingStrategy, 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.

File watcher using FileChangedReloadingStrategy

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 classpath and add FileChangedReloadingStrategy instance to it. This strategy enforces that every time you read the configuration, and if the configuration has been updated since the last read, in first step configuration will be reloaded and then the updated value will be returned.

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

Verify FileChangedReloadingStrategy

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??

Please remember reloading using FileChangedReloadingStrategy is effective, only if you are reading configuration frequently. But if you are reading configuration once on application startup and store it in the cache, then you need to read my previous post about reloading using watch service.

In this tutorial, we learn about apache commons configuration reloading strategy using FileChangedReloadingStrategy class. It helps in reload properties file without restarting server in Java.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

4 thoughts on “Java FileChangedReloadingStrategy Example”

  1. Hi Lokesh,

    I am working on password encryption in property and config files using jasypt jar along with apache commons 1.2 but i am getting “Exception in thread “main” java.lang.NoClassDefFoundError: org.apache.commons.logging.impl.NoOpLog” Exception i tried all solution which are mentioned in website but no use please can you help me ? and tried above example but same error 🙁

    Reply
      • Hi Lokesh,

        Yes i had issue with the Jar now i am able to solve it .But reading file happening from class path but if i moved to resource folder i am not able to read it and getting “Cannot locate configuration source” error.Can you please tell me how can i set custom path in my code using apache.commons.configuration jars.

        Code like this :
        // Apache Commons Configuration
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.setFileName(propertyFileName);
        config.load();
        System.out.println(“Checking File …”);

        Reply
  2. 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?

    Reply

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.