JMX客户端及对commons-pool的监控

import javax.management.MBeanServerConnection;

import javax.management.ObjectName;

import javax.management.remote.JMXConnector;

import javax.management.remote.JMXConnectorFactory;

import javax.management.remote.JMXServiceURL;

import com.sun.tools.attach.VirtualMachine;

import com.sun.tools.attach.VirtualMachineDescriptor;

public class JmxC {

public static void main(String[] args) {

if (args.length > 0) {

if (args[0].equals(“-l”)) {

JmxC c = new JmxC();

c.m1();

} else if (args[0].equals(“-p”)) {

int id = Integer.valueOf(args[1]);

System.out.println(“pid:” + id);

JmxC c = new JmxC();

if (args.length > 2 && args[2].equals(“-n”)) {

String name = args[3];

System.out.println(“args name:” + name);

if (args.length > 4 && args[4].equals(“-a”)) {

String atts = args[5];

System.out.println(“args atts:” + name);

c.processMbean(c.getConnectStrById(id), name, atts);

} else {

c.processMbean(c.getConnectStrById(id), name, null);

}

} else {

c.processMbean(c.getConnectStrById(id));

}

}

}

}

public void processMbean(String connectorAddress, String name, String att) {

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 beanSet = mbeanConn.queryNames(null, null);

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

ObjectName n = new ObjectName(name);

MBeanInfo info = mbeanConn.getMBeanInfo(n);

if (null != info) {

if (null != att && !att.isEmpty()) {

String[] as = att.split(“,”);

List al = new LinkedList();

System.out.print(" time " + “\t”);

for (String a : as) {

if (null != a && !a.isEmpty()) {

al.add(a);

System.out.print(a + “\t”);

}

}

System.out.println();

SimpleDateFormat dateformat1 = new SimpleDateFormat(

“HH:mm:ss”);

while (true) {

System.out.print(dateformat1.format(new Date()) + “\t”);

for (String a : al) {

Object value = null;

if (!a.contains(“-”)) {

value = mbeanConn.getAttribute(n, a);

} else {

String[] at = a.split(“-”);

value = (long) mbeanConn.getAttribute(n, at[0])

  • (long) mbeanConn.getAttribute(n,

at[1]);

}

System.out.print(value + “\t”);

}

System.out.println();

Thread.sleep(1000);

}

} else {

MBeanAttributeInfo[] atts = info.getAttributes();

for (MBeanAttributeInfo attr : atts) {

Object value = mbeanConn

.getAttribute(n, attr.getName());

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

}

}

} else {

System.err.println(“info is null”);

}

} 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 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 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 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 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”);

// 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);

最后

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。**

深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

[外链图片转存中…(img-Vy3i3vum-1715785444478)]

[外链图片转存中…(img-mFa3RhIE-1715785444479)]

[外链图片转存中…(img-sZZD9gg1-1715785444479)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!

如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值