Hi,

Java Management Extensions (JMX) is a Java technology that supplies tools for managing and monitoring applications, system objects, devices (e. g. printers) and service oriented networks. Those resources are represented by objects called MBeans (for Managed Bean), MBeans can be used for monitoring the resources deployed on servers.

In this current demonstration we will use a simple java client to retrieve the JBoss DataSource informations like ConnectionCreatedCount, AvailableConnectionCount and InUseConnectionCount. Here the “InUseConnectionCount” means that some thread are currently using these many number of connections to communicate with the database. Mostly in production scenarios it is desired to know how much connections are in use by applications and how much connections are availabe to be used. So in the following simple JMX code we can add Mailing service as well to send an alert E-Mail to the administrators as soon as we see any abnormal behaviour in our DataSources.

Step1). Create a directory somewhere in your file system like “/home/userone/testing”.

Step2). Write a simple java program to retrieve the informations/connection statistics from the DataSource deployed on JBoss Server, as following:

import javax.management.*;
import java.io.*;
import java.util.*;
import java.rmi.*;
import javax.naming.*;
public class MBeanTest
  {
      public static void main(String ar[]) throws Exception
      {
            Hashtable ht=new Hashtable();
            ht.put(Context.INITIAL_CONTEXT_FACTORY,"org.jboss.security.jndi.JndiLoginInitialContextFactory");
            ht.put(Context.PROVIDER_URL,"jnp://10.10.10.10:1099");
            ht.put(Context.SECURITY_PRINCIPAL,"admin");
            ht.put(Context.SECURITY_CREDENTIALS,"admin");

            System.out.println("nt 1- Getting InitialContext...... ");
            Context ctx = new InitialContext(ht);
            System.out.println("nt 2- Got InitialContext: "+ctx);

            MBeanServerConnection server = (MBeanServerConnection) ctx.lookup("jmx/invoker/RMIAdaptor");
            System.out.println("nntConnectionCreatedCount = "+ (Integer)server.getAttribute(new ObjectName("jboss.jca:service=ManagedConnectionPool,name=TestDS"), new String("ConnectionCreatedCount")));
            System.out.println("nntAvailableConnectionCount = "+ (Long)server.getAttribute(new ObjectName("jboss.jca:service=ManagedConnectionPool,name=TestDS"), new String("AvailableConnectionCount")));
            System.out.println("nntInUseConnectionCount = "+ (Long)server.getAttribute(new ObjectName("jboss.jca:service=ManagedConnectionPool,name=TestDS"), new String("InUseConnectionCount")));
      }
  }

Step3). Make sue that you have added the “jbossall-client.jar” file in your CLASSPATh before compiling and running the above program, this jar is present isnide “$JBOSS_HOME/client” directory. This jar does not contain any class files inside it but it has a “META-INF/MANIFEST.MF” file, which has the required classpath etries for a client application:

Above is just a sample to demonstrate how to retrieve the MBean informations from JBoss. This program can be extended in order to get the monitoring informations from various other modules deployed on JBoss.
.
.
Thanks
Middleware Magic 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.