Jconsole 监控tomcat
通过jconsole监控可以获取监控tomcat的相关的数据信息
如何通过代码来获取其中的线程和内存状况呢?
首先要配置好jconsole监控的相关配置,一搜基本就是那一个,
配置配不好的话接下来的工作就做不好了,所有要先配置好,然后可以上代码了:
1 package one; 2 3 import java.io.IOException; 4 import java.lang.management.MemoryMXBean; 5 import java.lang.management.MemoryUsage; 6 import java.lang.management.ThreadMXBean; 7 8 import java.lang.reflect.*; 9 import java.util.*; 10 11 import javax.management.*; 12 import javax.management.remote.*; 13 14 //mxbean_name constant 15 import static java.lang.management.ManagementFactory.*; 16 17 public class MyJconsole { 18 19 private SnapshotMBeanServerConnection server = null; 20 private String jmxURL = null; 21 private MBeanServerConnection mbsc = null; 22 private JMXConnector connector = null; 23 24 private JMXServiceURL serviceURL; 25 private Map<String, String[]> map; 26 private boolean hasPlatformMXBeans = false; 27 28 private ThreadMXBean threadMBean = null; 29 private MemoryMXBean memoryMBean = null; 30 31 public interface SnapshotMBeanServerConnection 32 extends MBeanServerConnection { 33 /** 34 * Flush all cached values of attributes. 35 */ 36 public void flush(); 37 } 38 39 public static class Snapshot { 40 private Snapshot() { 41 } 42 public static SnapshotMBeanServerConnection 43 newSnapshot(MBeanServerConnection mbsc) { 44 final InvocationHandler ih = new SnapshotInvocationHandler(mbsc); 45 return (SnapshotMBeanServerConnection) Proxy.newProxyInstance( 46 Snapshot.class.getClassLoader(), 47 new Class[] {SnapshotMBeanServerConnection.class}, 48 ih); 49 } 50 } 51 52 public synchronized ThreadMXBean getThreadMXBean() throws IOException { 53 if (hasPlatformMXBeans && threadMBean == null) { 54 threadMBean = 55 newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME, 56 ThreadMXBean.class); 57 } 58 return threadMBean; 59 } 60 61 public synchronized MemoryMXBean getMemoryMXBean() throws IOException { 62 if (hasPlatformMXBeans && memoryMBean == null) { 63 memoryMBean = 64 newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME, 65 MemoryMXBean.class); 66 } 67 return memoryMBean; 68 } 69 70 public MyJconsole(){ 71 jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi"; 72 map = new HashMap<String, String[]>(); 73 String[] credentials = new String[] { "monitorRole", "tomcat" }; 74 map.put("jmx.remote.credentials", credentials); 75 } 76 77 public void tryConnect() throws IOException { 78 try { 79 serviceURL = new JMXServiceURL(jmxURL); 80 connector = JMXConnectorFactory.connect(serviceURL,map); 81 mbsc = connector.getMBeanServerConnection(); 82 server = Snapshot.newSnapshot(mbsc); 83 ObjectName on = new ObjectName(THREAD_MXBEAN_NAME); 84 hasPlatformMXBeans = server.isRegistered(on); 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } 88 } 89 90 public static void main(String[] args){ 91 try { 92 MyJconsole mjc = new MyJconsole(); 93 mjc.tryConnect(); 94 ThreadMXBean tmBean = mjc.getThreadMXBean(); 95 MemoryMXBean memoryBean = mjc.getMemoryMXBean(); 96 int tlCount = tmBean.getThreadCount(); 97 int tdCount = tmBean.getDaemonThreadCount(); 98 int tpCount = tmBean.getPeakThreadCount(); 99 long ttCount = tmBean.getTotalStartedThreadCount(); 100 System.out.println("活动线程个数: "+tlCount); //当前线程数 101 System.out.println("峰值: " + tpCount); 102 System.out.println("守护线程数: " + tdCount); 103 System.out.println("启动线程数总数:" + ttCount); 104 MemoryUsage u = memoryBean.getHeapMemoryUsage(); 105 long memUsed = u.getUsed(); 106 long memMax = u.getMax(); 107 long memCommited = u.getCommitted(); 108 System.out.println("堆内存使用大小:"+ memUsed/1024+"Kb"); //堆内存; 109 System.out.println("当前堆大小: " + memUsed + "Kb"); 110 System.out.println("最大堆大小:" + memMax + "Kb"); 111 112 } catch (IOException e) { 113 e.printStackTrace(); 114 } 115 116 } 117 118 static class SnapshotInvocationHandler implements InvocationHandler { 119 120 private final MBeanServerConnection conn; 121 private Map<ObjectName, NameValueMap> cachedValues = newMap(); 122 private Map<ObjectName, Set<String>> cachedNames = newMap(); 123 124 @SuppressWarnings("serial") 125 private static final class NameValueMap 126 extends HashMap<String, Object> {} 127 128 SnapshotInvocationHandler(MBeanServerConnection conn) { 129 this.conn = conn; 130 } 131 132 synchronized void flush() { 133 cachedValues = newMap(); 134 } 135 136 public Object invoke(Object proxy, Method method, Object[] args) 137 throws Throwable { 138 final String methodName = method.getName(); 139 if (methodName.equals("getAttribute")) { 140 return getAttribute((ObjectName) args[0], (String) args[1]); 141 } else if (methodName.equals("getAttributes")) { 142 return getAttributes((ObjectName) args[0], (String[]) args[1]); 143 } else if (methodName.equals("flush")) { 144 flush(); 145 return null; 146 } else { 147 try { 148 return method.invoke(conn, args); 149 } catch (InvocationTargetException e) { 150 throw e.getCause(); 151 } 152 } 153 } 154 155 private Object getAttribute(ObjectName objName, String attrName) 156 throws MBeanException, InstanceNotFoundException, 157 AttributeNotFoundException, ReflectionException, IOException { 158 final NameValueMap values = getCachedAttributes( 159 objName, Collections.singleton(attrName)); 160 Object value = values.get(attrName); 161 if (value != null || values.containsKey(attrName)) { 162 return value; 163 } 164 // Not in cache, presumably because it was omitted from the 165 // getAttributes result because of an exception. Following 166 // call will probably provoke the same exception. 167 return conn.getAttribute(objName, attrName); 168 } 169 170 private AttributeList getAttributes( 171 ObjectName objName, String[] attrNames) throws 172 InstanceNotFoundException, ReflectionException, IOException { 173 final NameValueMap values = getCachedAttributes( 174 objName, 175 new TreeSet<String>(Arrays.asList(attrNames))); 176 final AttributeList list = new AttributeList(); 177 for (String attrName : attrNames) { 178 final Object value = values.get(attrName); 179 if (value != null || values.containsKey(attrName)) { 180 list.add(new Attribute(attrName, value)); 181 } 182 } 183 return list; 184 } 185 186 private synchronized NameValueMap getCachedAttributes( 187 ObjectName objName, Set<String> attrNames) throws 188 InstanceNotFoundException, ReflectionException, IOException { 189 NameValueMap values = cachedValues.get(objName); 190 if (values != null && values.keySet().containsAll(attrNames)) { 191 return values; 192 } 193 attrNames = new TreeSet<String>(attrNames); 194 Set<String> oldNames = cachedNames.get(objName); 195 if (oldNames != null) { 196 attrNames.addAll(oldNames); 197 } 198 values = new NameValueMap(); 199 final AttributeList attrs = conn.getAttributes( 200 objName, 201 attrNames.toArray(new String[attrNames.size()])); 202 for (Attribute attr : attrs.asList()) { 203 values.put(attr.getName(), attr.getValue()); 204 } 205 cachedValues.put(objName, values); 206 cachedNames.put(objName, attrNames); 207 return values; 208 } 209 210 // See http://www.artima.com/weblogs/viewpost.jsp?thread=79394 211 private static <K, V> Map<K, V> newMap() { 212 return new HashMap<K, V>(); 213 } 214 } 215 216 }