2

I have already googled and known to use the following options to enable debug a remote Java application.

-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044

However, my application will run in Yarn, and there may be several processes in one machine. So I cannot set a deterministic port in the java options due to the port conflicts. The pain of Yarn is that Yarn does not manage the port resources, so I don't know unused ports.

We have similar requirement for JMX. Fortunately, we can start JMX server in our application by ourselves, such as

  def start(port: Int): Int = {
    if (jmxConnectorServer != null) {
      // TODO log or throw error?
    }
    try {
      LocateRegistry.createRegistry(port);
    }
    catch {
      case e: java.rmi.server.ExportException if e.getCause != null && e.getCause.isInstanceOf[java.net.BindException] => {
        // TODO
        println(s"port $port already in use, try ${port + 1}")
        return start(port + 1)
      }
    }

    val env = new JHashMap[String, Any]()
    env.put("com.sun.management.jmxremote.authenticate", "false")
    env.put("com.sun.management.jmxremote.ssl", "false")

    val url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://:" + port + "/jmxrmi")
    val mBeanServer = ManagementFactory.getPlatformMBeanServer()
    jmxConnectorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, env, mBeanServer)
    jmxConnectorServer.start()

    port
  }

In the start method, we will try ports until we find an available one then return this port.

So I'm looking for similar APIs in JDWP. Thanks for your help in advance.

zsxwing
  • 20,270
  • 4
  • 37
  • 59

0 Answers0