写点什么

JMX 客户端及对 commons-pool 的监控,java 工程师面试视频

用户头像
Java高工P7
关注
发布于: 刚刚

e.printStackTrace();


} finally {


try {


if (connector != null)


connector.close();


// break;


} catch (IOException e) {


// TODO Auto-generated catch block


e.printStackTrace();


}


}


// ...


}


public void processMbean(String connectorAddress) {


if (null == connectorAddress)


return;


JMXConnector connector = null;


try {


System.out.println("conn:" + connectorAddress);


JMXServiceURL url = new JMXServiceURL(connectorAddress);


connector = JMXConnectorFactory.connect(url);


MBeanServerConnection mbeanConn = connector


.getMBeanServerConnection();


Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);


System.out.println("beanSet num:" + beanSet.size());


for (ObjectName name : beanSet) {


if (name.getDomain().equals("org.apache.commons.pool2")) {


System.out.println("name:" + name);


// ObjectInstance instance =


// mbeanConn.getObjectInstance(name);


MBeanInfo info = mbeanConn.getMBeanInfo(name);


MBeanAttributeInfo[] atts = info.getAttributes();


for (MBeanAttributeInfo attr : atts) {


Object value = mbeanConn.getAttribute(name,


attr.getName());


System.out.println(attr.getName() + "->" + value);


}


System.out.println();


System.out.println();


System.out.println();


}


}


} catch (Exception e) {


// TODO Auto-generated catch block


e.printStackTrace();


} finally {


try {


if (connector != null)


connector.close();


// break;


} catch (IOException e) {


// TODO Auto-generated catch block


e.printStackTrace();


}


}


// ...


}


public String getConnectStrById(int pid) {


List<VirtualMachineDescriptor> vms = VirtualMachine.list();


int i = 0;


for (VirtualMachineDescriptor desc : vms) {


if (!desc.id().equals("" + pid)) {


continue;


}


VirtualMachine vm;


try {


System.out.println("desc:" + desc);


System.out.println("process id:" + desc.id());


vm = VirtualMachine.attach(desc);


} catch (Exception e) {


e.printStackTrace();


continue;


}


try {


Properties props = vm.getAgentProperties();


System.out.println("args:" + props.get("sun.jvm.args"));


for (Map.Entry<Object, Object> entry : props.entrySet()) {


// System.out.println(entry.getKey() + "->" +


// entry.getValue());


}


String connectorAddress = props


.getProperty("com.sun.management.jmxremote.localConnectorAddress");


if (connectorAddress == null) {


System.out


.println("connectorAddress is null,and continue search");


props = vm.getSystemProperties();


String home = props.getProperty("java.home");


// Normally in ${java.home}/jre/lib/management-agent.jar but


// might


// be in ${java.home}/lib in build environments.


String agent = home + File.separator + "jre"


  • File.separator + "lib" + File.separator

  • "management-agent.jar";


File f = new File(agent);


if (!f.exists()) {


agent = home + File.separator + "lib" + File.separator


  • "management-agent.jar";


f = new File(agent);


if (!f.exists()) {


throw new IOException("Management agent not found");


}


}


agent = f.getCanonicalPath();


vm.loadAgent(agent, "com.sun.management.jmxremote");


props = vm.getAgentProperties();


connectorAddress = props


.getProperty("com.sun.management.jmxremote.localConnectorAddress");


if (connectorAddress == null) {


System.out.println("connectorAddress is null");


}


}


return connectorAddress;


} catch (Exception e) {


e.printStackTrace();


}


}


System.out.println("return null");


return null;


}


public void m1() {


List<VirtualMachineDescriptor> vms = VirtualMachine.list();


int i = 0;


for (VirtualMachineDescriptor desc : vms) {


System.out.println();


System.out.println();


System.out.println("------" + i++ + "----------------");


VirtualMachine vm;


try {


System.out.println("desc:" + desc);


System.out.println("process id:" + desc.id());


vm = VirtualMachine.attach(desc);


} catch (Exception e) {


e.printStackTrace();


continue;


}


JMXConnector connector = null;


try {


Properties props = vm.getAgentProperties();


System.out.println(props.get("sun.jvm.args"));


for (Map.Entry<Object, Object> entry : props.entrySet()) {


// System.out.println(entry.getKey() + "->" +


// entry.getValue());


}


String connectorAddress = props


.getProperty("com.sun.management.jmxremote.localConnectorAddress");


if (connectorAddress == null) {


System.out


.println("connectorAddress is null,and continue search");


props = vm.getSystemProperties();


String home = props.getProperty("java.home"


《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》
浏览器打开:qq.cn.hn/FTe 免费领取
复制代码


);


// Normally in ${java.home}/jre/lib/management-agent.jar but


// might


// be in ${java.home}/lib in build environments.


String agent = home + File.separator + "jre"


  • File.separator + "lib" + File.separator

  • "management-agent.jar";


File f = new File(agent);


if (!f.exists()) {


agent = home + File.separator + "lib" + File.separator


  • "management-agent.jar";


f = new File(agent);


if (!f.exists()) {


throw new IOException("Management agent not found");


}


}


agent = f.getCanonicalPath();


vm.loadAgent(agent, "com.sun.management.jmxremote");


props = vm.getAgentProperties();


connectorAddress = props


.getProperty("com.sun.management.jmxremote.localConnectorAddress");


if (connectorAddress == null) {


System.out.println("connectorAddress is null");


continue;


}


}


System.out.println("conn:" + connectorAddress);


JMXServiceURL url = new JMXServiceURL(connectorAddress);


connector = JMXConnectorFactory.connect(url);


MBeanServerConnection mbeanConn = connector


.getMBeanServerConnection();


Set<ObjectName> beanSet = mbeanConn.queryNames(null, null);


System.out.println("beanSet num:" + beanSet.size());


// ...


} catch (Exception e) {


e.printStackTrace();


} finally {


try {


if (connector != null)


connector.close();


// break;


} catch (IOException e) {


// TODO Auto-generated catch block


e.printStackTrace();


}


}


}


}


}


将上述代码编译,如果使用 javac 命令编译出错的话,建议先拷贝到 eclipse,然后直接拿到编译后的.class 文件。


2、使用上述工具来监控 commons-pool


1)首先我们将编译后的 class 文件存放在



然后我们先 cmd 进入到 jedis 目录:


2.1 先执行 java ?cn/uce/uop/jmx/JmxC -l




解决办法:


将 %JAVA_HOME%/jre/bin/attach.dll 拷贝到 %JAVA_HOME%/bin/目录下


再执行上述命令:




会列出当前的 java 进程,我们选择我们需要监控的进程,执行如下命令


2.2 javacn/uce/uop/jmx/JmxC -p 13260


pid:13260


desc:sun.tools.attach.WindowsAttachProvider@4617c264: 13260 cn.uce.uop.Test


process id:13260


args:-Dfile.encoding=UTF-8


conn:service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc2AAtVbmljYXN0UmVmMgAACzEwLjIwMS4yLjYzAADh5wsYyZlaKbJNg8lsRAAAAWC1Eqi1gAEAeA==


beanSet num:20


name:org.apache.commons.pool2:type=GenericObjectPool,name=pool


NumActive->30


NumIdle->0


NumWaiters->1


TimeBetweenEvictionRunsMillis->30000


Fairness->false


Lifo->true


Closed->false


MaxTotal->30


MaxIdle->5


MinIdle->0


TestOnCreate->false


TestOnReturn->false


TestWhileIdle->true


NumTestsPerEvictionRun->-1


MinEvictableIdleTimeMillis->60000


TestOnBorrow->false


BlockWhenExhausted->true


MaxWaitMillis->-1


DestroyedByEvictorCount->0


MeanIdleTimeMillis->0


CreatedCount->30


DestroyedCount->0


CreationStackTrace->java.lang.Exception


at org.apache.commons.pool2.impl.BaseGenericObjectPool.<init>(BaseGenericObjectPool.java:139)


at org.apache.commons.pool2.impl.GenericObjectPool.<init>(GenericObjectPool.java:107)


at redis.clients.util.Pool.initPool(Pool.java:44)


at redis.clients.util.Pool.<init>(Pool.java:23)


at redis.clients.jedis.JedisPool.<init>(JedisPool.java:185)


at redis.clients.jedis.JedisPool.<init>(JedisPool.java:162)


at redis.clients.jedis.JedisPool.<init>(JedisPool.java:109)


at cn.uce.uop.Test.main(Test.java:19)


BorrowedCount->38


ReturnedCount->8


MeanActiveTimeMillis->0


DestroyedByBorrowValidationCount->0


RemoveAbandonedOnMaintenance->false


MaxBorrowWaitTimeMillis->41


LogAbandoned->false


AbandonedConfig->false


RemoveAbandonedOnBorrow->false


RemoveAbandonedTimeout->2147483647


MeanBorrowWaitTimeMillis->1


上述会列出通过 jmx 能监控的 MBean 属性

用户头像

Java高工P7

关注

还未添加个人签名 2021.11.08 加入

还未添加个人简介

评论

发布
暂无评论
JMX客户端及对commons-pool的监控,java工程师面试视频