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:
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.