是否可以使用JMX API获取tomcat服务器的内存使用情况统计信息。哪个Mbean可以向我提供此信息?我在下面的代码中停留在ObjectName的形成上
JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:2020/jmxrmi");
JMXConnector jmxc = JMXConnectorFactory.connect(url);
MBeanServerConnection server = jmxc.getMBeanServerConnection();
Object o = jmxc.getMBeanServerConnection().getAttribute(
new ObjectName("-----");
想知道jconsole如何绘制内存图,以及源代码的任何指针吗?
最佳答案
MBeanServer connection = ManagementFactory.getPlatformMBeanServer();
Set<ObjectInstance> set = connection.queryMBeans(new ObjectName("java.lang:type=Memory"), null);
ObjectInstance oi = set.iterator().next();
// replace "HeapMemoryUsage" with "NonHeapMemoryUsage" to get non-heap mem
Object attrValue = connection.getAttribute(oi.getObjectName(), "HeapMemoryUsage");
if( !( attrValue instanceof CompositeData ) ) {
System.out.println( "attribute value is instanceof [" + attrValue.getClass().getName() +
", exitting -- must be CompositeData." );
return;
}
// replace "used" with "max" to get max
System.out.println(((CompositeData)attrValue).get("used").toString());