Config Listeners
Configuration change events can be listened for with Config.addListener(ConfigEventListener).
When to use it
Use a listener to react when an entry changes, either through Config.commit() or because the file changed on the file system, for example to reload a cache.
Example
import org.apache.juneau.config.*;
import org.apache.juneau.config.event.*;
import org.apache.juneau.config.store.*;
public class ConfigListenerExample {
public static void main(String[] args) throws Exception {
MemoryStore store = MemoryStore.create().build();
store.update("MyConfig.cfg", "[MySection]\nkey1 = a\n");
Config config = Config.create("MyConfig.cfg").store(store).build();
// Add a listener for changes to MySection/key1
config.addListener(events -> {
for (ConfigEvent event : events) {
if (event.getType() == ConfigEventType.SET_ENTRY && "MySection".equals(event.getSection()) && "key1".equals(event.getKey()))
System.out.println("key1 changed to " + event.getValue());
}
});
config.set("MySection/key1", "b");
config.commit();
}
}
Options
The ConfigEventListener interface consists of the following method:
The ConfigEvent class provides access to all the information about the updated entry:
The listener method is triggered:
- After Config.commit() is called.
- When the file changes on the file system.
In both cases, the listener is triggered after the changes have been committed.