Java – how do I get the server port number when using a JMX server with a transient port?

When launching a Java application using the following options:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.port=0
-Dcom.sun.management.jmxremote.local.only=false

Java uses temporary ports, which are very useful for avoiding conflicts

Can I programmatically get the actual port (or connection URL) from the application?

Solution

String url = sun.management.ConnectorAddressLink.importRemoteFrom(0)
String url = sun.management.ConnectorAddressLink.importRemoteFrom(0)
            .get("sun.management.JMXConnectorServer.0.remoteAddress");
    System.out.println(url);

This will print the URL

service:jmx:rmi:///jndi/rmi://hostname:57025/jmxrmi
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>