Java Tool Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 5.11

'com.sun.management.jmxremote' - JMX Agent for Local Connection

This section provides a tutorial example on how to turn on the out-of-the-box JMX agent on Sun JVM for monitoring tool 'jconsole' to connect on the local machine.

Since JDK 1.5, JMX has been included the Sun JVM. You can turn on the out-of-the-box JMX agent when you launch the JVM to run your application. This enables "jconsole" to connect to the JMX agent to monitor you Java application local or remotely.

In order to run your Java application and allow "jconsole" to monitor it on the local machine, you need to turn the "com.sun.management.jmxremote" system property with the "-D" option on the "java" command.

To this out, I modified the my PrimeNumberSeeker.java program with a higher ceiling value of 1000000 to let the loop running much longer:

/**
 * PrimeNumberSeeker.java
 * Copyright (c) 2008 by Dr. Herong Yang, http://www.herongyang.com/
 */
public class PrimeNumberSeeker extends Thread {
   private static final int ceiling = 1000000;
   private static final int interval = 1000;
   private static final int delay = 100;
   public int count = 0;
   public int current = 2;
   public int[] primes = null;
   public static void main(String[] a) {
      System.out.println("Period, Current int, # primes");
      PrimeNumberSeeker t = new PrimeNumberSeeker();
      t.start();
      int i = 0;
      while (true) {
         i++;
         System.out.println( i+", "+t.current+", "+t.count);
         try {
            sleep(interval);
         } catch (InterruptedException e) {
            System.out.println("Monitor interrupted.");
         }
      }
   }
   public void run() {
      primes = new int[ceiling];
      while (count < ceiling) {
         current++;
         int j = 2;
         boolean isPrime = true;
         while (j<current/2 && isPrime) {
            isPrime = current % j > 0;
            j++;
         }
         if (isPrime) {
            count++;
            primes[count-1] = current;
         }
         try {
            sleep(delay);
         } catch (InterruptedException e) {
            System.out.println("Runner interrupted.");
         }
      }
   }
}

To run this program with the out-of-the-box JMX agent turned on, I used the following commands:

C:\Progra~1\java\jdk1.6.0_02\bin\javac PrimeNumberSeeker.java

C:\Progra~1\java\jdk1.6.0_02\bin\java -Dcom.sun.management.jmxremote
   PrimeNumberSeeker

Period, Current int, # primes
1, 2, 0
2, 10, 4
...

Now my application PrimeNumberSeeker.java is running with the JMX agent turned on for local connection. See the next section on how to run "jconsole" to monitor this application.

Sections in This Chapter

JMX Technology and 'jconsole' Tool

'jconsole' Command Options and Connection Window

'com.sun.management.jmxremote' - JMX Agent for Local Connection

'jconsole' - Connecting to a Local JMX Agent

'com.sun.management.jmxremote.port' - JMX Agent for Remote Connection

'jconsole' - Connecting to a Remote JMX Agent

Dr. Herong Yang, updated in 2008
'com.sun.management.jmxremote' - JMX Agent for Local Connection