Seamless Server Deployment

Posted on Posted in Java, Technology Center

So you wanted a seamless deployment process to your servers without worrying about server specific settings? Just upload the war file and voila!, your production servers have the latest build. Here’s a step-by-step instruction how to achieve this.
STEP 1: Identify all your server variables.

Identify the variables that have different values across the servers such as data file locations and database settings. You will need to externalize these variables so that you don’t have to reconfigure them every time you redeploy. Some of the common variables are:

  • Local location of file/directory.
  • Server tracking variables (e.g. for web analytics)
  • Database access

STEP 2: Externalize your server variables.

The best solution I’ve found to externalize variable is a combination of JNDI and property file. The JNDI identifies which property file to use, while the property file contains the values needed for each server. You’ll need to have several property files, one for each server.

So for example, you have 3 servers that needs access to 3 separate file locations. You will then create 3 property files as follows:
server1.properties containing:

server.dataDir = /usr/home/test/data

server2.properties

server.dataDir = C:/data

server3.properties

server.dataDir = /var/www/html/test/data

Now, set your JNDI (in Tomcat) as follows:

  • Add the following line to server.xml under GlobalNamingResources:

  • Add mapping in your web.xml

  • Add ResourceLink in context.xml.

STEP 3: Add codes to access JNDI and property file.Now that you have the configurations in place, its time to tell your application to read the property files based on the JNDI settings. Create a singleton class that will retrieve the properties as follows:

try {
Context initContext = new InitialContext();
if (initContext == null ) {
throw new Exception(“Boom – No Context”);
}
Context envContext = (Context)initContext.lookup(“java:/comp/env”);
String propFilename = (String) envContext.lookup(“serverProperties”);
Properties prop = new Properties();
try {
prop.load(getClass().getClassLoader().getResourceAsStream(propFilename));
} catch (IOException e) {
e.printStackTrace();
}
… access prop, it now contains the property settings ….
} catch (Exception nex) {
nex.printStackTrace();
}

STEP 4: Test your deployment!

It’s actually that simple to configure your application to be server independent. The other thing you’d like to work on is to create an ant build that will build the war and auto-deploy the war to Tomcat.

One thought on “Seamless Server Deployment

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.