joe7pak
joe7pak

Reputation: 300

How to get data via MBean

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

Upvotes: 6

Views: 13684

Answers (1)

Trevor Freeman
Trevor Freeman

Reputation: 7232

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();

Upvotes: 7

Related Questions