-
Notifications
You must be signed in to change notification settings - Fork 484
Users Guide
There are two ways to get Archaius working out of the box using a local configuration file to feed dynamic properties to your application.
- By default, Archaius will look for a file named “config.properties” on the application’s classpath and read its content as configuration properties
- You can define a system property “configurationSource.additionalUrls” that contains the URL path to a local configuration file. For example, add this to your application start up script
-DconfigurationSource.additionalUrls=file:///apps/myapp/application.properties
Assume there is a property “lock.waitTime” in the configuration file and can be changed dynamically at runtime. Here is the code you can use in your application to utilize the dynamic nature of this property with Archaius:
// create a property whose value is type long and use 1000 as the default
// if the property is not defined
DynamicLongProperty timeToWait =
DynamicPropertyFactory.getInstance().createLongProperty("lock.waitTime", 1000);
// ...
ReentrantLock lock = ...;
// ...
lock.tryLock(timeToWait.get(), TimeUnit.MILLISECONDS); // timeToWait.get() returns up-to-date value of the property
There are two things to note in the above code:
- timeToWait is a property that is bound to a long value. There is no code needed to parse string to long.
- In the call to lock.tryLock(), instead of using a predefined long parameter, we used timeToWait.get() to get the value that could be potentially changed at runtime
Now if you need to change the value of “lock.waitTime”, simply edit the configuration file and change the value of the property. By default, Archaius will read the file every minute and this change will be effective within a minute in your application.
You can define multiple URLs delimited by comma “,” for the system property “configurationSource.additionalUrls”, in addition to the default “config.properties” file on classpath. Archaius will read “config.properties” first, then all the other URLs defined in the system property in the order it is defined. If there are two URLs that contains the same property, the final value will be from the one that gets read later.
For example, you can deploy your application with set of default properties in file config.properties and provide another set of properties as overrides in another URL.
Assume you define this in config.properties
lock.waitTime=200
and use this http URL to provide overriding properties
-DconfigurationSource.additionalUrls=http://myserver/properties
Also the content of http://myserver/properties contains this line
lock.waitTime=500
Using the same code in the previous example, the final value of lock.waitTime in your application will be 500 instead of 200. This use case is particularly helpful if your application is running in a cluster. By defining an additional configuration URL as a centralized HTTP URL, you can avoid the hassle of changing the same configuration file on each server of the cluster.
Here is the set of system properties that you can change
Name | Description | Default value |
configurationSource.defaultFileName | the default configuration file name on classpath | config.properties |
fixedDelayPollingScheduler.initialDelayMills | Initial delay in milliseconds of reading from the configuration source | 30000 |
fixedDelayPollingScheduler.delayMills | Fixed delay in milliseconds between two reads of the configuration URLs | 60000 |
Archaius uses SLF4J (http://www.slf4j.org/) for logging. SLF4J is a facade over logging that allows you to plug in any (or no) logging framework. See the SLF4J website for details.
As explained in previous section, by default Archaius uses a set of URLs as configuration source and poll them in a fixed delay. However, you can also provide your own configuration source and/or polling scheduler. For example, you may define your own configuration source from a relational database, a distributed key-value store like Cassandra, or third party service as AWS SimpleDB.
To provide your own configuration source, follow these steps:
public class DBConfigurationSource implements PolledConfigurationSource {
// ...
@Override
public PollResult poll(boolean initial, Object checkPoint)
throws Exception {
// implement logic to retrieve properties from DB
}
}
public class MyScheduler extends AbstractPollingScheduler {
// ...
@Override
protected synchronized void schedule(Runnable runnable) {
// schedule the runnable
}
@Override
public void stop() {
// stop the scheduler
}
}
PolledConfigurationSource source = ...
AbstractPollingScheduler scheduler = ...
DynamicConfiguration configuration = new DynamicConfiguration(source, scheduler);
DynamicPropertyFactory.initWithConfigurationSource(configuration);
DynamicStringProperty myprop = DynamicPropertyFactory.getInstance().createStringProperty(...);
If you already use or extend any type of AbstractConfiguration from Apache Commons Configuration, but would like to take advantage of Archaius to feed your application with dynamic properties, you can do this in two ways:
1. Use an implementation of com.netflix.config.AbstractPollingScheduler
to poll the dynamic configuration source and put them into your own Configuration
AbstractConfiguration myConfiguration = ...; // this is your original configuration
// ...
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(); // or use your own scheduler
PolledConfigurationSource source = new URLConfigurationSource(); // or use your own source
scheduler.setIgnoreDeletesFromSource(true); // don't treat properties absent from the source as deletes
scheduler.startPolling(source, myConfiguration);
// ...
DynamicPropertyFactory.initWithConfigurationSource(myConfiguration);
Now the original configuration becomes dynamic at runtime as properties from the polled configuration source will override their values in the original configuration. Note: In the above example, myConfiguration
will be accessed from different threads and updates may not be visible immediately depending how the configuration is implemented. You can utilize com.netflix.config.ConcurrentMapConfiguration
to ensure visibility of updates.
AbstractConfiguration myConfiguration = ...; // this is your original configuration
// create the dynamic configuration
AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler(); // or use your own scheduler
PolledConfigurationSource source = new URLConfigurationSource(); // or use your own source
DynamicConfiguration dynamicConfig = new DynamicConfiguration(source, scheduler);
ConcurrentCompositeConfiguration finalConfig = new ConcurrentCompositeConfiguration();
// add them in this order to make dynamicConfig override myConfiguration
finalConfig.add(dynamicConfig);
finalConfig.add(myConfiguration);
DynamicPropertyFactory.initWithConfigurationSource(finalConfig);
The later approach gives you the flexibility to add more types configurations down the road.