When using a JMX server with ephemeral port, how to get the server port number?

浪子不回头ぞ 提交于 2019-11-27 23:48:08

问题


When launching a Java application with these 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 an ephemeral port, which is very useful to avoid collisions.

Is it possible to get the actual port (or connection URL) programmatically from within the application ?


回答1:


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

This will print URL like

service:jmx:rmi:///jndi/rmi://hostname:57025/jmxrmi


来源:https://stackoverflow.com/questions/39344378/when-using-a-jmx-server-with-ephemeral-port-how-to-get-the-server-port-number

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!