Hi,

Many times it is desired to findout the Server state using the Java based APIs so that based on certain conditions it can be found whether the server is actually running or not. Usually it is needed by the automation tools which can find the server state and based on the state it decides whether to start the server or not.

The org.jboss.as.controller.client.ModelControllerClient class is the main class a custom client would use to manage a WildFly server instance or a Domain Controller or slave Host Controller. More information about the usage of this API can be found in the link: https://docs.jboss.org/author/display/WFLY8/The+native+management+API

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.helpers.Operations;
import org.jboss.dmr.ModelNode;
public class ServerStateModelControllerClient {
	public static void main(String[] args) throws Exception {
		ModelControllerClient client = null;
		client = ModelControllerClient.Factory.create("localhost", 9990);
		for(int i=0; i<10; i++) {
			ModelNode operation = Operations.createReadAttributeOperation(new ModelNode().setEmptyList(), "server-state");
			ModelNode result  = client.execute(operation);
                        java.util.Date date = new java.util.Date();
                        if (Operations.isSuccessfulOutcome(result)) {
                            System.out.printf(i + " - Server state: %s%n", Operations.readResult(result) + " at " + date);
                        } else {
                            System.out.printf(i + " - Failure! %s%n", Operations.getFailureDescription(result) + " at " + date);
                        }

                        Thread.sleep(5000);
   	        }
		client.close();
	}
  }

Users need to make sure to add the following JAR while compiling and running the program:

export CLASSPATH=/PATH/TO/wildfly-8.2.0.Final/bin/client/jboss-cli-client.jar:.:$CLASSPATH

.
.
Thanks
MiddlewareMagic Team 🙂

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.