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