For properties expected to change at runtime, use the Property API instead of calling Config directly. Property objects optimize the caching of resolved values from the configuration hierarchy, making them much more efficient for frequent access.
1. Create a Property Factory
Use DefaultPropertyFactory.from(config) to create a factory based on your existing configuration.
2. Create a Property
Use the factory to define a typed property with a default value.
3. Access the Value
Call .get() on the property object to retrieve the most recent cached value.
4. React to Changes
Register a com.netflix.archaius.api.PropertyListener to execute logic whenever a property value changes.
// Create a fast property factory using any Config as the source
DefaultPropertyFactory factory = DefaultPropertyFactory.from(config);
// Create a Property<Integer> object
Property<Integer> timeout = factory.getProperty("server.timeout").asInteger(DEFAULT_TIMEOUT_VALUE);
// Access the cached property value
Thread.sleep(timeout.get());
// React to property change notification
Property<Integer> timeout = factory
.getProperty("server.timeout")
.asInteger()
.addListener(new PropertyListener<Integer>() {
public void onChange(Integer value) {
socket.setReadTimeout(value);
}
public void onError(Throwable error) {
}
});