示例代码
String jmxURL = "service:jmx:rmi:///jndi/rmi://192.168.10.93:8999/jmxrmi"; JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); Map map = new HashMap(); // 用户名密码,在jmxremote.password文件中查看 String[] credentials = new String[] { "monitorRole", "tomcat" }; map.put("jmx.remote.credentials", credentials); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map); MBeanServerConnection mbsc = connector.getMBeanServerConnection(); // 端口最好是动态取得 ObjectName threadObjName = new ObjectName("Catalina:type=ThreadPool,name=http-8080"); MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName); // tomcat的线程数对应的属性值 String attrName = "currentThreadCount"; MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes(); System.out.println("currentThreadCount:" + mbsc.getAttribute(threadObjName, attrName));
完整的示例代码文件
import java.lang.management.MemoryUsage; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Formatter; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.management.MBeanAttributeInfo; import javax.management.MBeanInfo; import javax.management.MBeanServerConnection; import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.management.openmbean.CompositeDataSupport; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; /** * @author liuyazhuang * @date 2016-05-31 */ public class JMXTest { /** * main方法 * @param args */ public static void main(String[] args) { try { String jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi"; JMXServiceURL serviceURL = new JMXServiceURL(jmxURL); Map map = new HashMap(); String[] credentials = new String[] { "monitorRole", "tomcat" }; map.put("jmx.remote.credentials", credentials); JMXConnector connector = JMXConnectorFactory.connect(serviceURL, map); MBeanServerConnection 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)); // heap for (int j = 0; j s = mbsc.queryNames(managerObjName, null); for (ObjectName obj : s) { System.out.println("应用名:" + obj.getKeyProperty("path")); ObjectName objname = new ObjectName(obj.getCanonicalName()); System.out.println("最大会话数:" + mbsc.getAttribute(objname, "maxActiveSessions")); System.out.println("会话数:" + mbsc.getAttribute(objname, "activeSessions")); System.out.println("活动会话数:" + mbsc.getAttribute(objname, "sessionCounter")); } // ----------------- Thread Pool ---------------- ObjectName threadpoolObjName = new ObjectName( "Catalina:type=ThreadPool,*"); Set s2 = mbsc.queryNames(threadpoolObjName, null); for (ObjectName obj : s2) { System.out.println("端口名:" + obj.getKeyProperty("name")); ObjectName objname = new ObjectName(obj.getCanonicalName()); System.out.println("最大线程数:" + mbsc.getAttribute(objname, "maxThreads")); System.out.println("当前线程数:" + mbsc.getAttribute(objname, "currentThreadCount")); System.out.println("繁忙线程数:" + mbsc.getAttribute(objname, "currentThreadsBusy")); } } catch (Exception e) { e.printStackTrace(); } } public static String formatTimeSpan(long span) { long minseconds = span % 1000; span = span / 1000; long seconds = span % 60; span = span / 60; long mins = span % 60; span = span / 60; long hours = span % 24; span = span / 24; long days = span; return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d", days, hours, mins, seconds, minseconds).toString(); } }
到此这篇关于使用JMX监控Tomcat示例代码的文章就介绍到这了,更多相关JMX监控Tomcat内容请搜索IT俱乐部以前的文章或继续浏览下面的相关文章希望大家以后多多支持IT俱乐部!