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自带监控工具,只需要找到 ...
随机推荐
- Nagios监控Oralce
一.本文说明: 本文是监控本地的Oracle,其实监控远端的Oracle也是跟下面的步骤差不多的. 二.安装Nagios.Nagios插件.NRPE软件: 安装步骤可以参考<Linux下Nagi ...
- sqlite3把字段为int32(用c++的time(nullptr)获取的)的秒数显示为年月日时分秒
select id, type, msg, datetime(updateTime, 'unixepoch', 'localtime') from ServerLog
- Quartz2D简介及基本线条绘制
* Quartz2D简介 1.什么是Quartz2D? 他是一个二维的绘图引擎,同时支持iOS和Mac系统 2.Quartz2D能完成的工作 画基本线条,绘制文字,图片,截图,自定义UIView. 3 ...
- shell脚本中变量$$、$0等的含义
$0 这个程式的执行名字$n 这个程式的第n个参数值,n=1..9$* 这个程式的所有参数,此选项参数可超过9个.$# 这个程式的参数个数$$ 这个程式的PID(脚本运行的当前进程ID号)$! 执行上 ...
- java面试每日一题10
题目:利用递归方法求5! public class Recursion { public static void main(String args[]) throws NumberFormatExce ...
- myeclipse 常用快捷键总结
1 shift+enter 不管鼠标在当前行的什么位置,重新开启一行(向下) 2 shift+ctrl+enter 不管鼠标在当前行的什么位置,重新开启一行(向上) 3 Ctrl+D 删除一行 ...
- 【转】MYSQL入门学习之六:MYSQL的运算符
转载地址:http://www.2cto.com/database/201212/175862.html 一.算术运算符 1.加 www.2cto.com mysql> s ...
- 【转】学习总结--Cookie & Session总结
转载地址:http://www.phperzone.cn/portal.php?aid=718&mod=view 一.状态管理 1)什么是状态管理? 将浏览器与web服务器之间多次交互过程 ...
- android技术总结
1.要做一个尽可能流畅的ListView,你平时在工作中如何进行优化的? ①Item布局,层级越少越好,使用hierarchyview工具查看优化. ②复用convertView ③使用ViewHol ...
- c#之xml
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.X ...