OS:win7
jdk:1.6
Tomcat:7.0.26
1.Java的环境变量配置正确,网上很多,不再说了
2.修改Tomcat目录下的bin\catalina.bat
在该文件中查找set JAVA_OPTS=%JAVA_OPTS%,下面有一行为:
rem ----- Execute The Requested Command ---------------------------------------
在这一行的下面加
set JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
说明:9008为端口号,一会儿要用到。很多地方说tomcat需要权限验证,可是我这里没有用到,就可以连接成功,所以没有测试密码文件,仅将Dcom.sun.management.jmxremote.authenticate的值设为false。
3.记得编辑完bin\catalina.bat后,一定要双击执行。直接双击就可以,不要在cmd中执行,我执行出错(不知道为什么)
4.双击bin\startup.bat,最好双击执行,我在cmd中执行,无法启动Tomcat
5.在cmd中执行netstat -an,可查看到9008端口已经启用,说明Tomcat的Jmx配置成功
6.在cmd中敲入:jconsole(这个exe令在C:\Program Files\Java\jdk1.6.0_23\bin,即安装目录的bin下),即可启动jconsole的管理界面,在界面中输入如下内容:
service:jmx:rmi:///jndi/rmi://localhost:9008/jmxrmi
localhost:9008
即可进入管理界面,在管理界面的Tab页MBeans中,可以看到所有的MBeans
OS:xp
jdk:1.6
Tomcat:6.0.29
1.Java的环境变量配置正确,网上很多,不再说了
2.修改Tomcat目录下的bin\catalina.bat
在该文件中查找set JAVA_OPTS=%JAVA_OPTS%,下面有一行为:
rem ----- Execute The Requested Command ---------------------------------------
在这一行的下面加
set JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false
说明:9008为端口号,一会儿要用到。很多地方说tomcat需要权限验证,可是我这里没有用到,就可以连接成功,所以没有测试密码文件,仅将Dcom.sun.management.jmxremote.authenticate的值设为false。
3.记得编辑完bin\catalina.bat后,一定要双击执行。直接双击就可以,不要在cmd中执行,我执行出错(不知道为什么)
4.双击bin\startup.bat,最好双击执行,我在cmd中执行,无法启动Tomcat
5.在cmd中执行netstat -an,可查看到9008端口已经启用,说明Tomcat的Jmx配置成功
6.在cmd中敲入:jconsole(这个exe令在C:\Program Files\Java\jdk1.6.0_23\bin,即安装目录的bin下),即可启动jconsole的管理界面,在界面中输入如下内容:
service:jmx:rmi:///jndi/rmi://localhost:9008/jmxrmi
localhost:9008
即可进入管理界面,在管理界面的Tab页MBeans中,可以看到所有的MBeans
下面在代码中使用Jmx获取Tomcat中MBean的值
OS:xp jdk:1.6 Tomcat:6.0.29 1.Java的环境变量配置正确,网上很多,不再说了 2.修改Tomcat目录下的bin\catalina.bat 在该文件中查找set JAVA_OPTS=%JAVA_OPTS%,下面有一行为: rem ----- Execute The Requested Command --------------------------------------- 在这一行的下面加 set JAVA_OPTS=-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9008 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false 说明:9008为端口号,一会儿要用到。很多地方说tomcat需要权限验证,可是我这里没有用到,就可以连接成功,所以没有测试密码文件,仅将Dcom.sun.management.jmxremote.authenticate的值设为false。 3.记得编辑完bin\catalina.bat后,一定要双击执行。直接双击就可以,不要在cmd中执行,我执行出错(不知道为什么) 4.双击bin\startup.bat,最好双击执行,我在cmd中执行,无法启动Tomcat 5.在cmd中执行netstat -an,可查看到9008端口已经启用,说明Tomcat的Jmx配置成功 6.在cmd中敲入:jconsole(这个exe令在C:\Program Files\Java\jdk1.6.0_23\bin,即安装目录的bin下),即可启动jconsole的管理界面,在界面中输入如下内容: service:jmx:rmi:///jndi/rmi://localhost:9008/jmxrmi 或者 localhost:9008 即可进入管理界面,在管理界面的Tab页MBeans中,可以看到所有的MBeans 此时,Jmx监控Tomcat配置成功! 下面在代码中使用Jmx获取Tomcat中MBean的值 import java.util.*; import javax.management.*; import javax.management.remote.*;; public class JmxTest { public static void main(String[] args) { // TODO Auto-generated method stub try { --这里的url是在上图中输入的url JMXServiceURL url=new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:9008/jmxrmi"); //Map<String, String[]> map = new HashMap(); //String[] credentials=new String[] {"monitorRole","QED"}; //map.put("jmx.remote.credentials",credentials); JMXConnector conn=JMXConnectorFactory.connect(url); System.out.println("JMXConnector="+conn.toString()); String id=conn.getConnectionId(); System.out.println("Connection Id=" + id); MBeanServerConnection mbsc = conn.getMBeanServerConnection(); String domains[]=mbsc.getDomains(); System.out.println("# of domains="+domains.length); for (int i = 0; i < domains.length; i++) { System.out.println("Domain[" + i + "]=" + domains[i]); } Set<ObjectInstance> MBeanset = mbsc.queryMBeans(null, null); System.out.println("MBeanset.size() : " + MBeanset.size()); Iterator<ObjectInstance> MBeansetIterator = MBeanset.iterator(); while (MBeansetIterator.hasNext()) { ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next(); ObjectName objectName= objectInstance.getObjectName(); String canonicalName= objectName.getCanonicalName(); System.out.println("canonicalName : " + canonicalName); --在上图中,有Catalina:type=Server的port的值8005,运行程序后,在控制台能看到获取成功 if (objectName.toString().equals("Catalina:type=Server")) { // Get details of cluster MBeans String s = mbsc.getAttribute(objectName, "port").toString(); System.out.println("========================================="); System.out.println(s); System.out.println("========================================="); } //String canonicalKeyPropList=objectName.getCanonicalKeyPropertyListString(); } conn.close(); } catch(Exception ex) { System.out.println("Illegal Argument Exception: " + ex); } } }
下面是经过整理的代码,里面有一部分垃圾代码,大家可以选择性的看。
import java.math.BigDecimal; import java.util.*; import javax.management.*; import javax.management.remote.*; import javax.naming.Context; public class JmxTest { public static void main(String[] args) { JMXConnector conn = null; try { MBeanServerConnection mbsc = ConnJmx(conn); String KeyName = ""; String objectName = ""; String attributeName = ""; String attributeKey = ""; String value = ""; objectName = "java.lang:type=Memory"; attributeName = "NonHeapMemoryUsage"; attributeKey = "init"; value = GetValueByKey(objectName, attributeName, attributeKey); PrintContext(attributeName,value); KeyName = "进程数"; objectName = "java.lang:type=Threading"; attributeName = "ThreadCount"; attributeKey = ""; value = GetValueByKey(mbsc, objectName, attributeName, attributeKey); PrintContext(attributeName,value); KeyName = "正常运行时间"; objectName = "java.lang:type=Runtime"; attributeName = "Uptime"; attributeKey = ""; value = GetValueByKey(mbsc, objectName, attributeName, attributeKey); //System.out.println(attributeName + "=" + value); //value = ConvertMillToText(Long.parseLong(value)); PrintContext(attributeName,value); } catch(Exception ex) { System.out.println("Exception: " + ex); } finally { try { if (conn != null) { conn.close(); } } catch(Exception ex1){} } } private static void PrintContext(String key, String value) { System.out.println(key + "=" + value); } //从毫秒转至小时分钟 private static String ConvertMillToText(long millsecond)throws Exception { try { String s = ""; double d = (double)millsecond / (double)(1000*60*60); BigDecimal b = new BigDecimal(d); int hour = (int)Math.floor(d); double minute = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue(); if (hour > 0) { s = hour + " 小时 "; } minute = (minute - hour) * 60; b = new BigDecimal(minute); minute = b.setScale(0, BigDecimal.ROUND_HALF_UP).doubleValue(); s += (int)minute + " 分钟"; return s; } catch(Exception ex) { System.out.println("Exception: " + ex); throw ex; } } //连接到jmx private static MBeanServerConnection ConnJmx(JMXConnector conn)throws Exception { try { String _value = ""; JMXServiceURL url = new JMXServiceURL ("service:jmx:iiop:///jndi/corbaname::1.2@192.168.1.146:6888#jmx/rmi/RMIConnectorServer"); HashMap map = new HashMap(); String[] credentials=new String[] {"admin","admin"}; map.put("jmx.remote.credentials",credentials); conn = JMXConnectorFactory.connect(url, map); String id=conn.getConnectionId(); MBeanServerConnection mbsc = conn.getMBeanServerConnection(); return mbsc; } catch(Exception ex) { System.out.println("Illegal Argument Exception: " + ex); throw ex; } } //从MBeanServerConnection中取值 private static String GetValueByKey(MBeanServerConnection mbsc, String objectName, String attributeName, String attributeKey)throws Exception { try { String _value = ""; ObjectName _objectName = new ObjectName(objectName); if (attributeKey.equals("")) { _value = mbsc.getAttribute(_objectName, attributeName).toString(); } else { javax.management.openmbean.CompositeData data = (javax.management.openmbean.CompositeData)mbsc.getAttribute(_objectName, attributeName); _value = data.get(attributeKey).toString(); } return _value; } catch(Exception ex) { System.out.println("Illegal Argument Exception: " + ex); throw ex; } } private static String GetValueByKey(String objectName, String attributeName, String attributeKey)throws Exception { JMXConnector conn = null; try { String _value = ""; JMXServiceURL url=new JMXServiceURL ("service:jmx:iiop:///jndi/corbaname::1.2@192.168.1.146:6888#jmx/rmi/RMIConnectorServer"); HashMap map = new HashMap(); String[] credentials=new String[] {"admin","admin"}; map.put("jmx.remote.credentials",credentials); conn = JMXConnectorFactory.connect(url, map); String id=conn.getConnectionId(); MBeanServerConnection mbsc = conn.getMBeanServerConnection(); //Set<ObjectInstance> MBeanset = mbsc.queryMBeans("java.lang:type=Memory", null); //Set<ObjectInstance> MBeanset = mbsc.queryMBeans(null, null); ObjectName _objectName = new ObjectName(objectName); if (attributeKey.equals("")) { _value = mbsc.getAttribute(_objectName, attributeName).toString(); } else { javax.management.openmbean.CompositeData data = (javax.management.openmbean.CompositeData)mbsc.getAttribute(_objectName, attributeName); _value = data.get(attributeKey).toString(); } return _value; } catch(Exception ex) { System.out.println("Illegal Argument Exception: " + ex); throw ex; } finally { if (conn != null) { conn.close(); } } } private static void TestConn()throws Exception { JMXConnector conn = null; try { //JMXServiceURL url=new JMXServiceURL ("service:jmx:rmi:///jndi/rmi://localhost:9008/jmxrmi"); JMXServiceURL url=new JMXServiceURL ("service:jmx:iiop:///jndi/corbaname::1.2@192.168.1.146:6888#jmx/rmi/RMIConnectorServer"); HashMap map = new HashMap(); String[] credentials=new String[] {"admin","admin"}; map.put("jmx.remote.credentials",credentials); conn = JMXConnectorFactory.connect(url, map); System.out.println("JMXConnector="+conn.toString()); String id=conn.getConnectionId(); System.out.println("Connection Id=" + id); MBeanServerConnection mbsc = conn.getMBeanServerConnection(); String domains[]=mbsc.getDomains(); System.out.println("# of domains="+domains.length); for (int i = 0; i < domains.length; i++) { System.out.println("Domain[" + i + "]=" + domains[i]); } Set<ObjectInstance> MBeanset = mbsc.queryMBeans(null, null); System.out.println("MBeanset.size() : " + MBeanset.size()); Iterator<ObjectInstance> MBeansetIterator = MBeanset.iterator(); while (MBeansetIterator.hasNext()) { ObjectInstance objectInstance = (ObjectInstance)MBeansetIterator.next(); ObjectName objectName= objectInstance.getObjectName(); String canonicalName= objectName.getCanonicalName(); System.out.println("canonicalName : " + canonicalName); if (objectName.toString().equals("Catalina:type=Server")) { // Get details of cluster MBeans String s = mbsc.getAttribute(objectName, "port").toString(); System.out.println("========================================="); System.out.println(s); System.out.println("========================================="); } //String canonicalKeyPropList=objectName.getCanonicalKeyPropertyListString(); } conn.close(); } catch(Exception ex) { System.out.println("Illegal Argument Exception: " + ex); throw ex; } finally { if (conn != null) { conn.close(); } } } }