JavaRanch Home    
 
This page:         last edited 25 November 2011         What's Changed?         Edit

JMX Example   

This is a simple application that can be monitored using JMX. In addition to the parameters that the JVM makes available for monitoring, it creates two MBean objects of its own.

The application consists of 3 classes: the HelloMBean interface to declare what operations and properties the custom MBeans make available, the Hello class that implements the interface -this is the object that can be monitored-, and the HelloAgent application that ties it all together.


public interface HelloMBean
{
    public String getMessage();
    public void setMessage (String message);
    public int getChangeCount();
    public void resetCounter();
}


public class Hello implements HelloMBean 
{
    private String message;
    private int changeCount;

    public String getMessage()
    {
        return message;
    }

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

    public int getChangeCount()
    {
        return changeCount;
    }

    public void resetCounter()
    {
        changeCount = 0;
    }
}

After starting the application with

 java -classpath . -Dcom.sun.management.jmxremote HelloAgent &

run the JMX console via "jconsole" from the command line. In the MBeans tab you'll see the hello1 and hello2 objects. Their "message" property can be manipulated independently, and the change count is updated accordingly. The application will quit automatically after 5 minutes.

Instead of jconsole you can also use VisualVM (which ships with the JDK) if you install its MBeans plugin.


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

public class HelloAgent
{

    public static void main (String args[]) throws Exception
    {
        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
        HelloMBean hello1 = new Hello();
        HelloMBean hello2 = new Hello();

        try
        {
            ObjectName helloObjectName1 = new ObjectName("HelloServer:type=Hello,name=hello1");
            server.registerMBean(hello1, helloObjectName1);
            ObjectName helloObjectName2 = new ObjectName("HelloServer:type=Hello,name=hello2");
            server.registerMBean(hello2, helloObjectName2);
        } catch (MalformedObjectNameException e)
        {
            System.out.println("Bad object name");
            e.printStackTrace();
        } catch (InstanceAlreadyExistsException e)
        {
            System.out.println("Already exists");
            e.printStackTrace();
        } catch (MBeanRegistrationException e)
        {
            System.out.println("Registration problems");
            e.printStackTrace();
        } catch (NotCompliantMBeanException e) {
            System.out.println("Registration problems");
            e.printStackTrace();
        }
    }

    Thread.sleep(300000);
}


CategoryCodeSamples CodeBarn

JavaRanchContact us — Copyright © 1998-2014 Paul Wheaton