In this tutorial we will cover how to load System Properties or Application Properties running on WildFly Application Server. We will also discuss how to set System Properties when running WildFly in a Kubernetes environment.
In a Bare Metal installation of WildFly, there are mainly three strategies to load Properties:
- Add the System Properties as JVM arguments to the start up script
- Include the System Properties in WildFly Configuration
- Include the Application Properties in a module of the application server
- Inject the System Properties through an environment variable (WildFly 25 or newer)
Let’s see them all in detail.
Setting System Property 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
Next, edit the standalone.conf file and add it to the JAVA_OPTS variable:
JAVA_OPTS="$JAVA_OPTS -Dproperty=value"
Finally, in domain mode, add it as JVM Option in the host.xml file:
<jvm-options> <option value="-server"/> <option value="-XX:MetaspaceSize=96m"/> <option value="-XX:MaxMetaspaceSize=256m"/> <option value="-Dproperty=value"/> </jvm-options>
Setting System Properties into 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>
Finally, you can read/write System Properties from the CLI as follows:
$ ./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 Application properties from a folder
If you want to load your Applicaion Properties from a folder, we recommend 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
Then, 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>
Finally, 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(); } }
Setting System Properties at application level
Next, it is worth mentioning that WildFly is able to use the META-INF/jboss.properties file to set System Properties at deployment level.
Here is an example application which includes the jboss-properties file to set System Properties only for that deployment:
test.war ├── index.jsp └── META-INF └── jboss.properties
Therefore, if the jboss.properties includes the following:
foo=bar
Then, your index.jsp will display the property “foo”:
<% out.println("foo: " + System.getProperty("foo")); %>
Injecting properties through environment variables
As the cloud use-cases rely often on environment variables rather than system properties, it is also possible to inject properties from environment variables as well..
Please note, is is required to use WildFly 25 or newer to use this feature.
For example, if you have the following configuration:
<file relative-to="jboss.server.log.dir" path="${log_file:server.log}"/>
Then, you can inject the value of the path element as follows:
$ export LOG_FILE=wildfly.log $ ./standalone.sh
Finally, note that the System property will take precedence over the environment variable if both are defined.
Setting Properties in MicroProfile applications
If you are developing applications using MicroProfile API, we recommend checking the MicroProfile Config which allows fine-grained control over your application properties: Configuring Microservices with MicroProfile Configuration
Setting Properties in Kubernetes environment
Finally, if you are deploying your applications on OpenShift or Kubernetes, the most common option to pass properties to WildFly runtime is via Environment Properties. For example, if you are deploying WildFly using Helm Charts, then you can add them in the env section of your YAML file:
build: uri: https://github.com/wildfly/quickstart.git ref: 27.0.0.Final contextDir: microprofile-config mode: bootable-jar env: - name: MAVEN_ARGS_APPEND value: -Pbootable-jar-openshift -Djkube.skip=true deploy: replicas: 1 env: - name: CONFIG_PROP value: Hello from OpenShift
Besides, if you want to externalize the properties, for example from a ConfigMap, then you can reference the ConfigMap from your deploy section as follows:
deploy: replicas: 1 envFrom: - configMapRef: name: <name-of-your-config-map>
To learn more about deploying WildFly on Kubernetes using Helm Charts check this article: WildFly on the Cloud with Helm
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");