根据进程ID连接JMX,代码测试_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 根据进程ID连接JMX,代码测试

根据进程ID连接JMX,代码测试

 2018/6/19 12:20:42  f543711700  程序员俱乐部  我要评论(0)
  • 摘要:importsun.misc.VM;importsun.misc.VMSupport;importjava.io.IOException;importjava.net.ServerSocket;/***CreatedbyDUDUon2018/6/12.*/publicclassServer{publicstaticvoidmain(String[]args)throwsInterruptedException{try{//ServerSocketss=newServerSocket(21000
  • 标签:测试 代码 连接
class="java" name="code">import sun.misc.VM;
import sun.misc.VMSupport;

import java.io.IOException;
import java.net.ServerSocket;

/**
 * Created by DUDU on 2018/6/12.
 */
public class Server {

    public static void main(String[] args) throws InterruptedException {
        try {
//            ServerSocket ss = new ServerSocket(21000);
//            String addr = System.getProperty("com.sun.management.jmxremote.localConnectorAddress");
            String addr = (String) VMSupport.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
            System.out.println("addr1:" + addr);
            if (addr == null) {
//                Thread.currentThread().getContextClassLoader().loadClass("");
//                Class.forName("sun.management.Agent");
                sun.management.Agent.premain("");
            }
            addr = (String) VMSupport.getAgentProperties().get("com.sun.management.jmxremote.localConnectorAddress");
            System.out.println(addr);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        Thread.sleep(50000000L);
    }

}

JVM在加载sun.management.Agent后,会监听一个地址,端口随机,我测试的时候是:
service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc1AAtVbmljYXN0UmVmMgAACjEwLjEuNy4yMTcAAMptncwWe1hOy/PhQ2ADAAABY/0c42+AAQB4

注意这时候的IP和端口实际是在后面加密的串里面,前面那个127.0.0.1是骗人的

所以客户端连接程序为:


JMXServiceURL jmxUrl = new JMXServiceURL("service:jmx:rmi://127.0.0.1/stub/rO0ABXNyAC5qYXZheC5tYW5hZ2VtZW50LnJlbW90ZS5ybWkuUk1JU2VydmVySW1wbF9TdHViAAAAAAAAAAICAAB4cgAaamF2YS5ybWkuc2VydmVyLlJlbW90ZVN0dWLp/tzJi+FlGgIAAHhyABxqYXZhLnJtaS5zZXJ2ZXIuUmVtb3RlT2JqZWN002G0kQxhMx4DAAB4cHc1AAtVbmljYXN0UmVmMgAACjEwLjEuNy4yMTcAAMptncwWe1hOy/PhQ2ADAAABY/0c42+AAQB4");
        JMXConnector jmxc = JMXConnectorFactory.connect(jmxUrl);

发表评论
用户名: 匿名