日期:2014-05-20 浏览次数:20708 次
package com.test.thread; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; public class ThreadThreshold { // mbean server connection private static MBeanServerConnection mbsc = null; /** * @param args */ public static void main(String[] args) { try { String jmxURL = "service:jmx:rmi:///jndi/rmi://localhost:8050/jmxrmi";//tomcat jmx url JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, null); mbsc = connector.getMBeanServerConnection(); ObjectName threadObjName = new ObjectName( "Catalina:type=ThreadPool,name=http-8080");//这里说明一下,端口最好是动态取得 MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName); String attrName = "currentThreadCount";//tomcat的线程数对应的属性值 MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes(); System.out.println("currentThreadCount:"+mbsc.getAttribute(threadObjName, attrName)); } catch (Exception e) { e.printStackTrace(); } } }