Is it possible to enable remote jmx monitoring programmatically?
Asked Answered
K

1

9

I need to programmatically start a new java process and dynamically set the JMX port. 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?

Kowal answered 16/12, 2014 at 15:35 Comment(3)
IMHO its not possible.Traduce
See this answer. You can still remotely monitor the JVM using Java Attach API if that is your goal.Escuage
Can I have an accepted answer?Arjun
A
19

By the time your code is called you've missed your chance to configure the jmxremote connector.

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.

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();
}
Arjun answered 18/3, 2016 at 12:58 Comment(3)
Great to know I've helped :)Arjun
careful, this will accept any password unless you do HashMap<String,Object> env = new HashMap<String,Object>(); env.put("jmx.remote.x.password.file", "path-to-password-file");, and pass that in when you create the JMXConnectorServer, where you currently have the null. Still, thanks so much for putting me on the right path!!Bonner
I've not been able to programmatically assign a JMX authenticator. Is there a way to inject it instead of using system properties?Gastronome

© 2022 - 2024 — McMap. All rights reserved.