Run multiple app instances with diff config files on same tomcat server

28 / May / 2015 by Amit Jain 1 comments

The application I am working on, requires different configurations for the each client. For internal QA environment, we wanted to deploy multiple instances of this app with different configuration setup so that it can be tested easily and without creating tomcat instance for each deployment.

So the challenge was to use different external configuration file for each war file. We tried passing parameter (using -D= but this parameter was detected at the time when war wasn’t exploaded).

So thought came that each war file should know either the environment variable or the path to external config file. So we decided to create and add the file dynamically to the war file at the time of its creation. This file had the path to the external config to be used and that worked.

Lets looks at sample of what we did :
In _Events.groovy file
[sourcecode lang=”GROOVY”]

eventCompileStart = {
updateEnvVarForExtConfig()
}

private updateEnvVarForExtConfig() {
private updateEnvVarForExtConfig() {
try {
def path = getClass().getProtectionDomain().getCodeSource().getLocation().getPath()
2.times {
path = path.substring(0, path.lastIndexOf(File.separator))
}
path += "${File.separator}grails-app${File.separator}conf${File.separator}CustomConfig.groovy"
File file = new File(path)
String configPath = System.getProperty("MY_CONFIG_PATH") ?: ""
file.text = """
class CustomConfig {
public static final String MY_CONFIG_PATH = "${configPath}"
}
""".stripMargin()
} catch (Throwable e) {
println "| Error occurred while creating CustomConfig – " + e.message
}
}}

[/sourcecode]
The above method in _events.groovy, creates the file CustomConfig.groovy in grails-app/conf folder that holds the path to external config file.

In Config.groovy, we used this CustomConfig file to read that path.

[sourcecode lang=”groovy”]

grails.config.locations = []

String externalConfigLocation = CustomConfig?.MY_CONFIG_PATH?.size() > 0 ? CustomConfig.MY_CONFIG_PATH : System.getenv(Constants.DEFAULT_EXT_CONFIG_PATH)
if (externalConfigLocation) {
println "Load config from external file located at " + externalConfigLocation
grails.config.locations << "file:" + externalConfigLocation
} else {
println "Warning: No external config file configured"
}
[/sourcecode]

So now while creating war file, we can pass the path to external config file as given below.

Say for QA – grails -DMY_CONFIG_PATH=~/Config_A war
Say for Demo – grails -DMY_CONFIG_PATH=~/Config_B war

If you have solved the same problem differently let us know.

Cheers!!
~~Amit Jain~~

FOUND THIS USEFUL? SHARE IT

comments (1 “Run multiple app instances with diff config files on same tomcat server”)

Leave a Reply

Your email address will not be published. Required fields are marked *