Java JMX tutorial - A "Hello world" JMX application

Java JMX FAQ: Can you share a simple Java JMX example?

Here is some sample Java source code for some JMX tests that I created recently. I got a large percentage of this code from Sun's JMX MBean tutorial when I first started working with JMX, and I think it's some decent "Hello World" starter code. The main thing I've done here is (a) clarify what they've written and (b) added a shell script to start the JMX application. (I'm also about to publish some other JMX source code that I know I wrote myself.)

Our JMX MBean interface

First, here's the source code for a Java MBean interface named HelloMBean:

public interface HelloMBean {
    public void setMessage(String message);
    public String getMessage();
    public void sayHello();
}

A class to implement the JMX MBean interface

Next, here's the source code for a Java class named Hello that implements the HelloMBean interface we just defined:

public class Hello implements HelloMBean {
    private String message = null;

    public Hello() {
        message = "Hello, world";
    }

    public Hello(String message) {
        this.message = message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getMessage() {
        return message;
    }

    public void sayHello() {
        System.out.println(message);
    }
}

A JMX example class (a JMX service)

Next up, here's the source code for a Java class named SimpleAgent that includes a main method that starts up our JMX service application:

import javax.management.*;
import java.lang.management.*;

public class SimpleAgent {
    private MBeanServer mbs = null;

    public SimpleAgent() {

        // Get the platform MBeanServer
        mbs = ManagementFactory.getPlatformMBeanServer();

        // Unique identification of MBeans
        Hello helloBean = new Hello();
        ObjectName helloName = null;

        try {
            // Uniquely identify the MBeans and register them with the platform MBeanServer 
            helloName = new ObjectName("FOO:name=HelloBean");
            mbs.registerMBean(helloBean, helloName);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    // Utility method: so that the application continues to run
    private static void waitForEnterPressed() {
        try {
            System.out.println("Press  to continue...");
            System.in.read();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String argv[]) {
        SimpleAgent agent = new SimpleAgent();
        System.out.println("SimpleAgent is running...");
        SimpleAgent.waitForEnterPressed();
     }
}

A script to start our JMX service

And finally here's a Unix/Linux shell script named run.sh that I wrote to run this sample JMX application ("JMX service"):

#!/bin/sh

java -Dcom.sun.management.jmxremote \
     -Dcom.sun.management.jmxremote.port=1617 \
     -Dcom.sun.management.jmxremote.authenticate=false \
     -Dcom.sun.management.jmxremote.ssl=false \
     SimpleAgent

As you can see from that code, my application will listen on port 1617 of whatever computer system it is run on.

To run all of this code, save the source code shown here to the appropriate files (HelloMBean.java, Hello.java, and SimpleAgent.java, and run.sh), compile them, and then run them with the run.sh script.

Access the JMX service with JConsole

Once the application is running you can then connect to it with the jconsole application and view its JMX resources. I don't have any JConsole screenshots here at this time, but in short, JConsole is a GUI application that Sun ships with the latest SDK releases. If you have the SDK, you should also have an application named jconsole, and you use jconsole as a "JMX console" to connect to both local and remote JMX services.

Assuming that my JMX example application is running on port 1617 (as shown in the script above) and on some remote server, you can access this JMX service via jconsole by (a) specifying the IP address of the server this service is running on and (b) that port. No password is needed to access this JMX service.