Multiple Java Applications using JMX

筅森魡賤 提交于 2019-12-08 07:53:18

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/17812792/multiple-java-applications-using-jmx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!