Pages

Monday, July 19, 2010

System Properties in JBoss AS [EWP]

Share it Please

Properties play an important role in enterprise application. Values are hard coded and they are accessed by the application code. When we need a different value, we just change the property value and we got the new value.

In this article, we will see how we can send properties to a JBoss application server and access them in the application code. There are 2 ways by which we can set the properties in a JBoss server

1. By providing them in JBoss_home/bin/run.conf

Run.conf file is used by run.bat and run.sh in setting a number of properties for the server and the environment. In order to setup the properties in run.conf  look for the “JAVA_OPTS” and add your properties at the end like,

-Dname=jagadesh

But we need to restart the server in order for the properties to get effective.

2. The second one was a better solution,

Create a file called “properties-service.xml” in the deploy directory [see if it is already there, if not create with the name]. Now copy the below code to it

<server>
    <mbean code="org.jboss.varia.property.SystemPropertiesService"
           name="jboss.util:type=Service,name=SystemProperties">
           
        <!-- Load properties from each of the given comma seperated URLs -->
        <!--<attribute name="URLList">
            http://somehost/some-location.properties,
            ./conf/somelocal.properties
        </attribute>
        -->
                            
        <!-- Set properties using the properties file style. -->
        <attribute name="Properties">
            name=jagadesh
            age=below20
        </attribute>
           
    </mbean>
</server>

I have defined 2 properties name and age.
attribute name="URLList" allows defining the url of the locations from where the properties files can be loaded.

Now once you create this file in the deploy folder, you can access the properties like

String myName=System.getProperty("name");from your application code.

One of the main advantages of using the second option is that we don’t need to restart the server, just access the property and we will get the recent value if changed.

More articles to come, so happy coding…..

No comments :

Post a Comment