This section describes how to use configuration manager when you have a need to have different configurations values for different environments. For example, you thread pool might be limited to 2 threads in development environment, 4 in QA and 10 in production.
To be able to support multiple environments configuration manager uses
substitution mechanism while processing configuration file (config.xml).
Any value in the config.xml file that is enclosed in '?' (like ?ENV?)
will be substituted with either value of same named runtime parameter passed when Java is started
or environment variable. For example, if you have environment variable
called ENV with values prod for production, qa for qa and dev for development,
you can use ?ENV?
to point to different path in configuration manager, as described below.
Create a configuration config.xml file and put it in a class path of your application Simple config.xml file below has log4j.xml file and property file that will depend on the environment.
config.xml
<?xml version="1.0" encoding="UTF-8" ?> <configurationManager key="Sample App1 Configuration" jmx="true"> <properties key="sampleAppProps" fileName="?ENV?/SampleAppProperties.properties" /> ... </configurationManager>
In the same directory where you have config.xml create a subdirectory called dev
.
Create the SampleAppProperties in that directory:
SampleAppProperties.properties
serverName=MyAppserver numThread=2
In the same directory where you have config.xml create a subdirectory called qa
.
Create the SampleAppProperties in that directory:
SampleAppProperties.properties
serverName=MyAppserver numThread=4
import org.naftulin.configmgr.ConfigurationUtility; .... int numThreads = ConfigurationUtility.getInteger("sampleAppProps", "numThread");
Define environment variable ENV.
In DOS command window type set ENV=dev
In csh on Unix type setenv ENV=dev
Right mouse-click on My Computer, propeties, Advance tab, Environment variables, and add a new variable name ENV value dev
That's it. While running your program in the on the machine where you defined environment variable, the value will be substituted in the configuration file. This way you will have different values on QA and Development.
In the example above, instead of unsing environment variable, you can use the
Java runtime parameter. To do that add the folowing to the command line:
java -DENV=dev ...
for development environment and
java -DENV=qa ...
for QA environment. Please note that runtime
parameters will have precedense over the environment variable values. For example,
if you have environment variable called ROLE with value 'dev' but you pass in
runtime parameter ROLE with value 'dev12', 'dev12' will be used during substitution.