Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find dynamically allocated JMX port when port is 0

Tags:

java

jmx

For a java application when jmx is enabled and the port is set 0, a port will be dynamically allocated:

-Dcom.sun.management.jmxremote.port=0

What is the best way to find out what port is allocated? I managed to find it out using ps and pfiles on Solaris, hoping there is a simpler why to find it (programmatically)

Also is there a better way to assign dynamic jmx ports to java applications on the same box and keep track of them?

like image 850
user518066 Avatar asked Nov 12 '22 23:11

user518066


1 Answers

This question was answered here

String url = sun.management.ConnectorAddressLink.importRemoteFrom(0)
        .get("sun.management.JMXConnectorServer.0.remoteAddress");
String portStr = url.substring(url.lastIndexOf(":") + 1, url.lastIndexOf("/jmxrmi"));      
int port = Integer.valueOf(portStr);
System.out.println(port);
like image 78
Nikita Gorbachevski Avatar answered Nov 14 '22 21:11

Nikita Gorbachevski