通过jconsole监控可以获取监控tomcat的相关的数据信息
如何通过代码来获取其中的线程和内存状况呢?
首先要配置好jconsole监控的相关配置,一搜基本就是那一个,
配置配不好的话接下来的工作就做不好了,所有要先配置好,然后可以上代码了:
package one; import java.io.IOException;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.lang.management.ThreadMXBean; import java.lang.reflect.*;
import java.util.*; import javax.management.*;
import javax.management.remote.*; //mxbean_name constant
import static java.lang.management.ManagementFactory.*; public class MyJconsole { private SnapshotMBeanServerConnection server = null;
private String jmxURL = null;
private MBeanServerConnection mbsc = null;
private JMXConnector connector = null; private JMXServiceURL serviceURL;
private Map<String, String[]> map;
private boolean hasPlatformMXBeans = false; private ThreadMXBean threadMBean = null;
private MemoryMXBean memoryMBean = null; public interface SnapshotMBeanServerConnection
extends MBeanServerConnection {
/**
* Flush all cached values of attributes.
*/
public void flush();
} public static class Snapshot {
private Snapshot() {
}
public static SnapshotMBeanServerConnection
newSnapshot(MBeanServerConnection mbsc) {
final InvocationHandler ih = new SnapshotInvocationHandler(mbsc);
return (SnapshotMBeanServerConnection) Proxy.newProxyInstance(
Snapshot.class.getClassLoader(),
new Class[] {SnapshotMBeanServerConnection.class},
ih);
}
} public synchronized ThreadMXBean getThreadMXBean() throws IOException {
if (hasPlatformMXBeans && threadMBean == null) {
threadMBean =
newPlatformMXBeanProxy(server, THREAD_MXBEAN_NAME,
ThreadMXBean.class);
}
return threadMBean;
} public synchronized MemoryMXBean getMemoryMXBean() throws IOException {
if (hasPlatformMXBeans && memoryMBean == null) {
memoryMBean =
newPlatformMXBeanProxy(server, MEMORY_MXBEAN_NAME,
MemoryMXBean.class);
}
return memoryMBean;
} public MyJconsole(){
jmxURL = "service:jmx:rmi:///jndi/rmi://127.0.0.1:8999/jmxrmi";
map = new HashMap<String, String[]>();
String[] credentials = new String[] { "monitorRole", "tomcat" };
map.put("jmx.remote.credentials", credentials);
} public void tryConnect() throws IOException {
try {
serviceURL = new JMXServiceURL(jmxURL);
connector = JMXConnectorFactory.connect(serviceURL,map);
mbsc = connector.getMBeanServerConnection();
server = Snapshot.newSnapshot(mbsc);
ObjectName on = new ObjectName(THREAD_MXBEAN_NAME);
hasPlatformMXBeans = server.isRegistered(on);
} catch (Exception e) {
e.printStackTrace();
}
} public static void main(String[] args){
try {
MyJconsole mjc = new MyJconsole();
mjc.tryConnect();
ThreadMXBean tmBean = mjc.getThreadMXBean();
MemoryMXBean memoryBean = mjc.getMemoryMXBean();
int tlCount = tmBean.getThreadCount();
int tdCount = tmBean.getDaemonThreadCount();
int tpCount = tmBean.getPeakThreadCount();
long ttCount = tmBean.getTotalStartedThreadCount();
System.out.println("活动线程个数: "+tlCount); //当前线程数
System.out.println("峰值: " + tpCount);
System.out.println("守护线程数: " + tdCount);
System.out.println("启动线程数总数:" + ttCount);
MemoryUsage u = memoryBean.getHeapMemoryUsage();
long memUsed = u.getUsed();
long memMax = u.getMax();
long memCommited = u.getCommitted();
System.out.println("堆内存使用大小:"+ memUsed/1024+"Kb"); //堆内存;
System.out.println("当前堆大小: " + memUsed + "Kb");
System.out.println("最大堆大小:" + memMax + "Kb"); } catch (IOException e) {
e.printStackTrace();
} } static class SnapshotInvocationHandler implements InvocationHandler { private final MBeanServerConnection conn;
private Map<ObjectName, NameValueMap> cachedValues = newMap();
private Map<ObjectName, Set<String>> cachedNames = newMap(); @SuppressWarnings("serial")
private static final class NameValueMap
extends HashMap<String, Object> {} SnapshotInvocationHandler(MBeanServerConnection conn) {
this.conn = conn;
} synchronized void flush() {
cachedValues = newMap();
} public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
final String methodName = method.getName();
if (methodName.equals("getAttribute")) {
return getAttribute((ObjectName) args[0], (String) args[1]);
} else if (methodName.equals("getAttributes")) {
return getAttributes((ObjectName) args[0], (String[]) args[1]);
} else if (methodName.equals("flush")) {
flush();
return null;
} else {
try {
return method.invoke(conn, args);
} catch (InvocationTargetException e) {
throw e.getCause();
}
}
} private Object getAttribute(ObjectName objName, String attrName)
throws MBeanException, InstanceNotFoundException,
AttributeNotFoundException, ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(
objName, Collections.singleton(attrName));
Object value = values.get(attrName);
if (value != null || values.containsKey(attrName)) {
return value;
}
// Not in cache, presumably because it was omitted from the
// getAttributes result because of an exception. Following
// call will probably provoke the same exception.
return conn.getAttribute(objName, attrName);
} private AttributeList getAttributes(
ObjectName objName, String[] attrNames) throws
InstanceNotFoundException, ReflectionException, IOException {
final NameValueMap values = getCachedAttributes(
objName,
new TreeSet<String>(Arrays.asList(attrNames)));
final AttributeList list = new AttributeList();
for (String attrName : attrNames) {
final Object value = values.get(attrName);
if (value != null || values.containsKey(attrName)) {
list.add(new Attribute(attrName, value));
}
}
return list;
} private synchronized NameValueMap getCachedAttributes(
ObjectName objName, Set<String> attrNames) throws
InstanceNotFoundException, ReflectionException, IOException {
NameValueMap values = cachedValues.get(objName);
if (values != null && values.keySet().containsAll(attrNames)) {
return values;
}
attrNames = new TreeSet<String>(attrNames);
Set<String> oldNames = cachedNames.get(objName);
if (oldNames != null) {
attrNames.addAll(oldNames);
}
values = new NameValueMap();
final AttributeList attrs = conn.getAttributes(
objName,
attrNames.toArray(new String[attrNames.size()]));
for (Attribute attr : attrs.asList()) {
values.put(attr.getName(), attr.getValue());
}
cachedValues.put(objName, values);
cachedNames.put(objName, attrNames);
return values;
} // See http://www.artima.com/weblogs/viewpost.jsp?thread=79394
private static <K, V> Map<K, V> newMap() {
return new HashMap<K, V>();
}
} }