-
Notifications
You must be signed in to change notification settings - Fork 486
Getting Started
There are two ways to get Archaius working out of the box by 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. The file can be also in a jar file’s root directory.
- In addition,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.
If neither the default configuration file nor the URLs defined by the system property can be read, there won’t be any fatal error but you won’t be able to get real values of your properties, unless you register a configuration source with DynamicPropertyFactory (See Users Guide).
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.