查找本地进程的jmx url的代码

简介: update: 2016-10-18在jdk7之后,有一个jcmd的工具,可以来启动jmx:ManagementAgent.stopManagementAgent.start_localManagementAgent.start参考:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html ---好久没写blog了,先来篇充充数。

update: 2016-10-18

在jdk7之后,有一个jcmd的工具,可以来启动jmx:

ManagementAgent.stop
ManagementAgent.start_local
ManagementAgent.start


参考:https://docs.oracle.com/javase/8/docs/technotes/guides/troubleshoot/tooldescr006.html 


---


好久没写blog了,先来篇充充数。。


当想用JMX连接本地进程,而这个进程又没有配置JMX相关的参数,怎样才能连到这个进程?

下面的代码是从ActiveMQ的代码里抠出来的,可以得到本地进程的jmx url。

不过当目标进程配置了-Djava.io.tmpdir 参数时,不能正常工作,原因是JDK的bug。

参考:

http://dikar.iteye.com/blog/1415408

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6649594


所以ActiveMQ5.8在用命令行时,用 "./activemq-admin list" ,得不出结果,改用"./activemq list "就可以了。

原因是"activemq-admin"这个脚本里,配置了一个java.io.tmpdir 的参数。

给ActiveMQ提了个issue,不过貌似没人理。。

https://issues.apache.org/jira/browse/AMQ-4541


import java.io.File;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.List;
import java.util.Properties;

public class AbstractJmxCommand  {
    private static final String CONNECTOR_ADDRESS =
        "com.sun.management.jmxremote.localConnectorAddress";

    public static String getJVM() {
        return System.getProperty("java.vm.specification.vendor");
    }

    public static boolean isSunJVM() {
        // need to check for Oracle as that is the name for Java7 onwards.
        return getJVM().equals("Sun Microsystems Inc.") || getJVM().startsWith("Oracle");
    }

    /**
     * Finds the JMX Url for a VM by its process id
     *
     * @param pid
     * 		The process id value of the VM to search for.
     *
     * @return the JMX Url of the VM with the given pid or null if not found.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected String findJMXUrlByProcessId(int pid) {

        if (isSunJVM()) {
            try {
                // Classes are all dynamically loaded, since they are specific to Sun VM
                // if it fails for any reason default jmx url will be used

                // tools.jar are not always included used by default class loader, so we
                // will try to use custom loader that will try to load tools.jar

                String javaHome = System.getProperty("java.home");
                String tools = javaHome + File.separator +
                        ".." + File.separator + "lib" + File.separator + "tools.jar";
                URLClassLoader loader = new URLClassLoader(new URL[]{new File(tools).toURI().toURL()});

                Class virtualMachine = Class.forName("com.sun.tools.attach.VirtualMachine", true, loader);
                Class virtualMachineDescriptor = Class.forName("com.sun.tools.attach.VirtualMachineDescriptor", true, loader);

                Method getVMList = virtualMachine.getMethod("list", (Class[])null);
                Method attachToVM = virtualMachine.getMethod("attach", String.class);
                Method getAgentProperties = virtualMachine.getMethod("getAgentProperties", (Class[])null);
                Method getVMId = virtualMachineDescriptor.getMethod("id",  (Class[])null);

                List allVMs = (List)getVMList.invoke(null, (Object[])null);

                for(Object vmInstance : allVMs) {
                    String id = (String)getVMId.invoke(vmInstance, (Object[])null);
                    if (id.equals(Integer.toString(pid))) {

                        Object vm = attachToVM.invoke(null, id);

                        Properties agentProperties = (Properties)getAgentProperties.invoke(vm, (Object[])null);
                        String connectorAddress = agentProperties.getProperty(CONNECTOR_ADDRESS);

                        if (connectorAddress != null) {
                            return connectorAddress;
                        } else {
                            break;
                        }
                    }
                }
            } catch (Exception ignore) {
            }
        }

        return null;
    }
}


相关文章
|
7月前
|
机器学习/深度学习 负载均衡 算法
计算机网络编程 | 并发服务器代码实现(多进程/多线程)
计算机网络编程 | 并发服务器代码实现(多进程/多线程)
87 0
|
10月前
Echarts实战案例代码(19):利用visualMap视觉映射组件制作五色玫瑰工作进程图
Echarts实战案例代码(19):利用visualMap视觉映射组件制作五色玫瑰工作进程图
153 0
|
2月前
|
安全 Linux 开发者
⭐⭐⭐⭐⭐Linux C/C++ 进程崩溃诊断以及有效数据收集:解锁代码问题快速定位与修复的方法
⭐⭐⭐⭐⭐Linux C/C++ 进程崩溃诊断以及有效数据收集:解锁代码问题快速定位与修复的方法
91 1
|
11天前
|
Linux Shell 调度
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
【Linux系列】fork( )函数原理与应用详解——了解【父子进程及其特性】(代码演示,画图帮助理解,思维导图,精简)(11)
|
19天前
|
C++
【C++】在使用代码组装URL时,一定要注意的坑......
【C++】在使用代码组装URL时,一定要注意的坑......
17 0
|
25天前
|
Linux Shell vr&ar
进程从代码到二进制到运行时的过程
【4月更文挑战第18天】Linux系统中二进制程序的格式,介绍了ELF(Executable and Linkable Format)格式。
|
2月前
|
消息中间件 并行计算 网络协议
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
探秘高效Linux C/C++项目架构:让进程、线程和通信方式助力你的代码飞跃
42 0
|
2月前
|
数据采集 调度 计算机视觉
3段代码详解python中的单线程、多线程和多进程
3段代码详解python中的单线程、多线程和多进程
25 0
|
9月前
|
调度
在 代码运行前加 strace 可以查看函数调度进程
在 代码运行前加 strace 可以查看函数调度进程
|
5月前
|
存储 算法 编译器
xv6(17) 进程三:代码部分
进程三:代码部分
88 0