RSS Feed for This PostCurrent Article

Java - Auto Reloading of Configuration File

Download Source Code

This is a follow up to my previous article, XML Configuration for Java Program. In the article I uses Apache Commons Configuration for my application configuration, and I also provided a singleton wrapper class for it.

Now come the question, what if the configuration file is changed and the application needs to reload the information ?

To do this using Apache Commons Configuration is very straightforward. You only need to specify the ReloadingStrategy that you want to use for the file.

In my Java class, I added extra few lines of code to achieve this.

private AppConfiguration(String fileName) {
FileChangedReloadingStrategy strategy = new FileChangedReloadingStrategy();
// You can set the refresh relay, default to 5 seconds

In this context, I uses FileChangedReloadingStrategy to detect the file change.

A reloading strategy will reload the configuration every time its underlying file is changed.

This reloading strategy does not actively monitor a configuration file, but is triggered by its associated configuration whenever properties are accessed. It then checks the configuration file’s last modification date and causes a reload if this has changed.

To avoid permanent disc access on successive property lookups a refresh delay can be specified. This has the effect that the configuration file’s last modification date is only checked once in this delay period. The default value for this refresh delay is 5 seconds.

This strategy only works with FileConfiguration instances.

To test the code, I first read the information out from the file, then sleep for 30 seconds before I read it again.

public static void main(String args[]) {
AppConfiguration appConfig = AppConfiguration.getInstance();


// Sleep for 30 seconds, modify the config file during this time
try {
} catch (InterruptedException e) {



Simple, isn’t it !! :)

Trackback URL

RSS Feed for This Post2 Comment(s)

  1. Anonymous | Oct 7, 2007 | Reply

    Not so simple. The problem of concurrent access to the file is not solved.

  2. Al | Oct 12, 2007 | Reply

    imho, concurrent access to configuration files is probably wrong - you don’t want more than the administrator changing the config!

1 Trackback(s)

  1. From Develop a Java Plugin Framework | | Oct 7, 2007

RSS Feed for This PostPost a Comment