How to set System properties in JBoss / WildFly ?

In this tutorial we will cover how to load System Properties from applications running on WildFly Application Server.

There are mainly three strategies to load System Properties in WildFly:

  1. Add the System Properties as JVM arguments to the start up script
  2. Include the Properties in WildFly Configuration
  3. Include the Properties in a module of the application server

Let’s see them all in detail.

Setting System Properties as JVM arguments

This is the simplest option and works as any other Java application. You can add the System Property MSG to the start up script of WildFly as follows:

$ ./standalone.sh -DMSG=Hello

This option is recommended in limited use cases. It is used more often as a tweak to the application server’s system properties.

Setting System Properties to WildFly configuration

The other option requires that you either add your system property directly into the configuration file or use the CLI. Let’s see first the configuration file approach:

In the Standalone mode,  the change will go into standalone.xml
In the Domain mode, the change will go into domain.xml
Add the system-properties  element right after the extensions element.

<extensions>
  . . .
</extensions>

<system-properties>
        <property name="my.project.dir" value="/home/francesco"/>
</system-properties>

System properties can be read/written as well using the CLI:

$ ./bin/jboss-cli.sh  
[standalone@localhost:9999 /] /system-property=foo:add(value=bar)
[standalone@localhost:9999 /] /system-property=foo:read-resource
{
    "outcome" => "success",
    "result" => {"value" => "bar"}
}

How to load properties from a folder

If you want to load your System Properties from a folder, it is recommended to place your property file in a module and use that module in your application.

Let’s see how to do it. Place the property file (say file.properties) in the “bin” folder of the application server and launch the jboss-cli.sh:

$ jboss-cli.sh

Execute the following command:

module add --name=configuration --resources=file.properties

The following configuration will be created under the “modules” folder:

configuration/
└── main
    ├── file.properties
    └── module.xml

As you can see, our module named “configuration” has been defined. The file module.xml contains the list of resources available in that module:

<?xml version='1.0' encoding='UTF-8'?>

<module xmlns="urn:jboss:module:1.1" name="configuration">
    <resources>
        <resource-root path="file.properties"/>
    </resources>
</module>

Now, if you want to use this module at application level, you can add it to your jboss-deployment-structure.xml:

<jboss-deployment-structure>
  <deployment>
      <dependencies>
         <module name="configuration" export="TRUE"/>
      </dependencies>
    </deployment>
</jboss-deployment-structure>

As an alternative, if the property file is shared between multiple applications, you can include it in the list of global modules, within your server configuration:

<subsystem xmlns="urn:jboss:domain:ee:1.0" >            
  <global-modules>
    <module name="configuration" slot="main" />            
  </global-modules>
</subsystem>

When the Property file is available as module, to load it, you can use the following code:

    Properties props = new Properties();
 
        try {
            java.io.InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("file.properties");
            // Read the Properties file
            props.load(stream);
 
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

JBoss AS 4-5-6

One cool way to add a list of System properties to JBoss is the Properties MBean Service.
In the deployment folder look for “properties-service.xml“. (if you don’t have it in your release you can create it at any time) :

Now add your properties either in the URLList Attribute or in the Properties Attribute:

<server>
<mbean code="org.jboss.varia.property.SystemPropertiesService" 
     name="jboss:type=Service,name=SystemProperties">
 
    <attribute name="URLList">
      http://somehost/some-location.properties,
      ./conf/somelocal.properties
    </attribute>

    <attribute name="Properties">
       property1=This is the value of my property
       property2=This is the value of my other property
    </attribute>
   
</server>

As you can see the The “URLList” is a comma-separated list of URL strings from which to load properties file-formatted content while the “Properties” is a specification of multiple property name=value pairs

Now you can access your properties with standard:

System.getProperty("property1");