How to get data through MBean

I implement the servlet as a JMX manager that runs on the same Tomcat instance as all monitored servlets. I can see the data of controlled servlets when opening JConsole. From my servlet manager, I can list all the available standard MBeans, including the ones that I created in controlled servlets, using this code as follows:

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 somewhere ... where???? // In the MBeanInfo? // In the MBeanAttributeInfo?? } } } 

The problem is that I do not know how to extract data from these MBeans. The answer should be obvious, because no one seems to be asking, but I have a gift to ignore the obvious. Your help would be greatly appreciated.

Bill

+4
source share
1 answer

All you have to do is something like below:

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

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

One note, this involves a remote connection, but from your question, it seems like you're accessing beans locally? If so, you can use platform.getMBeanServer () to directly access MBeanServer. For instance. MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();

+5
source

Source: https://habr.com/ru/post/1399869/


All Articles