I have found a good utility class which is part of the apache configuration jar to reload the property or resource file without restarting the JVM or Application Server. FileChangedReloadingStrategy class can be used to reload the property/resource file at runtime if there is any modification is done in the property file.
In the below given example I have used the FileChangedReloadingStrategy.
1 2 |
PropertiesConfiguration property = new PropertiesConfiguration("file"); property.setReloadingStrategy(new FileChangedReloadingStrategy()); |
You may set appropriate refresh delay to FileChangedReloadingStrategy to reduce the IO operation.
Four implementations for ReloadingStrategy are provided by the apache configuration API.
- FileChangedReloadingStrategy
- InvariantReloadingStrategy
- ManagedReloadingStrategy
- VFSFileChangedReloadingStrategy
You may also write your own implementation of property file reload strategy through giving an implementation for org.apache.commons.configuration.reloading.ReloadingStrategy interface.
Sample Program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
package com.ourownjava.corejava.file; import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy; /** * * @author ourownjava.com * @date August 17th, 2011 * * Reloading property file without restarting the JVM * */ public class ResourceLoadingExample { public static void main(String args[]) throws InterruptedException,ConfigurationException { //load the property file from file system final PropertiesConfiguration property = new PropertiesConfiguration("/home/thosan/java/testfiles/application.properties"); //set the reloading strategy property.setReloadingStrategy(new FileChangedReloadingStrategy()); //starting a thread to make changes in the property file, this is only to test final Thread thread = new PropertyFileChangerThread("/home/thosan/java/testfiles/application.properties"); thread.start(); //print the changes in file every 6 second, this is only for testing while (true) { final Iterator i = property.getKeys(); while (i.hasNext()) { System.out.println(i.next()); } Thread.sleep(6000); } } } class PropertyFileChangerThread extends Thread { private String fileName; PropertyFileChangerThread(String fileName) { this.fileName = fileName; } public void run() { try { while (true) { //open the property file on append mode and write key value pair in every 5 seconds. final FileWriter fileWriter = new FileWriter(fileName, true); fileWriter.write("\n"); fileWriter.write(Math.random() + "=" + Math.random()); fileWriter.flush(); Thread.sleep(5000); fileWriter.close(); } } catch (final IOException e) { e.printStackTrace(); } catch (final Exception e) { e.printStackTrace(); } } } |