Reloading configuration files in Java without stopping the server

Q1. Why & when do you want to use properties files for the project configuration as opposed to Java constants?
A1. It is a best practice to externalize application related properties that are likely to change. If you hard code the properties that are likely to change, then you need to recompile, package, and re-deploy the application when a property changes. If you put it in a configuration file like XML or name/value pair in .properties/.yml file, then when a property changes, you need to STOP the server, copy the modified configuration file to its location, and then START the server.

Q2. Why do you want to auto reload configuration files(s) when any changes happen?
A2. This prevents any server down-time as the server does not have to be stopped and started. The modified file can be copied to its location and the changes can be reloaded by the application.

Q3. How do you auto reload a configuration file in Java?
A3. The Apache commons-configuration library simplifies this task.

Step 1: Add commons configuration library dependency in your pom.xml file.

Step 2: Sample Java/Spring code that use the library classes to reload the properties file. The key class is the “FileChangedReloadingStrategy”.

Q4. Is there any other alternative approaches to auto load configuration files in Java?
A4. Yes. The “WatchService” that was introduced in Java 7. It has two methods take() and poll(). The take() method wait for next change to happen. This is a blocking method. The poll() method does not block, and its task is to check for event changes. This approach is handy for applications requiring customised logic for auto reload. The Apache configuration library is much more easier to implement as explained earlier for standard auto load of configuration on any property change.


🔥 300+ Java Interview FAQs

Java & Big Data Tutorials

Top