Jconsole 监控tomcat
通过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>();
}
} }
Jconsole 监控tomcat的更多相关文章
- jconsole监控tomcat
一.专业术语 GC垃圾回收机制:当需要分配的内存空间不再使用的时候,JVM将调用垃圾回收机制来回收内存空间. JMX(Java Management Extensions,即Java管理扩展)是一个为 ...
- 通过JCONSOLE监控TOMCAT的JVM使用情况
这个也是要学入一下,JVMr 虚拟机原理不可少. 参考配置URL“: http://blog.163.com/kangle0925@126/blog/static/277581982011527723 ...
- 使用jconsole监控tomcat(推荐配置)
1.在tomcat启动过程中,开启相应的参数配置 $Tomcat_home/bin/catalina.sh: 1 2 3 4 -Dcom.sun.management.jmxremote -Dcom. ...
- 通过jconsole监控tomcat JVM 内存、线程、CPU
从Java 5开始 引入了 JConsole,来监控 Java 应用程序性能和跟踪 Java 中的代码.jconsole是JDK自带监控工具,只需要找到 JDK 安装路径,打开 bin 文件夹,双击 ...
- Jconsole监控tomcat 的JVM内存的设置
主要参考这位仁兄的文章 http://elf8848.iteye.com/blog/471676 照做后发现还是不行,原来是Linux服务器配置了多块网卡,在设置 Djava.rmi.server. ...
- LoadRunner监控Tomcat的几种方法
通过JConsole监控Tomcat 1.打开tomcat5的bin目录中的catalina.bat文件,在头部注释部分的后面加上: set JAVA_OPTS=%JAVA_OPTS% -Dcom.s ...
- JConsole监控远程Tomcat服务器
为了解决内存溢出的问题,会用到一些监视内存的工具,jconsole这个工具在jdk1.7自带了.这个工具可以查看系统的堆,非堆,线程,等等的一些整体的情况,从而可以判断出系统的一个大概的性能情况. c ...
- 如何使用JCONSOLE 监控eclipse的tomcat
在默认情况下,使用jconsole 监控本地tomcat 是不需要任何配置的,直接连接就可以监控tomcat. 但是在eclipse 下启动是监控不了. 解决方法: 设置jvm参数: ...
- JConsole监控Linux上的Tomcat
JConsole监控Linux上的Tomcat 从Java 5开始引入了 JConsole,来监控 Java 应用程序性能和跟踪 Java 中的代码.jconsole是JDK自带监控工具,只需要找到 ...
随机推荐
- java.lang.Runtime类总结 【转】
转自:http://blog.chinaunix.net/uid-128922-id-289994.html Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类 ...
- Docker 端口映射问题解决
在操作Docker容器时发现了其一个端口映射的BUG,具体表现为:开启容器时做了端口映射80:8080,即宿主机的80端口映射到容器内部的8080Jboss端口.一开始测试也没有什么问题,都可以联通, ...
- pgadmin(IDE)工具连接postgres数据库
1. 下载软件 软件地址:http://www.pgadmin.org/download/pgagent.php 2.安装软件 安装过程:略 打开软件64位会出现 “无 ...
- [thml]HTML select标签 获取选中的option的value及Text内容
很简单的select标签: <select id="hello" onchange="getContent(this.value,this.options[this ...
- data-属性
html5中出现data标签,该标签可以为div,p,span,td等各种标签提供属性 <div id="button" data-mm='{"name" ...
- 用ThreadLocal为线程生成唯一标识及实现原理
1.在多线程编程中,有时候需要自动为每个启动的线程生成一个唯一标识,这个时候,通过一个ThreadLocal变量来保存每个线程的标识是最有效.最方便的方式了. 2.ThreadLocal 实例通常是类 ...
- 20151124002 treeView 数型菜单的操作
20151124002 treeView 数型菜单的操作 protected void FillTree() { SqlConnection1 = new Syst ...
- GUI_Delay函数
GUI_Delay()函数 使用GUI_Delay()函数时,对于其延时时间不确定,明明设置为最小值1,延时时间 仍旧太长,不能达到需求.遂决定研究明白其实现机理. uC/OS-II使用OSTimeD ...
- LAMMP架构的企业级应用
LAMMP架构的企业级应用 ========================================= LAMMP是什么 LAMMP的实现 LAMMP适用的生产环境 ============= ...
- 改变CSS样式
改变CSS样式 1.改变HTML元素样式的语法 //改变HTML样式的语法 document.getElementById(id).style.property = new style 例子: < ...