Java Code Geeks

Saturday, March 21, 2009

How to connect to Tomcat to gather information from JMX MBeans?

Tomcat provides see of information through its JMX MBeans. It is also easy to connect to tomcat over JMX, just run tocat using the following options -




This will start the tomcat server on port 9999. Use these JVM options in the catalina.bat file - and keep them as JAVA_OPTS.

Once tomcat has opened this port, connect to tomcat using this sample code -

  1. JMXServiceURL jurl = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");

  2. JMXConnector jmxc = JMXConnectorFactory.connect(jurl, null);

  3. MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();

This will connect to the tomcat server running on localhost with port 9999.

To fetch the value of an attribute - use the following code -

  1. mbsc.getAttribute(
    new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME, "VmVersion");

The above code will fetch the JVM version of the tomcat JVM using the standard runtime MBean of the JVM.