Using the raw management API to manage the application server

If you don't feel like learning a scripting language to manage the application server, you can still use the raw management API from within your Java classes. Don't be influenced by the fact that we left this option as the last one; in fact, using the native management API is not difficult at all since it is based on very few classes and has little compile-time and runtime dependencies on the JBoss API.

For this reason, you can use the management API as well from any Java EE application, by simply adding the following dependencies in the META-INF/MANIFEST.MF file of your application:

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

The core API named detyped management API is quite simple; the primary class is org.jboss.dmr.ModelNode, which we already mentioned in the Jython section. A ModelNode is essentially just a wrapper around a value; the value is typically a basic JDK type, which can be retrieved using the getType() method of ModelNode.

In addition to the jboss-dmr API, the other module that is used to connect to the management API is jboss-as-controller-client.

Note

You don't need to download any of these libraries, since both of these modules are included in the application server release 7.

Reading management model descriptions via the raw management API

Using the detyped management API is not too different from the scripting language counterpart; at first, you need to create a management client that can connect to your target process's native management socket (which can be an individual standalone mode server, or in a domain mode environment, the domain controller):

ModelControllerClient client = ModelControllerClient.Factory.create(InetAddress.getByName("localhost"), 9999);

Next, you need to create an operation request object using the org.jboss.dmr.ModelNode class, as shown in the following command:

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);

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

As you can see, the ModelNode objects can be chained in order to reach an operation (in the example, the JNDI view), which is available on a node path (in our case the naming subsystem).

Once you have added the ModelNode attributes, you can issue the execute commands on your node, which will in turn return ModelNode, where the result of the operation will be stored.

Creating your resource watches using the detyped API

Now that you have learned the basics of the detyped management API, we will illustrate a concrete example; our goal will be monitoring a server resource (the number of active JDBC connections for a datasource) using an EJB. You can use this pattern to create your own server watches, which can be integrated with your application environment:

import java.io.IOException;
import java.net.*;
import java.util.logging.Logger;

import javax.annotation.Resource;
import javax.ejb.*;

import org.jboss.as.controller.client.ModelControllerClient;
import org.jboss.dmr.ModelNode;

@Stateless
public class WatchMyDB
{

  private final static Logger logger = Logger.getLogger(WatchMyDB.class.getName()  );());

  @Resource
  private TimerService timerService;

  @Schedule(dayOfWeek = "*", hour = "*", minute = "*", second = "*/30",year="*", persistent = false)
  public void backgroundProcessing()
  {
    ModelControllerClient client=null;
    try {
      client = ModelControllerClient.Factory.create
      (InetAddress.getByName("localhost"), 9999);


      ModelNode op = new ModelNode();
      op.get("operation").set("read-resource");
      op.get("include-runtime").set(true); 
          
      ModelNode address = op.get("address");
      address.add("subsystem", "datasources");
      address.add("data-source", "ExampleDS");
      address.add("statistics", "pool");
      ModelNode returnVal=null;
      returnVal = client .execute(op);

      ModelNode node2 = returnVal .get("result");
      String _activeCount = node2.get("ActiveCount").asString();

      if (_activeCount.equals("undefined")) {
        return; // Connection unused
      }
      int activeCount = Integer.parseInt(_activeCount);

      if (activeCount > 50) {
        alertAdministrator(); // Implement it !
      }
    }
    catch (Exception exc) {
      logger.info("Excepton ! "+exc.getMessage());
    }
    finally {
      safeClose(client);

    }
  }
  public static void safeClose(final Closeable closeable) {
    if (closeable != null) try {
      closeable.close();
    } catch (Exception e) {
      logger.info("Excepton closing the client! "+e.getMessage());
    }
  }

}

We will not rehash the basic concepts about EJB Timers, which have been discussed in Chapter 3, Beginning Java EE 6 – EJBs. We suggest that you have a look at the highlighted section of the code, which shows how you can chain your ModelNode objects in order to reach the attribute that we are going to monitor (the activeCount attribute of the ExampleDS datasource).

Once you have the value of the activeCount attribute, we leave to your imagination all the possible actions you can undertake!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.58.194