JMX example: A Java JMX Swing application

Here is some sample Java source code for some JMX tests that I created a while ago. I started my first tests with an introductory JMX tutorial, then created this JMX example to see how JMX might work with a standalone application, in this case, a Java Swing GUI application. Let's look at the code.

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

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

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

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

  public Hello() {
    message = "HI there";

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

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

  public String getMessage() {
    return message;

  public void sayHello() {

Next up, here's the source code for a Java class named SimpleAgent2 that includes a main method that kicks off this JMX application and JMX service:

import javax.swing.*;

public class SimpleAgent2 {
   private MBeanServer mbs = null;

   public SimpleAgent2() {

      // 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=SwingHelloThere");
         mbs.registerMBean(helloBean, helloName);
      } catch(Exception e) {

   public static void main(String argv[]) {
      SimpleAgent2 agent = new SimpleAgent2();
      System.out.println("SimpleAgent2 is running...");
      JFrame f = new JFrame();

And finally here's a Unix/Linux shell script named that I wrote to run this sample JMX application:


java \ \ \ \

To run all of this code, save the source code shown here to the appropriate files (,, and, and, compile them, and then run them with the script.

Once the application is running you can then connect to it with the jconsole application and view its JMX resources.