How to get data via MBean

12,333

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

Share:
12,333
joe7pak
Author by

joe7pak

Just your average, everyday programmer trying to pay the mortgage. Lots of Java experience, threads/concurrency, sockets, JAXB, JMX, Web Services, in order to talk to satellites, cell phones, and radios. Also have created a few mini languages with JAXB/xml to solve various parsing problems when talking to said radio and satallite equipment. C#, C++, DB, Python are a few of my lesser skills. And mountain biking.

Updated on June 11, 2022

Comments

  • joe7pak
    joe7pak almost 2 years

    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

  • joe7pak
    joe7pak about 12 years
    Obvious it was. Mistake was not reading API docs carefully. Thanks for the help.
  • joe7pak
    joe7pak about 12 years
    I am accessing the Beans locally ... manager and the beans are all in the same JVM. I'll try the MBeanServer method as it looks much more direct. Thanks again.