jmx监控tomcat

  1. import java.lang.management.MemoryUsage;  
  2. import java.text.SimpleDateFormat;  
  3. import java.util.Date;  
  4. import java.util.Formatter;  
  5. import java.util.HashMap;  
  6. import java.util.Iterator;  
  7. import java.util.Map;  
  8. import java.util.Set;  
  9.   
  10. import javax.management.MBeanAttributeInfo;  
  11. import javax.management.MBeanInfo;  
  12. import javax.management.MBeanServerConnection;  
  13. import javax.management.ObjectInstance;  
  14. import javax.management.ObjectName;  
  15. import javax.management.openmbean.CompositeDataSupport;  
  16. import javax.management.remote.JMXConnector;  
  17. import javax.management.remote.JMXConnectorFactory;  
  18. import javax.management.remote.JMXServiceURL;  
  19.   
  20. public class JMXTest {  
  21.   
  22.     /** 
  23.      * @param args 
  24.      */  
  25.     public static void main(String[] args) {  
  26.         try {  
  27.   
  28.             String jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi";  
  29.   
  30.             JMXServiceURL serviceURL = new JMXServiceURL(jmxURL);  
  31.   
  32.             Map map = new HashMap();  
  33.             String[] credentials = new String[] { "monitorRole""tomcat" };  
  34.             map.put("jmx.remote.credentials", credentials);  
  35.             JMXConnector connector = JMXConnectorFactory.connect(serviceURL,  
  36.                     map);  
  37.             MBeanServerConnection mbsc = connector.getMBeanServerConnection();  
  38.   
  39.             // 端口最好是动态取得  
  40.             ObjectName threadObjName = new ObjectName(  
  41.                     "Catalina:type=ThreadPool,name=http-8080");  
  42.             MBeanInfo mbInfo = mbsc.getMBeanInfo(threadObjName);  
  43.   
  44.             String attrName = "currentThreadCount";// tomcat的线程数对应的属性值  
  45.             MBeanAttributeInfo[] mbAttributes = mbInfo.getAttributes();  
  46.             System.out.println("currentThreadCount:"  
  47.                     + mbsc.getAttribute(threadObjName, attrName));  
  48.   
  49.             // heap  
  50.             for (int j = 0; j < mbsc.getDomains().length; j++) {  
  51.                 System.out.println("###########" + mbsc.getDomains()[j]);  
  52.             }  
  53.             Set MBeanset = mbsc.queryMBeans(nullnull);  
  54.             System.out.println("MBeanset.size() : " + MBeanset.size());  
  55.             Iterator MBeansetIterator = MBeanset.iterator();  
  56.             while (MBeansetIterator.hasNext()) {  
  57.                 ObjectInstance objectInstance = (ObjectInstance) MBeansetIterator  
  58.                         .next();  
  59.                 ObjectName objectName = objectInstance.getObjectName();  
  60.                 String canonicalName = objectName.getCanonicalName();  
  61.                 System.out.println("canonicalName : " + canonicalName);  
  62.                 if (canonicalName  
  63.                         .equals("Catalina:host=localhost,type=Cluster")) {  
  64.                     // Get details of cluster MBeans  
  65.                     System.out.println("Cluster MBeans Details:");  
  66.                     System.out  
  67.                             .println("=========================================");  
  68.                     // getMBeansDetails(canonicalName);  
  69.                     String canonicalKeyPropList = objectName  
  70.                             .getCanonicalKeyPropertyListString();  
  71.                 }  
  72.             }  
  73.             // ------------------------- system ----------------------  
  74.             ObjectName runtimeObjName = new ObjectName("java.lang:type=Runtime");  
  75.             System.out.println("厂商:"  
  76.                     + (String) mbsc.getAttribute(runtimeObjName, "VmVendor"));  
  77.             System.out.println("程序:"  
  78.                     + (String) mbsc.getAttribute(runtimeObjName, "VmName"));  
  79.             System.out.println("版本:"  
  80.                     + (String) mbsc.getAttribute(runtimeObjName, "VmVersion"));  
  81.             Date starttime = new Date((Long) mbsc.getAttribute(runtimeObjName,  
  82.                     "StartTime"));  
  83.             SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
  84.             System.out.println("启动时间:" + df.format(starttime));  
  85.   
  86.             Long timespan = (Long) mbsc.getAttribute(runtimeObjName, "Uptime");  
  87.             System.out.println("连续工作时间:" + JMXTest.formatTimeSpan(timespan));  
  88.             // ------------------------ JVM -------------------------  
  89.             // 堆使用率  
  90.             ObjectName heapObjName = new ObjectName("java.lang:type=Memory");  
  91.             MemoryUsage heapMemoryUsage = MemoryUsage  
  92.                     .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,  
  93.                             "HeapMemoryUsage"));  
  94.             long maxMemory = heapMemoryUsage.getMax();// 堆最大  
  95.             long commitMemory = heapMemoryUsage.getCommitted();// 堆当前分配  
  96.             long usedMemory = heapMemoryUsage.getUsed();  
  97.             System.out.println("heap:" + (double) usedMemory * 100  
  98.                     / commitMemory + "%");// 堆使用率  
  99.   
  100.             MemoryUsage nonheapMemoryUsage = MemoryUsage  
  101.                     .from((CompositeDataSupport) mbsc.getAttribute(heapObjName,  
  102.                             "NonHeapMemoryUsage"));  
  103.             long noncommitMemory = nonheapMemoryUsage.getCommitted();  
  104.             long nonusedMemory = heapMemoryUsage.getUsed();  
  105.             System.out.println("nonheap:" + (double) nonusedMemory * 100  
  106.                     / noncommitMemory + "%");  
  107.   
  108.             ObjectName permObjName = new ObjectName(  
  109.                     "java.lang:type=MemoryPool,name=Perm Gen");  
  110.             MemoryUsage permGenUsage = MemoryUsage  
  111.                     .from((CompositeDataSupport) mbsc.getAttribute(permObjName,  
  112.                             "Usage"));  
  113.             long committed = permGenUsage.getCommitted();// 持久堆大小  
  114.             long used = heapMemoryUsage.getUsed();//  
  115.             System.out.println("perm gen:" + (double) used * 100 / committed  
  116.                     + "%");// 持久堆使用率  
  117.   
  118.             // -------------------- Session ---------------  
  119.             ObjectName managerObjName = new ObjectName(  
  120.                     "Catalina:type=Manager,*");  
  121.             Set<ObjectName> s = mbsc.queryNames(managerObjName, null);  
  122.             for (ObjectName obj : s) {  
  123.                 System.out.println("应用名:" + obj.getKeyProperty("path"));  
  124.                 ObjectName objname = new ObjectName(obj.getCanonicalName());  
  125.                 System.out.println("最大会话数:"  
  126.                         + mbsc.getAttribute(objname, "maxActiveSessions"));  
  127.                 System.out.println("会话数:"  
  128.                         + mbsc.getAttribute(objname, "activeSessions"));  
  129.                 System.out.println("活动会话数:"  
  130.                         + mbsc.getAttribute(objname, "sessionCounter"));  
  131.             }  
  132.   
  133.             // ----------------- Thread Pool ----------------  
  134.             ObjectName threadpoolObjName = new ObjectName(  
  135.                     "Catalina:type=ThreadPool,*");  
  136.             Set<ObjectName> s2 = mbsc.queryNames(threadpoolObjName, null);  
  137.             for (ObjectName obj : s2) {  
  138.                 System.out.println("端口名:" + obj.getKeyProperty("name"));  
  139.                 ObjectName objname = new ObjectName(obj.getCanonicalName());  
  140.                 System.out.println("最大线程数:"  
  141.                         + mbsc.getAttribute(objname, "maxThreads"));  
  142.                 System.out.println("当前线程数:"  
  143.                         + mbsc.getAttribute(objname, "currentThreadCount"));  
  144.                 System.out.println("繁忙线程数:"  
  145.                         + mbsc.getAttribute(objname, "currentThreadsBusy"));  
  146.             }  
  147.   
  148.         } catch (Exception e) {  
  149.             e.printStackTrace();  
  150.         }  
  151.     }  
  152.   
  153.     public static String formatTimeSpan(long span) {  
  154.         long minseconds = span % 1000;  
  155.   
  156.         span = span / 1000;  
  157.         long seconds = span % 60;  
  158.   
  159.         span = span / 60;  
  160.         long mins = span % 60;  
  161.   
  162.         span = span / 60;  
  163.         long hours = span % 24;  
  164.   
  165.         span = span / 24;  
  166.         long days = span;  
  167.         return (new Formatter()).format("%1$d天 %2$02d:%3$02d:%4$02d.%5$03d",  
  168.                 days, hours, mins, seconds, minseconds).toString();  
  169.     }  

你可能感兴趣的:(tomcat)