Sunday, January 23, 2011

Setting Java System Properties in JBoss

I like using JVM system properties as a way to access non hard coded resources. Typing System.getProperty("myPropertyName") is convenient and clean. The best part is that these properties have JVM scope, so you don't have to worry about class loaders.

For convenience let me define a couple of variables:

%JBOSS_HOME= directory where JBoss is installed
%JBOSS_SERVER= server instance name

There are 2 ways to define system properties in JBoss:

a) By specifying them in %JBOSS_HOME/bin/run.conf

You use run.conf, don't you? Don't change run.sh or run.bat directly. That makes your application less portable. The file run.conf is used by run.sh or run.bat to setup a number of properties. To setup system properties you need modify the JAVA_OPTS variable. Find this section in run.conf and modify it accordingly:

#
# Specify options to pass to the Java VM.
#
if [ "x$JAVA_OPTS" = "x" ]; then
JAVA_OPTS="-Xms500m -Xmx500m -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000 -DyourPropertyName=yourPropertyValue"
fi

As you can see I added the property yourPropertyName to the System Properties. So, once you restart the server, you will able to simply call System.getProperty("yourPropertyName") and get the value of it in any application running in JBoss.

The problem with this approach is that to change the values of the properties, or add values, you need to restart the server.

b) Use properties-service.xml

This is my favorite method. In $JBOSS_HOME/server/$JBOSS_SERVER/deploy you will find the file properties-service.xml . In this file you can specify new system properties, modify existing ones and even remove old ones. All you need to do is uncomment the following block in properties-service.xml:






yourPropertyName=yourPropertyValue



You don't need to restart the server for defined properties or changed in properties-service.xml to take effect. Placing properties with links to web services endpoints, LDAP servers and other resources in properties-service.xml makes an application EAR or WAR more portable and easier to move from a development environment to a production environment since the properties are outside of the deployment instruments.

Reference from: http://www.hugotroche.com/my_weblog/2008/07/setting-java-sy.html

No comments:

Post a Comment