In this tutorial we will show how to use detyped management API to control your application server resources. The CLI tool that comes with the application server uses this interface, and user can develop custom clients that use it as well.

A detyped API works by making it possible to build up arbitrarily complex data structures using a small number of Java types. All of the parameter values and return values in the API are expressed using those few types. Ideally, most of the types are basic JDK types, like java.lang.String, java.lang.Integer, etc. In addition to the basic JDK types, JBoss AS 7's detyped management API uses a small library called jboss-dmr.

The public API exposed by jboss-dmr is very simple: the primary class is org.jboss.dmr.ModelNode. A ModelNode is essentially just a wrapper around some value; the value is typically some basic JDK type. A ModelNode exposes a getType() method. This method returns a value of type org.jboss.dmr.ModelType, which is an enum of all the valid types of values.

In addition to jboss-dmr API the other module which is used to connect to the management API is jboss-as-controller-client. Both of these modules are included into the application server release 7.

Using the management API

There are basically two way to use the management API: from a standalone application or from a module running into the application server. If you are planning to create a standalone application, the suggested option is including the jboss-as-controller-client into your pom.xml :

<dependency>
    <groupId>org.jboss.as</groupId>
    <artifactId>jboss-as-controller-client</artifactId>
    <version>7.1.1.Final</version>
</dependency>

If you plan to test the API from the application server, all you need is adding the required Dependencies into your MANIFEST file:

Dependencies: org.jboss.as.controller-client,org.jboss.dmr					

Here is a sample Servlet which performs a JNDI-lookup:

package com.sample;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.InetAddress;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.as.controller.client.OperationBuilder;
import org.jboss.as.controller.client.helpers.ClientConstants;
import org.jboss.dmr.ModelNode;

@WebServlet("/Test")
public class Test extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            PrintWriter out = response.getWriter();
            ModelNode op = new ModelNode();
            op.get("operation").set("jndi-view");

            ModelNode address = op.get("address");
            address.add("subsystem", "naming");

            op.get("recursive").set(true);
            op.get("operations").set(true);
            ModelControllerClient client = ModelControllerClient.Factory.create(
                    InetAddress.getByName("127.0.0.1"), 9999);

            ModelNode returnVal = client.execute(op);
            out.println(returnVal.get("result").toString());
            out.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }
}

}

In this example we have created a ModelNode with the following fields:

  • operation: the name of the operation to invoke. All operation requests must include this field and its value must be a String.
     
  • address: the address of the resource to invoke the operation against. This field's must be of ModelType.LIST with each element in the list being a ModelType.PROPERTY. If this field is omitted the operation will target the root resource. Here we are targeting the jndi-view operation for the naming subsystem.

Running the Sample Servlet will display the JNDI tree of the application server.

Here's another sample (inspired from http://stackoverflow.com/questions/9704715/how-to-list-jboss-as-7-datasource-properties-in-java-code ) which invokes the read-resource command on the available data sources:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {

            PrintWriter out = response.getWriter();
            List<ModelNode> dataSources = getDataSources();
            for (ModelNode dataSource : dataSources) {
                out.println("Datasource: " + dataSource.asString());
            }

            out.close();

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public static List<ModelNode> getDataSources() throws IOException {
        final ModelNode request = new ModelNode();
        request.get(ClientConstants.OP).set("read-resource");
        request.get("recursive").set(true);
        request.get(ClientConstants.OP_ADDR).add("subsystem", "datasources");
        ModelControllerClient client = null;
        try {
            client = ModelControllerClient.Factory.create(InetAddress.getByName("127.0.0.1"), 9999);
            final ModelNode response = client.execute(new OperationBuilder(request).build());

            return response.get(ClientConstants.RESULT).get("data-source").asList();
        } finally {
            safeClose(client);
        }
    }

    public static void safeClose(final Closeable closeable) {
        if (closeable != null) try {
            closeable.close();
        } catch (Exception e) {
            // no-op
        }
    }

Creating a Datasource programmatically on AS 7 and WildFly example

By using the Model API, you can also create resources. For example, here is how to create a Datasource programmatically on Jboss AS 7 and WildFly application server:

public void createMySQLDatasource() throws Exception{
  ModelNode request = new ModelNode();
  request.get(ClientConstants.OP).set(ClientConstants.ADD);
  request.get(ClientConstants.OP_ADDR).add("subsystem","datasources");
  request.get(ClientConstants.OP_ADDR).add("data-source","java:/MySQLDS");
  request.get("jndi-name").set("java:/MySQLDS");
  request.get("connection-url").set("jdbc:mysql://localhost/test");
  request.get("driver-class").set("com.mysql.jdbc.Driver");
  request.get("driver-name").set("mysql-connector-java-5.1.29-bin.jar");
  request.get("user-name").set("username");
  request.get("password").set("password");
  request.get("pool-name").set("pool_NewDatasource");
  ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("127.0.0.1"), 9999);
  client.execute(new OperationBuilder(request).build());
}

In the above example we have created programmatically a Datasource bound into the "Java:/MySQLDS" JNDI binding, using the mysql-connector-java-5.1.29-bin.jar Driver.

0
0
0
s2smodern

Related articles available on mastertheboss.com

JBoss AS 7 introduction

This is an introduction tutorial to the newest JBoss AS 7 which a

Develop Java EE applications with JBoss AS 7

In this tutorial we will learn how to create and deploy a Java EE

Adding users with JBoss AS 7

If you are planning to add new users to your management interface

JNDI view in JBoss AS 7

JNDI can be checked on the naming subsystem of the application se

How to install a module on WildFly / JBoss AS 7

Installing a module on WildFly / JBoss AS 7 requires creating a p

WildFly / JBoss EAP classloading explained

JBoss EAP / WildFly classloading explained As mandated by Java E