tomcatjmxmbeans

Accessing built-in MBeans in Tomcat programmatically


Basically I'm trying to modify the code from this tutorial here: http://docs.oracle.com/javase/tutorial/jmx/remote/custom.html so that I can access the MBeans from tomcat that are described here: http://wiki.apache.org/tomcat/FAQ/Monitoring

there is no problem accessing the JMX Bean java.lang:type=Memory from code since it's interface is defined in java.lang. Here's the code example of that:

    ObjectName mbeanName = new ObjectName("java.lang:type=Memory");
    MemoryMXBean mxbeanProxy2 = JMX.newMXBeanProxy(mbsc, mbeanName, MemoryMXBean.class, true);
    MemoryUsage memUsage = mxbeanProxy2.getHeapMemoryUsage();
    echo("\nMemory Utilization: " + (memUsage.getUsed()/(double)memUsage.getMax()) * 100 +  "%");

Here the mbsc is an instance of MBeanServerConnection. The problem is that when I'm trying to access the built-in MBeans in tomcat in a similar way I run into the problem that I can't find any interface defined for any of the tomcat MBeans. I can monitor the MBeans from JConsole but for this I need to be able to do this from code. I found it somewhere that this could also done with something like this:

ObjectName mbeanName2 = new ObjectName("Catalina:type=ThreadPool,name=\"http-apr-8080\"");
Object value = mbsc.getAttribute(mbeanName, "name");

But this gives me this exception: Exception in thread "main" javax.management.AttributeNotFoundException: No such attribute: name at com.sun.jmx.mbeanserver......

I feel like I'm missing something fairly basic. But the information on this specifically seem to be very limited and google did not help much.


Solution

  • I think there's a typo in your second snippet of code. You created a new ObjectName for the Catalina ThreadPool called mbeanName2, but when you try to retrieve the attribute "name", you're still using mbeanName.

    So it should be:

    ObjectName mbeanName2 = new ObjectName("Catalina:type=ThreadPool,name=\"http-apr-8080\"");
    Object value = mbsc.getAttribute(mbeanName2, "name");
    

    Other than that, you code should work fine.