Hi,

During various builds of JBoss AS7 the MBean access methodology was kee changing, even the JMX subsystem XSD “jboss-as-jmx_1_1.xsd” also changed a lot from JBossAS7.0 till JBossAS7.1.1 Final. So here we are going to see How can we access the JBoss MBeans Remotely & Programatically. Here are the following points which we will notice as part of this article:

Point-1). Accessing MBeans using the JBoss Native Management port 9999 on JBoss AS7.1.1.Final.

Point-2). The jmx subsystem inside our JBoss configuration file like “standalone-full.xml” should look something like following:

        <subsystem xmlns="urn:jboss:domain:jmx:1.1">
            <show-model value="true"/>
             <remoting-connector  use-management-endpoint="true" />
        </subsystem>

Here If the value of “use-management-endpoint” is “true” then this connector will use the management endpoint, otherwise it will use the remoting subsystem endpoint.

Point-3). Here we will also need to make sure that we enable the following System Property in the JBoss Cofiguration file (standalone*.xml) So that JBoss will make the “jboss.web:type=Manager” access available for the remote JMX Clients.

    <system-properties>
        <property name="org.apache.tomcat.util.ENABLE_MODELER" value="true"/>
    </system-properties>

***NOTE***: If you will not add the above System property for your JBoss then JBoss will not expose some of the MBeans which are available as part of “jboss.web:type=*”

Accessing MBeans Programatically Remotely/Locally

Step-1). After enabling the System property org.apache.tomcat.util.ENABLE_MODELER=true on your JBoss, Start your JBoss AS7.1.1 Final like following

     ./standalone.sh  -c standalone-full.xml

You can also start it in a particular Bind address as well.

Step-2). Write a Simple Class “JBossJMXClient.java” inside “/home/userone/JmxDemo/” as following:

import javax.management.MBeanServerConnection;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import javax.management.*;

public class JBossJMXClient {
    public static void main(String[] args) throws Exception {
        String host = "localhost";  // Your JBoss Bind Address default is localhost
        int port = 9999;  // management-native port

        String urlString ="service:jmx:remoting-jmx://" + host + ":" + port;
        System.out.println("		nnt****  urlString: "+urlString);

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

        //Invoke on the JBoss AS MBean server
        int count = connection.getMBeanCount();
        System.out.println(count);

        ObjectName objectName=new ObjectName("jboss.web:type=Manager,path=/Log4jDemo,host=default-host");
        Integer sessionMaxAliveTime=(Integer)connection.getAttribute(objectName, "sessionMaxAliveTime");
        Integer maxInactiveInterval=(Integer)connection.getAttribute(objectName, "maxInactiveInterval");
        Integer activeSessions=(Integer)connection.getAttribute(objectName, "activeSessions");
        Integer sessionCounter=(Integer)connection.getAttribute(objectName, "sessionCounter");
        Integer sessionAverageAliveTime=(Integer)connection.getAttribute(objectName, "sessionAverageAliveTime");
        Long processingTime=(Long)connection.getAttribute(objectName, "processingTime");
        Integer sessionIdLength=(Integer)connection.getAttribute(objectName, "sessionIdLength");
        Integer maxActive=(Integer)connection.getAttribute(objectName, "maxActive");
        Integer maxActiveSessions=(Integer)connection.getAttribute(objectName, "maxActiveSessions");
        Integer expiredSessions=(Integer)connection.getAttribute(objectName, "expiredSessions");
        Integer rejectedSessions=(Integer)connection.getAttribute(objectName, "rejectedSessions");

        System.out.println("	maxInactiveInterval     = "+maxInactiveInterval);
        System.out.println("	activeSessions          = "+activeSessions);
        System.out.println("	sessionCounter          = "+sessionCounter);
        System.out.println("	sessionAverageAliveTime = "+sessionAverageAliveTime);
        System.out.println("	sessionAverageAliveTime = "+sessionAverageAliveTime);
        System.out.println("	processingTime          = "+processingTime);
        System.out.println("	sessionIdLength         = "+sessionIdLength);
        System.out.println("	maxActiveSessions       = "+maxActiveSessions);
        System.out.println("	maxActive               = "+maxActive);
        System.out.println("	expiredSessions         = "+expiredSessions);

        jmxConnector.close();
    }
}

/*  Make sure that the following System property is enable in your JBoss

    <system-properties>
        <property name="org.apache.tomcat.util.ENABLE_MODELER" value="true"/>
    </system-properties>
*/

***NOTE***: In the above program we are trying to access various attributes present inside the ObjectName “jboss.web:type=Manager,path=/Log4jDemo,host=default-host” where “Log4jDemo” is the Context-Root of My WebApplication which i wanted to monitor. And th edefault Host-Name is “default-host”, In your case these values might be different so better use the JConsole to connect to JBoss Locally and then get the exact Object name from it.

Step-3). Now oipen a Terminal where we need to set the PATh and CLASSPATH like following:

For UNIX Based Operating System:

export PATH=/home/userone/jdk1.6.0_21/bin:$PATH
export CLASSPATH=/home/userone/jboss-as-7.1.1.Final/bin/client/jboss-client.jar:$CLASSPATH:.:

++++++++++++++++++++++++++++++++
For Windows Based Operating System

set PATH=C:jdk1.6.0_21bin;%PATH%
set CLASSPATH=c:jboss-as-7.1.1.Finalbinclientjboss-client.jar;%CLASSPATH%;.;

***NOTE***: It is must that your JMX Client should include the “jboss-client.jar” in it’s classpath as above.

Step-4). Now compile and run the above program:

[userone@localhost JmxDemo]$ javac JBossJMXClient.java
[userone@localhost JmxDemo]$ java JBossJMXClient

	****  urlString: service:jmx:remoting-jmx://localhost:9999

May 20, 2012 9:54:12 AM org.xnio.Xnio <clinit>
INFO: XNIO Version 3.0.3.GA
May 20, 2012 9:54:12 AM org.xnio.nio.NioXnio <clinit>
INFO: XNIO NIO Implementation Version 3.0.3.GA
May 20, 2012 9:54:12 AM org.jboss.remoting3.EndpointImpl <clinit>
INFO: JBoss Remoting version 3.2.3.GA
243
	maxInactiveInterval     = 1800
	activeSessions          = 3
	sessionCounter          = 3
	sessionAverageAliveTime = 0
	sessionAverageAliveTime = 0
	processingTime          = 0
	sessionIdLength         = 18
	maxActiveSessions       = -1
	maxActive               = 3
	expiredSessions         = 0

Accessing MBeans using JConsole Locally

As mentioned earlier that If you will not add the above System property (org.apache.tomcat.util.ENABLE_MODELER=true) for your JBoss then JBoss will not expose some of the MBeans which are available as part of “jboss.web:type=*” So after enabling it Just start the Server and Open the “$JAVA_HOME/bin/jconsole” . jconsole will be able to detect any locally running JAVA process just connect to your JBoss process through it then you will be able to see the following kind of JBoss MBeans inside jconsole:

JConsole_WebContainerMBean_Access_JBossAS711

.
.
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.