Use conditional statements in WildFly CLI

WildFly Command Line Interface is not a real programming language with complex structures. Nevertheless it's possible to execute conditional logic in it and this short tutorial will show how to do it.

WildFly Command Line Interface contains the "if" keywords which can be used to build conditional logic.

here is the synopsys of the command:

if (condition_expression) of (command_line) 

The if statement includes a command that is executed at the beginning of the if-else control flow. After the response of the command_line is received from the controller, it is evaluated using the condition_expression.
The condition_expression consists of a node path, equals sign ('==') and a specific value. If the target value designated with the node path equals to the specified value, then the condition is met and the if block (which is a batch) is executed.

If the condition wasn't met then the else block, if present, would be executed.

For example, here is how you can execute a conditional deployment- e.g. to check if the application myproject.war is not already deployed:

if (outcome != success) of /deployment=myproject.war:read-resource   
     deploy myproject.war   

Another example, slightly more complex, will check if the com.mysql module has been already installed. The conditional execution will either install it (if not installed already) or just print a message (saying that it’s already installed):

if (outcome != success) of /core-service=module-loading/:list-resource-loader-paths(module=com.mysql)   
    module add --name=com.mysql --resources=mysql-connector-java-5.1.31-bin.jar --dependencies=javax.api,javax.transaction.api
    echo module com.mysql already installed

Finally, you can check also the return value of a particular node and execute conditional logic based on the value of it:

if (result.value==true) of /system-property=test:read-resource

Related articles available on

How do I get the list of MBeans with twiddle?

  JBoss provides a simple command line tool that allows for inte

How to count active session with twiddle ?

  There are some well known alternatives to count the number of

How do I run garbage collector from shell?

  You can suggest JBoss to run the garbage collector in differen

How do I check my application status from shell?

  This is a two process step: at first you need to retrieve the

How do you shut down multiple JBoss instances?

  Supposing you have configured 2 instances of JBoss, one on por

twiddle reference guide

Please note: twiddle is not available any more as default Command

Follow us on Twitter