Prasanth Nair
Prasanth Nair

Reputation: 509

config changes without redeployment

My web application has several integrations with external systems and all these integration Rest URLs are kept in a config file with in web app. My application reads this config file at start up and use the URL values while making connections to external systems. But quite often it happens that one of the external systems is down and we have to use an alternate URL. In that case, we typically will have to modify the config and redeploy the war file. Is there a way to modify config file with new value without going through a redeployment of the war file?

Upvotes: 1

Views: 1979

Answers (1)

JosemyAB
JosemyAB

Reputation: 407

In my projects i usually work with Apache Commons Configuration for the management of config files (properties). This library has the capability of automatic reload the values when file changes.

This is muy suggestion of implementation:

Create a class "MyAppConfigProperties" for load the properties file and read your configuration keys:

public class MyAppConfig {

    //Apache Commons library object
    private PropertiesConfiguration configFile;

    private void init() {
        try {
            //Load the file            
            configFile = new PropertiesConfiguration(
                    MyAppConfig.class.getClassLoader().getResource("configFile.properties"));

            // Create refresh strategy with "FileChangedReloadingStrategy"
            FileChangedReloadingStrategy fileChangedReloadingStrategy = new FileChangedReloadingStrategy();
            fileChangedReloadingStrategy.setRefreshDelay(1000);
            configFile.setReloadingStrategy(fileChangedReloadingStrategy);

        } catch (ConfigurationException e) {
            //Manage the exception
        }
    }

    /**
     * Constructor por defecto.
     */
    public MyAppConfig() {
        super();
        init();
    }

    public String getKey(final String key) {

        try {
            if (configFile.containsKey(key)) {
                return configFile.getString(key);
            } else {
                return null;
            }

        } catch (ConversionException e) {
            //Manage Exception
        }
    }
}

Now you have to construct a instance of this class (singleton) and use it in all places in that you need to reed a config key.

Every time you use the method "getKey" you will get the last value of the key without deploy and restart.

Upvotes: 1

Related Questions