Archive for March, 2015

How to list all the deployments belongs to any server group in WildFly8.2 using java code ?

Hi,

Here is another example of using the ModelControllerClient API which can be used to retrived the details of the applications which are deployed on the JBoss AS7 Server groups or the WildFly 8.2 Application Server’ server groups in Domain mode.

As many times it is desired to pull the list of the applications deployed/targeted to different server-groups present in a domain. Many times we might not know the names of all the server groups and the name of the applications either, So in that case following kind of Java based API can be useful to fetch such information.

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 java.io.IOException;

// CLI API
import org.jboss.as.cli.CommandContext;
import org.jboss.as.cli.CommandContextFactory;
import org.jboss.as.cli.CommandLineException;
import org.jboss.as.cli.CommandFormatException;
import org.jboss.as.controller.client.ModelControllerClient;

// DMR API
import org.jboss.dmr.ModelNode;

public class Main
 {
   public static void main(String[] args) throws CommandLineException, IOException
   {
      String host = args[0];   // Specify IP Address/host name here
      int port = Integer.parseInt(args[1]);   // usually 9999


      Main main=new Main();
      CommandContext ctx = CommandContextFactory.getInstance().newCommandContext("admin","admin@123".toCharArray());
      ctx.connectController(host, port);
      ModelNode deployRequest = null;
      try {
                String request="/server-group=*/deployment=*/:read-resource(recursive=false,proxies=true,include-runtime=true,include-defaults=true)";
                deployRequest = ctx.buildRequest(request);
      } catch (CommandFormatException e) {
                e.printStackTrace();
      }
      main.runCommands(ctx,deployRequest);
      ctx.disconnectController();
   }

   public void runCommands(CommandContext ctx,ModelNode modelNode) {
         System.out.println("Running command ---- ");
         ModelControllerClient client = ctx.getModelControllerClient();
         if(client != null) {
            try {
                  ModelNode response = client.execute(modelNode);
                  System.out.println("nt response = > "+response);
            } catch (IOException e) {
                e.printStackTrace();
            }
         } else {
              System.out.println("The ModelControllerClient is not available. Connection error might have occured.");
        }
    }
 }

NOTE: Users need to make sure that they add the client jar present inside the “$WILDFLY_HOME/bin/client” directory to their classpath before compiling and running the above code. Above program shows that the client can connect to WildFly remotely, But it is also possible to connect to WildFly from within the same container. (no need to pass URL and credentials)

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

– The output of the above code can be something like following:

         WARN: can't find jboss-cli.xml. Using default configuration values.
         Mar 30, 2015 12:48:36 PM org.xnio.Xnio <clinit>
         INFO: XNIO version 3.3.0.Final
         Mar 30, 2015 12:48:36 PM org.xnio.nio.NioXnio <clinit>
         INFO: XNIO NIO Implementation Version 3.3.0.Final
         Mar 30, 2015 12:48:36 PM org.jboss.remoting3.EndpointImpl <clinit>
         INFO: JBoss Remoting version 4.0.6.Final
         Running command ----

         	 response = > {
             "outcome" => "success",
             "result" => [
                 {
                     "address" => [
                         ("server-group" => "main-server-group"),
                         ("deployment" => "jaxws-cxfXMLdemo.war")
                     ],
                     "outcome" => "success",
                     "result" => {
                         "enabled" => true,
                         "name" => "jaxws-cxfXMLdemo.war",
                         "runtime-name" => "jaxws-cxfXMLdemo.war"
                     }
                 },
                 {
                     "address" => [
                         ("server-group" => "other-server-group"),
                         ("deployment" => "jaxws-cxfXMLdemo.war")
                     ],
                     "outcome" => "success",
                     "result" => {
                         "enabled" => true,
                         "name" => "jaxws-cxfXMLdemo.war",
                         "runtime-name" => "jaxws-cxfXMLdemo.war"
                     }
                 }
             ]
         }

.
.
Thanks
MiddlewareMagic Team 🙂


How to find Server State in WildFly 8.2 using ModelControllerClient ?

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 🙂


Copyright © 2010-2012 Middleware Magic. All rights reserved. |