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自带监控工具,只需要找到 ...
随机推荐
- Ubuntu 下配置ftp服务端
安装sudo apt-get install pure-ftpdsudo netstat –ano | grep “21”查看21端口是否已开启.完成安装后,就可以用Ubuntu系统的用户名和密码登陆 ...
- Hibernate,JPA注解@SecondaryTable
使用类一级的 @SecondaryTable或@SecondaryTables注解可以实现单个实体到多个表的映射. 使用 @Column或者 @JoinColumn注解中的table参数可指定某个列所 ...
- Linux下资源利用率监测利器—nmon使用
最近接了一个任务,就是测试公司服务器的资源利用率,这样对每种业务平时所占资源有了大体的了解,进而为下一步的虚拟化做准备.找了很多开源的工具,但都不理想,有的安装很复杂,需要联网(而我们的服务器很多都不 ...
- zepto.js + iscroll.js上拉加载 下拉加载的 移动端 新闻列表页面
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- 工具项与菜单项实现相同的功能(DevExpress)
1.在工具栏中添加菜单项.如下:在bar1工具栏中添加菜单项this.barButtonMenuItem(包含图标) this.bar1.LinksPersistInfo.AddRange(new D ...
- 删除ecshop登录后台看到的系统信息
登陆ecshop后台,默认打开在页面顶部会出现个系统信息,显示操作系统,数据库版本,以及安装日期系统版本号.部分客户问到怎么删除,这里最模板提供ecshop教程告诉大家方法. 找到admin\temp ...
- Spring MVC 中请求返回之后的页面没法加载css、js等静态文件
1.是否被拦截,这个在Web.xml配置中servlet拦截是“/”,如果是则 a.使用spring MVC 的静态资源文件 <!-- 静态文件访问,主要是针对DispatcherServlet ...
- Python模块学习
6. Modules If you quit from the Python interpreter and enter it again, the definitions you have made ...
- 关于plsql表如何创建自增长列
1首先在sequence中创建新序列 在oracle中sequence就是所谓的序列号,每次取的时候它会自动增加,一般用在需要按序列号排序的地方. 这是语句创建 create sequence ide ...
- setprecision(int n)等格式函数用法 分类: POJ 2015-06-11 10:56 17人阅读 评论(0) 收藏
**这些用法前最好用 #include <iostream> //不要用iostream.h ,会出现好多问题 #include <iomanip> // io 流控制头 ...