Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get data via MBean

Tags:

java

jmx

I'm implementing a servlet as a JMX manager that runs in the same instance of Tomcat that all of the monitored servlets are running in. I can see the data of the monitored servlets when I open JConsole. From within my manager servlet I can enumerate all of the available standard MBeans, including the ones I've created in the monitored servlets, using this code like this:

JMXServiceURL url = new JMXServiceURL(        "service:jmx:rmi://localhost:1099/jndi/rmi://localhost:1099/jmxrmi" );

mConnector = JMXConnectorFactory.connect( url );
mMBSC = mConnector.getMBeanServerConnection();
mObjectName = new ObjectName( "com.blahCompany.blah.blah:type=BlahBlah" );

// just looking for one specific bean
Set<ObjectName> myMbean = mMBSC.queryNames( mObjectName, null );

if( myMBean.size() == 1 ) // I know it exists
{
     MBeanInfo mbeanInfo = mMBSC.getMBeanInfo( <ObjectName extracted from Set> );
     MBeanAttributeInfo[] mbeanAttributeInfos = mbeanInfo.getAttributes();

     for( MBeanAttributeInfo attribInfo : mbeanAttributeInfos )
     {
         if( attribInfo.isReadable() )
         {
             String attribName = attribInfo.getName();
             String attribReturnType = attribInfo.getType();

             // The data's somewhere ... where????
             // In the MBeanInfo?
             // In the MBeanAttributeInfo??
         }
     }
}

The problem is I don't know how to actually extract the data from these MBeans. The answer must be godawful obvious because no one else seems to have asked, but I do have a gift for overlooking the obvious. Your help will be gratefully appreciated.

Bill

like image 940
joe7pak Avatar asked Mar 05 '12 20:03

joe7pak


People also ask

What is an MBean server?

An MBean server is a repository of MBeans that provides management applications access to MBeans. Applications do not access MBeans directly, but instead access them through the MBean server via their unique ObjectName. An MBean server implements the interface javax. management. MBeanServer.

What is JMX MBean?

An MBean is a managed Java object, similar to a JavaBeans component, that follows the design patterns set forth in the JMX specification. An MBean can represent a device, an application, or any resource that needs to be managed.


1 Answers

All you need to do is something like the below:

Object value = mMBSC.getAttribute(objectName, attributeName);

Or create a proxy object that gets an instance of the MBean interface and allows you to access it that way. A tutorial on how to do this is here: http://docs.oracle.com/javase/tutorial/jmx/remote/custom.html

One note, this is assuming a remote connection, but from your question it seems your are accessing the beans locally? If that is the case then you can use platform.getMBeanServer() to get access to the MBeanServer more directly. E.g. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

like image 175
Trevor Freeman Avatar answered Oct 11 '22 01:10

Trevor Freeman