Yes, you should create JMX connector programmatically.
As easier workaround, you can select another port for JMX in runtime if default port is busy by just killed process. OR just attempt opening your port again and again until it succeeds.
Here is code snippet i use to open JConsole-compatible JMX connector. In Scala, sorry, but you should be able to adapt it easily enough
def startJmx(port: Int): Unit = {
if (port < 1) {
return
}
val log = LoggerFactory.getLogger(getClass)
log.info("Starting JMX server connector on port {}", port)
val registry = LocateRegistry.createRegistry(port)
val server = ManagementFactory.getPlatformMBeanServer()
val url = new JMXServiceURL(s"service:jmx:rmi:///jndi/rmi://localhost:$port/jmxrmi")
val connectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, Collections.emptyMap(), server)
val thread = new Thread {
override def run = try {
connectorServer.start()
} catch {
case e: Exception => log.error("Unable to start JMX connector", e)
}
}
thread.setDaemon(true)
thread.setName("JMX connector Thread")
thread.start()
}
tcp 0 0 localhost:57525 localhost:3100 TIME_WAIT -
. There are also some questions about SO_REUSEADDR in general (like https://mcmap.net/q/103298/-what-is-the-meaning-of-so_reuseaddr-setsockopt-option-linux-duplicate/602119) which also mention TIME_WAIT in the answers. – FontesServerSocket.setReuseAddress(true)
? – Deprecatory