theomega
theomega

Reputation: 32031

Multiple Java Applications using JMX

I'm very new to JMX, and what I try to achieve is the following: I want to use JMX for monitoring multiple instances of the same Java-Application. The problem is, that this application might be run multiple times at the same time. I need the possibility to monitor the JMX values from another (remote) host.

Example Java-Application:

public class Test {
  public static void main(String[] args) {
    while(true) {
      try {
        Thread.sleep(1000);
        System.out.println("I'm running");
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
  }
}

Compiled using javac Test.java and then executed using

java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.local.only=false \
-Djava.net.preferIPv4Stack=true \
Test

The application now runs, but I do not know how to connect to this process from jconsole: I can use netstat to find out the port on which the JVM listens but I cannot connect because I receive a "no such object in table" exception when connecting.

If I run it using

java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.authenticate \
-Dcom.sun.management.jmxremote.ssl=false \
-Dcom.sun.management.jmxremote.local.only=false \
-Djava.net.preferIPv4Stack=true \
-Dcom.sun.management.jmxremote.port=1412 \
Test

, I can connect to 1.2.3.4:1412 using jconsole. But: I cannot run this application a second time without modifiying the port, which is quite logical as the application cannot bind to the port a second time.

How can I run the same application multiple times (with the same commandline!) and then connect to the multiple instances?

Upvotes: 1

Views: 6088

Answers (2)

Gray
Gray

Reputation: 116878

I can connect to 1.2.3.4:1412 using jconsole. But: I cannot run this application a second time without modifiying the port, which is quite logical as the application cannot bind to the port a second time.

Right each of your JVMs is going to have to live on a different port.

How can I run the same application multiple times (with the same commandline!) and then connect to the multiple instances?

By running them on a different port:

java -Dcom... -Dcom.sun.management.jmxremote.port=1412 Test
java -Dcom... -Dcom.sun.management.jmxremote.port=1413 Test

You can also use the port=0 to get a "dynamic" RMI port but I'm not sure how you are going to see that remotely.

You might consider using my SimpleJMX package which makes the whole publishing of JMX beans super easy. It also allows you to publish JMX to different ports easily and programably.

Upvotes: 1

Ben Roling
Ben Roling

Reputation: 103

See my answer on your other question: Enable JMX in Hadoop Job

You can set port=0 and then turn up logging such that you can see the port that was chosen as indicated here: https://forums.oracle.com/message/4798165#4798165

Upvotes: 1

Related Questions