……天天向上

好的想法总是无穷无尽

统计

留言簿(1)

阅读排行榜

评论排行榜

TOMCAT获取信息:JMXServiceURL 、JMXConnector 和MBeanServerConnection

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

  1. import java.util.Iterator;
  2. import java.util.Set;
  3. import javax.management.Attribute;
  4. import javax.management.MBeanInfo;
  5. import javax.management.MBeanServerConnection;
  6. import javax.management.MBeanServerInvocationHandler;
  7. import javax.management.ObjectInstance;
  8. import javax.management.ObjectName;
  9. import javax.management.remote.JMXConnector;
  10. import javax.management.remote.JMXConnectorFactory;
  11. import javax.management.remote.JMXServiceURL;
  12. public class Client {
  13. public static void main(String[] args) throws Exception {
  14. JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/server");
  15. JMXConnector jmxc = JMXConnectorFactory.connect(url, null);
  16. MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  17. ObjectName mbeanName = new ObjectName("chengang:name=HelloWorld");
  18. // 把所有Domain都打印出来
  19. System.out.println("Domains:---------------");
  20. String domains[] = mbsc.getDomains();
  21. for (int i = 0; i < domains.length; i++) {
  22. System.out.println("\tDomain[" + i + "] = " + domains[i]);
  23. }
  24. // MBean的总数
  25. System.out.println("MBean count = " + mbsc.getMBeanCount());
  26. // 对name属性的操作(属性名的第一个字母要大写)
  27. mbsc.setAttribute(mbeanName, new Attribute("Name", "PANDA"));// 设值
  28. System.out.println("Name = " + mbsc.getAttribute(mbeanName, "Name"));// 取值
  29. // 得到proxy代理后直接调用的方式
  30. HelloMBean proxy = (HelloMBean) MBeanServerInvocationHandler.newProxyInstance(mbsc, mbeanName, HelloMBean.class, false);
  31. proxy.printHello();
  32. proxy.printHello("Raymend");
  33. // 远程调用的方式
  34. mbsc.invoke(mbeanName, "printHello", null, null);
  35. mbsc.invoke(mbeanName, "printHello", new Object[] { "熊猫烧香" }, new String[] { String.class.getName() });
  36. // 得mbean的信息
  37. MBeanInfo info = mbsc.getMBeanInfo(mbeanName);
  38. System.out.println("Hello Class: " + info.getClassName());
  39. System.out.println("Hello Attriber:" + info.getAttributes()[0].getName());
  40. System.out.println("Hello Operation:" + info.getOperations()[0].getName());
  41. // 得到所有的MBean的ObjectName
  42. System.out.println("all ObjectName:---------------");
  43. Set set = mbsc.queryMBeans(null, null);
  44. for (Iterator it = set.iterator(); it.hasNext();) {
  45. ObjectInstance oi = (ObjectInstance) it.next();
  46. System.out.println("\t" + oi.getObjectName());
  47. }
  48. // 关闭MBeanServer连接
  49. jmxc.close();
  50. }
  51. }

posted on 2012-09-05 14:14 japper 阅读(4983) 评论(0)  编辑  收藏 所属分类: Java


只有注册用户登录后才能发表评论。


网站导航: