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 🙂

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.