javaapache-commons-confignetflix-archaius

Property change listener for Archaius


I'm using Archaius to keep system properties aligned between all the servers hosting my app. I can see all the property getters can take a callback to be called whenever that property changes (for instance see the third argument in getStringProperty()) but I want to listen for any property change. Is there some interface I can implement and register as a listener somewhere?


Solution

  • I did not find an answer in any of the Archaius How-to documentation, but Apache Commons Configuration does provide a solution in their Configuration Events documentation (which Archaius is fully compatible with).

    I created a listener by implementing org.apache.commons.configuration.event.ConfigurationListener, which defines a single method called configurationChanged() and takes a single ConfigurationEvent parameter. This method is called once before and once after the property actually changes, with a beforeUpdate boolean on the event parameter indicating the timing for each call.

    Once I created the listener, I registered it with my Archaius configuration manager:

    ConfigurationManager.getConfigInstance().addConfigurationListener(new MyCustomConfigurationListener());
    

    Now I can see MyCustomConfigurationListener.configurationChanged() is called twice (once before and once after) on every property change.