/
Querying MBean Attributes in JBoss

Querying MBean Attributes in JBoss

Introduction

This page quickly describes how to access MBean attributes from within a Servlet or JSP in JBoss. In my case, I needed to update an existing monitoring JSP to add some information about the status of several DataSources. We were interested in monitoring how many connections were available in each of the connection pools the app was using.

In this example, we assume that we already know the JMX locations for each of the MBeans we are interested in. If this is not the case, you can use the MBeanServer to discover the available MBeans. However, that is out of scope for this page.

Sample Code

First, you need to get access to the JBoss MBeanServer. This is quite simple:

MBeanServer mbs = MBeanServerLocator.locateJBoss();

Note that MBeanServerLocator is a JBoss class located in package org.jboss.mx.util. You will need to add the appropriate JBoss JAR as a dependency in order to compile this code. If you are using Maven, add the following dependency to your pom.xml:

<dependency>
   <groupId>jboss</groupId>
   <artifactId>jboss-jmx</artifactId>
   <version>4.0.2</version>
   <type>jar</type>
   <scope>provided</scope>
</dependency>

Once you have access to the MBeanServer, you can go ahead and query it for the attributes you are interested in. You'll need the ObjectName of the MBean you want to query. For example, the ObjectName of the default JBoss DataSource might be:

jboss.jca:name=DefaultDS,service=ManagedConnectionPool

So, to query the relevant MBean attributes of the default datasource's connection pool, do something like this:

MBeanServer mbs = MBeanServerLocator.locateJBoss();
String jmxLocation = "jboss.jca:name=DefaultDS,service=ManagedConnectionPool";
ObjectName objectName = new ObjectName(jmxLocation);
String maxConnections = String.valueOf(mbs.getAttribute(objectName, "MaxSize"));
String availableConnections = String.valueOf(mbs.getAttribute(objectName, "AvailableConnectionCount"));
String inUseConnections = String.valueOf(mbs.getAttribute(objectName, "InUseConnectionCount"));

Note: MBeanServer and ObjectName are in package javax.management.

Tip - JMX Locations

If you have access to the JBoss JMX Console, you can use it to figure out what the JMX location of your MBean is.