Skip to content

Getting Started

Kirill Kulikov edited this page Apr 23, 2014 · 15 revisions

Getting Archaius

You can download Archaius binaries from Maven Central or use this URL http://search.maven.org/#search%7Cga%7C1%7Carchaius

To add Archaius to your maven dependency, use the following

 <dependency>
  <groupId>com.netflix.archaius</groupId>
  <artifactId>archaius-core</artifactId>
  <version>0.6.0</version>
 </dependency>

Using a local file as the configuration source

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 “archaius.configurationSource.additionalUrls” that contains the URL path to a local configuration file. For example, add this to your application start up script
-Darchaius.configurationSource.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().getLongProperty("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:

  1. timeToWait is a property that is bound to a long value. There is no code needed to parse string to long.
  2. 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.

Using multiple URLs as the configuration sources

You can define multiple URLs delimited by comma “,” for the system property “archaius.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

-Darchaius.configurationSource.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.

Change the default setting

Here is the set of system properties that you can change

Operation HTTP action Notes
archaius.configurationSource.defaultFileName the default configuration file name on classpath config.properties
archaius.fixedDelayPollingScheduler.initialDelayMills Initial delay in milliseconds of reading from the configuration source 30000
archaius.fixedDelayPollingScheduler.delayMills Fixed delay in milliseconds between two reads of the configuration URLs 60000

Logging

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.