JavaRanch Home
 
Front Page FAQs Ranchers Categories Recent Changes To Do Upload

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.

Note that JDK 5 is required for this. JMX is available as an add-on for JDK 1.4, but since that doesn't include the jconsole application for monitoring, a different HTTP/HTML-based mechanism must be used. Details about that can be found in the JDC Tech Tip, on which this example is based.


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.


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
Front Page FAQs Ranchers Categories Recent Changes To Do Upload
Last Edited: 29 September 2007 What's Changed?
 
Copyright © 1998-2008 Paul Wheaton | Home | Contact Us | Privacy | Register