How to implement an auto reloadable XML using Apache VFS

XML is usually used to store light configuration that controls the business flow or the layout of an application, it’s meant to be managed by business users in the production phase due to its simple human-readable syntax.

A typical example is to store the default theme/layout of the application, control the accepted currencies of a financial application, turning on/off features of an application.

Since these configurations are prone to regular updates, it’s not practical to restart the application on each configuration change.

In this tutorial, we describe how to implement an auto reloadable XML using Apache VFS in Java.

1- app.xml

Suppose we have the following app.xml file which holds the business and layout configuration for our financial application:

2- pom.xml

In order to use Apache VFS, we add the following dependencies to pom.xml:

3- AppConfiguration.java

Create a POJO class named as AppConfiguration.java which maps the attributes of app.xml:

4- XMLConfigurationManager.java

Now, this is our configuration processor, we’ll create a class which loads the configuration at the application startup and listens for any runtime change on the XML file.

Our manager mainly acts as a cache and may be defined as a Singleton which populates AppConfiguration bean in the constructor and repopulates it on each XML change. Whenever we need to read the configuration from other classes or modules, we get an instance of the Cache and use AppConfiguration, hence any XML change will be reflected instantly on the application without the need for a restart.

Below is the implementation of XMLConfigurationManager:

As we notice, we read the configuration in the constructor and attach an event listener to the XML file using DefaultFileMonitor and FileListener classes provided by Apache VFS. On each change event, we call loadConfiguration() to repopulate the AppConfiguration bean.

Through this way, the latest version of XML would always be exposed to the application through XMLConfigurationManager and AppConfiguration bean.

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Liam Recent comment authors
newest oldest most voted
Liam
Guest
Liam

It’s not practical to restart the application on each configuration change.
http://rollingskygame.com/run-3