Advertise with us

Our eBooks

How to use environment variables in WildFly configuration (standalone.xml or host.xml)

Passing environment variables to WildFly or JBoss AS can be accomplished in two ways:

1) Using Java System Properties

This is can be achieved in Linux/Unix hosts by exporting the environment variable so that it can be injected as Java System Property into your JBoss or WildFly process. Here is an example:

$ export LOGPATH=/home/jboss/log
$ ./standalone.sh -DlogPath=LOGPATH

 Then in your configuration you can use the System Property with a standard Bean Shell expression, as in this example:

<appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
 <param name="File" value="${logPath}/server.log"/>
   . . . .  
</appender>

2) Referencing directly environment variables

There is also a more "direct" approach to use environment variables, without turning them into System Properties: just use the env pattern in the Bean Shell expression. See the following example:

<appender name="FILE" class="org.jboss.logging.appender.RollingFileAppender">
 <param name="File" value="${env.LOGPATH}/server.log"/>
   . . . .  
</appender>

 The env pattern is quite flexible, as you can use it in conjunction with System Properties and a default fall back expression. See the following example:

<interface name="public">
    <inet-address value="${jboss.bind.address,env.HOST:192.168.10.1}"/>
</interface>

In the above example, at first the Java system property jboss.bind.address is evaluated and, if included, used. Otherwise the env.HOST Environment variable is checked. If none is used, the interface is assigned the fallback expression 192.168.10.1

Finally, it is worth mentioning that you can pass environment variables to start a WildFly server in a container using the "-e" parameter. Here is an example:

docker run --name="wildfly" -d -p 8080:8080 -p 9990:9990 
    -e WILDFLY_PASS="admin" 
    -e POSTGRES_USER="postgres" 
    -e POSTGRES_PASSWORD="password" 
    -e POSTGRES_CONNECTION="jdbc:postgresql://postgres/postgresdb" 
    wildfly

if you want to learn more about WildFly expression and how are evaluated, check this tutorial: How Wildfly uses the expression evaluator to evaluate attributes

© 2021 mastertheboss.com. All Rights Reserved.

Please publish modules in offcanvas position.