JBoss: Get data using JMX

First in order to get data from JBoss using JMX, you need to add jboss-client.jar to your classpath. You can find this JAR file in the client directory of your JBoss installation.

In order to open a connection, you first need to create a URL:

String host = "localhost";
String port = "9010";
String urlString = "service:jmx:rmi:///jndi/rmi://" + host + ":" + port + "/jmxrmi";

You can then use this URL to get a connection to the MBean server:

JMXServiceURL serviceURL = new JMXServiceURL(urlString);
JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

With this MBean server connection, you can now for example get the number of MBeans:

int count = connection.getMBeanCount();
System.out.println("MBean count = " + count);

Now to get some more useful information, you’ll need to get an Object Name and can then use it to query some attributes of an MBean e.g.:

ObjectName objectName = new ObjectName("jboss.system:type=ServerInfo");
Integer activeThreadCount = (Integer) connection.getAttribute(objectName, "ActiveThreadCount");
System.out.println("activeThreadCount = " + activeThreadCount);

Similarly, you can also call a function of an MBean:

objectName = new ObjectName("org.hornetq:address=\"jms.queue.DLQ\",module=Core,name=\"jms.queue.DLQ\",type=Queue");
Object obj = connection.invoke(objectName, "listMessages", new Object[] {""}, new String[] { "java.lang.String" });
System.out.println("messages = "+obj.toString());

The parameters to the invoke method of the connection are:

  1. The object name specifying which MBean you want to work with.
  2. The name of the MBean method you want to call.
  3. An array containing the parameters of the method (in our case an empty String).
  4. The signature of the method (in our case, the method takes a String as parameter).

Once you are done, you just need to close the JMX connection:

jmxConnector.close();

Leave a Reply

Your email address will not be published. Required fields are marked *