You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Avin <av...@yahoo.com.INVALID> on 2016/04/20 05:10:59 UTC

Composite Configuration with ReloadingFileBasedConfiguration

I am trying to implement the Apache Configuration 2 in my codebaseimport java.io.File;
import java.util.concurrent.TimeUnit;

import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
import org.apache.commons.configuration2.event.EventListener;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;

import org.apache.commons.configuration2.CompositeConfiguration;

public class Test {

    private static final long DELAY_MILLIS = 10 * 60 * 5;


    public static void main(String[] args) {
        // TODO Auto-generated method stub
        CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
        PropertiesConfiguration props = null;
        try {
            props = initPropertiesConfiguration(new File("/tmp/DEV.properties"));
        } catch (ConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        compositeConfiguration.addConfiguration( props );
        compositeConfiguration.addEventListener(ConfigurationBuilderEvent.ANY,
                new EventListener<ConfigurationBuilderEvent>()
                {
                    @Override
                    public void onEvent(ConfigurationBuilderEvent event)
                    {
                        System.out.println("Event:" + event);

                    }
                });

        System.out.println(compositeConfiguration.getString("property1"));

        try {
            Thread.sleep(14*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // Have a script which changes the value of property1 in DEV.properties
        System.out.println(compositeConfiguration.getString("property1"));
    }

    protected static PropertiesConfiguration initPropertiesConfiguration(File propsFile) throws ConfigurationException { 

        if(propsFile.exists()) {

            final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
                    new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
                    .configure(new Parameters().fileBased()
                        .setFile(propsFile)
                        .setReloadingRefreshDelay(DELAY_MILLIS)
                        .setThrowExceptionOnMissing(false)
                        .setListDelimiterHandler(new DefaultListDelimiterHandler(';')));
            final PropertiesConfiguration propsConfiguration = builder.getConfiguration();
            PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
                    null, 1, TimeUnit.SECONDS);
                trigger.start();

            return propsConfiguration;
        } else {
            return new PropertiesConfiguration();
        }

    }
}
Here is a sample code that I using to check whether the Automatic Reloading works or not. However when the underlying property file is updated, the configuration doesn't reflect it.Once the DEV.properties is updated, it doesn't get reflected when the compositeConfiguration.getString("property1") is called again. 

Re: Composite Configuration with ReloadingFileBasedConfiguration

Posted by Oliver Heger <ol...@oliver-heger.de>.
Hi Avin,

the reloading is only performed by the configuration builder. After you
have obtained a configuration from the builder, it will not change
automatically due to a reload operation. Citing from the user's guide [1]:

"One important point to keep in mind when using this approach to
reloading is that reloads are only functional if the builder is used as
central component for accessing configuration data. The configuration
instance obtained from the builder will not change automagically! So if
an application fetches a configuration object from the builder at
startup and then uses it throughout its life time, changes on the
external configuration file become never visible. The correct approach
is to keep a reference to the builder centrally and obtain the
configuration from there every time configuration data is needed."

This section of the user's guide also contains a working example. If you
want to combine multiple configuration sources with reloading support,
use CombinedConfiguration rather than CompositeConfiguration. How this
is done is described at [2].

HTH
Oliver

[1]
http://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html#Reloading_File-based_Configurations
[2]
http://commons.apache.org/proper/commons-configuration/userguide/howto_combinedbuilder.html#Reloading_Support

Am 20.04.2016 um 05:10 schrieb Avin:
> I am trying to implement the Apache Configuration 2 in my codebaseimport java.io.File;
> import java.util.concurrent.TimeUnit;
> 
> import org.apache.commons.configuration2.PropertiesConfiguration;
> import org.apache.commons.configuration2.builder.ConfigurationBuilderEvent;
> import org.apache.commons.configuration2.builder.ReloadingFileBasedConfigurationBuilder;
> import org.apache.commons.configuration2.builder.fluent.Parameters;
> import org.apache.commons.configuration2.convert.DefaultListDelimiterHandler;
> import org.apache.commons.configuration2.event.EventListener;
> import org.apache.commons.configuration2.ex.ConfigurationException;
> import org.apache.commons.configuration2.reloading.PeriodicReloadingTrigger;
> 
> import org.apache.commons.configuration2.CompositeConfiguration;
> 
> public class Test {
> 
>     private static final long DELAY_MILLIS = 10 * 60 * 5;
> 
> 
>     public static void main(String[] args) {
>         // TODO Auto-generated method stub
>         CompositeConfiguration compositeConfiguration = new CompositeConfiguration();
>         PropertiesConfiguration props = null;
>         try {
>             props = initPropertiesConfiguration(new File("/tmp/DEV.properties"));
>         } catch (ConfigurationException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         }
>         compositeConfiguration.addConfiguration( props );
>         compositeConfiguration.addEventListener(ConfigurationBuilderEvent.ANY,
>                 new EventListener<ConfigurationBuilderEvent>()
>                 {
>                     @Override
>                     public void onEvent(ConfigurationBuilderEvent event)
>                     {
>                         System.out.println("Event:" + event);
> 
>                     }
>                 });
> 
>         System.out.println(compositeConfiguration.getString("property1"));
> 
>         try {
>             Thread.sleep(14*1000);
>         } catch (InterruptedException e) {
>             // TODO Auto-generated catch block
>             e.printStackTrace();
>         }
> 
>         // Have a script which changes the value of property1 in DEV.properties
>         System.out.println(compositeConfiguration.getString("property1"));
>     }
> 
>     protected static PropertiesConfiguration initPropertiesConfiguration(File propsFile) throws ConfigurationException { 
> 
>         if(propsFile.exists()) {
> 
>             final ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration> builder =
>                     new ReloadingFileBasedConfigurationBuilder<PropertiesConfiguration>(PropertiesConfiguration.class)
>                     .configure(new Parameters().fileBased()
>                         .setFile(propsFile)
>                         .setReloadingRefreshDelay(DELAY_MILLIS)
>                         .setThrowExceptionOnMissing(false)
>                         .setListDelimiterHandler(new DefaultListDelimiterHandler(';')));
>             final PropertiesConfiguration propsConfiguration = builder.getConfiguration();
>             PeriodicReloadingTrigger trigger = new PeriodicReloadingTrigger(builder.getReloadingController(),
>                     null, 1, TimeUnit.SECONDS);
>                 trigger.start();
> 
>             return propsConfiguration;
>         } else {
>             return new PropertiesConfiguration();
>         }
> 
>     }
> }
> Here is a sample code that I using to check whether the Automatic Reloading works or not. However when the underlying property file is updated, the configuration doesn't reflect it.Once the DEV.properties is updated, it doesn't get reflected when the compositeConfiguration.getString("property1") is called again. 
> 

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org