How to allocate a dynamic port and get information about it [JMX]
Requirement: I want to start multiple processes (with ieDcom.sun.management.jmxremote.port properties removed) that can be viewed remotely via JMX on another machine.
Initially Dcom.sun.management.jmxremote.port = 9875 (select from properties files), but it builds me to only start one process. Saving the port information from the computer (where I start the process) in a database for later use.
Then I used -Dcom.sun.management.jmxremote.port = 0 to dynamically allocate ports to run multiple processes. Now I can start several processes, but I cannot get the port information so I can save this information in a database that will be used later for remote connections.
Thank.
source to share
I used this trick to get a dynamic port
ServerSocket socket = new ServerSocket(0);
int jmxPort = socket.getLocalPort();
This will allocate a dynamic port and then pass it as VM args Dcom.sun.management.jmxremote.port = jmxPort
The jmx port can be stored in the database and can be used when a JMX connection needs to be made.
Hope it will be helpful.
source to share