简体   繁体   English

是否可以通过编程方式启用远程jmx监视?

[英]Is it possible to enable remote jmx monitoring programmatically?

I need to programmatically start a new java process and dynamically set the JMX port. 我需要以编程方式启动新的Java进程并动态设置JMX端口。 So instead of doing this 所以不要这样做

-Djava.rmi.server.hostname=127.0.0.1 -Dcom.sun.management.jmxremote.port=9995 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false

I would like to do the following 我想做以下

System.setProperty("java.rmi.server.hostname", "127.0.0.1" );
System.setProperty("com.sun.management.jmxremote", "true" );
System.setProperty("com.sun.management.jmxremote.authenticate", "false" );
System.setProperty("com.sun.management.jmxremote.ssl", "false" );
System.setProperty("com.sun.management.jmxremote.port", "9995"  );

but it doesn't work. 但这不起作用。 Any idea why? 知道为什么吗?

By the time your code is called you've missed your chance to configure the jmxremote connector. 在调用代码时,您已经错过了配置jmxremote连接器的机会。

What you need to do is create your own rmi registry and a JMXConnectorServer to listen for rmi calls and pass them to the MBeanServer. 您需要做的是创建您自己的rmi注册表和JMXConnectorServer来侦听rmi调用并将它们传递给MBeanServer。

private void createJmxConnectorServer() throws IOException {
    LocateRegistry.createRegistry(1234);
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    JMXServiceURL url = new JMXServiceURL("service:jmx:rmi://localhost/jndi/rmi://localhost:1234/jmxrmi");
    JMXConnectorServer svr = JMXConnectorServerFactory.newJMXConnectorServer(url, null, mbs);
    svr.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM