Commons Configuration2 ReloadingFileBasedConfiguration -


i trying implement apache configuration 2 in codebase

import 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 script changes 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 sample code using check whether automatic reloading works or not. when underlying property file updated, configuration doesn't reflect it.

as per documentation :

one important point keep in mind when using approach reloading reloads functional if builder used central component accessing configuration data. configuration instance obtained builder not change automagically! if application fetches configuration object builder @ startup , uses throughout life time, changes on external configuration file become never visible. correct approach keep reference builder centrally , obtain configuration there every time configuration data needed.

https://commons.apache.org/proper/commons-configuration/userguide/howto_reloading.html#reloading_file-based_configurations

this different old implementation was.

i able execute sample code making 2 changes :

  1. make builder available globally , access configuration builder :

    system.out.println(builder.getconfiguration().getstring("property1"));

  2. add listener builder : `builder.addeventlistener(configurationbuilderevent.any, new eventlistener() {

        public void onevent(configurationbuilderevent event) {         system.out.println("event:" + event);     } }); 

posting sample program, able demonstrate

import 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.event.eventlistener; import org.apache.commons.configuration2.reloading.periodicreloadingtrigger;  public class testdynamicprops {     public static void main(string[] args) throws exception {          parameters params = new parameters();         reloadingfilebasedconfigurationbuilder<propertiesconfiguration> builder =             new reloadingfilebasedconfigurationbuilder<propertiesconfiguration>(propertiesconfiguration.class)             .configure(params.filebased()                 .setfile(new file("src/main/resources/override.properties")));         periodicreloadingtrigger trigger = new periodicreloadingtrigger(builder.getreloadingcontroller(),             null, 1, timeunit.seconds);         trigger.start();          builder.addeventlistener(configurationbuilderevent.any, new eventlistener<configurationbuilderevent>() {              public void onevent(configurationbuilderevent event) {                 system.out.println("event:" + event);             }         });          while (true) {             thread.sleep(1000);             system.out.println(builder.getconfiguration().getstring("property1"));         }      } } 

Comments

Popular posts from this blog

java - nested exception is org.hibernate.exception.SQLGrammarException: could not extract ResultSet Hibernate+SpringMVC -

sql - Postgresql tables exists, but getting "relation does not exist" when querying -

asp.net mvc - breakpoint on javascript in CSHTML? -