External Configuration with JBoss

private-propertyIn a project I’m currently working on, I need to make some parameters configurable, and they need to be outside the .war file that I’m deploying. For example, let’s say I’m creating a service which reads data from some other RESTful service. And let’s say that the other RESTful service has two URLs, one for test and one for production. I’d like to be able to deploy my .war, and then edit a file outside of that .war file to configure which URL my service should be using.

My first inclination was to try to do this with a JNDI Environment Entry, so I began researching that approach. However, while the EJB spec states in 20.2.4 that the container must “provide a deployment tool that allows the Deployer to set and modify the values of the enterprise bean’s environment entries” (thanks for finding that one, ipage), JBoss does not seem to have such a facility.

Soon I started to wonder if JNDI wasn’t a bit overkill for what I needed to do, anyway.  I didn’t want to specify my parameters on the command-line; I wanted to simplify deployment and wanted to be able to change these values at runtime without restarting the server. But perhaps a System Property was all I needed.

As it turns out, JBoss has the System Properties Management Service for such things.  Here’s what you need to do:

  1. Make sure properties-plugin.jar is in your ${JBOSS_HOME}/server/<server>/lib directory.
  2. Make sure the properties-service.xml is in your deploy directory (you can find a copy in the “default” server directory)
  3. You now have two options, either edit the URLList to have a comma-separated list of locations of properties files, or you can specify your properties directly in properties-service.xml in the <attribute name=”Properties”> element.

Now, to access your property, all you need to do is call the venerable System.getProperty() method.

Photo Credit: Shelley Gibb

Tags: , ,

This entry was posted on Friday, January 9th, 2009 at 4:52 pm and is filed under blog. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

4 Responses to “External Configuration with JBoss”

  1. feniix Says:

    January 12th, 2009 at 7:29 pm

    What would be the difference with setting environment variables from the command line in the startup script in a JAVA_OPTS variable?

  2. feniix Says:

    January 12th, 2009 at 7:31 pm

    I meant:

    set JAVA_OPTS=%JAVA_OPTS% -DURL=pete.com
    or
    JAVA_OPTS=”${JAVA_OPTS} -DURL=pete.com”
    export JAVA_OPTS

  3. admin Says:

    January 12th, 2009 at 7:39 pm

    @feniix – nothing really. In my particular situation, it’s easier to have people deploy an .xml file to the deploy directory than it is to make different run scripts for each server that define the different JAVA_OPTS.

  4. feniix Says:

    January 13th, 2009 at 2:36 am

    I slightly dissent on that, I mean, may be for a local environment its better/configurable from the IDE really quick and without a restart.
    I tried the properties-service.xml with one of the applications I manage that uses the environment variables defined in the command line and it seems is not working as expected.
    I still need to do another test making sure that the properties-service.xml gets loaded always before the application.
    At any rate, if you define properties in the service.xml I’d say that you application won’t get the new property values from there even if you redeploy the properties-service.xml

    I was really looking forward to see this working on my environments.

    It’s pretty cool though :)