JMX results are confusing

409 Views Asked by At

I am trying to learn JMX for the last few days and now got confuse here.

  1. I have written a simple JMX programe which is using the APIs of package java.lang.management and trying to extract the Pid, CPU time, user time. In my result I am only getting the results of current JVM thread which is my JMX programe itself but I thought I should get the result of all the processes running over JVM on the same machine. How I will get the pids, cpu time, user time for all java processes running in JVM(LINUX/WDs).
  2. How should I can get the pids, cpu time, user time for all non-java processes running in my machine(LINUX/WDs).

My code is below:

public void update() throws Exception{
    final ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    final long[] ids = bean.getAllThreadIds();
    final ThreadInfo[] infos = bean.getThreadInfo(ids);
    for (long id : ids) {
        if (id == threadId) {
            continue;   // Exclude polling thread
        }
        final long c = bean.getThreadCpuTime(id);
        final long u = bean.getThreadUserTime(id);
        if (c == -1 || u == -1) {
            continue;   // Thread died
        }
    }
    String name = null;
    for (int i = 0; i < infos.length; i++) {
        name = infos[i].getThreadName();
        System.out.print("The name of the id is /n" + name);
    }
}

I am always getting the result:

The name of the id is Attach Listener
The name of the id is Signal Dispatcher
The name of the id is Finalizer
The name of the id is Reference Handler
The name of the id is main

I have some other java processes running on my machine they are not been included in the results of bean.getAllThreadIds() API..

1

There are 1 best solutions below

4
On

Ah, now I see what you want to do. I'm afraid I have some bad news.

The APIs that are exposed through ManagementFactory allow you to monitor only the JVM in which your code is running. To monitor other JVMs, you have to use the JMX Remoting API (javax.management.remote), and that introduces a whole new range of issues you have to deal with.

It sounds like what you want to do is basically write your own management console using the stock APIs provided by out-of-the-box JDK. Short answer: you can't get there from here. Slightly longer answer: you can get there from here, but the road is long, winding, uphill (nearly) the entire way, and when you're done you will most likely wish you had gone a different route (read that: use a management console that has already been written).

I recommend you use JConsole or some other management console to monitor your application(s). In my experience it is usually only important that a human (not a program) interpret the stats that are provided by the various MBeans whose references are obtainable through the ManagementFactory static methods. After all, if a program had access to, say, the amount of CPU used by some other process, what conceivable use would it have with that information (other than to provide it in some human-readable format)?