Developing a JMX MBean – Part 1

Im going to take you through the process of creating, packaging, deploying and using, a java managed bean otherwise know as an MBean.

MBeans are regular java beans which implement some interface with the suffix MBean

First, create your interface, this is going to expose the methods you can call on your MBean.

package uk.co.foo.jmx.mbean;

public interface HelloMBean
{
    public abstract String getIdentification();
    public abstract void getName(String name);
}

Now create the implementation class for this interface. I have chosen to call this class HelloMBeanImpl but it doesnt have to be, it could be called anything. The only requirement is that it implements an interface with the suffix of MBean. That been said, it is convention to call the MBean inteface the name of the implementation class with the suffix MBean

package uk.co.foo.jmx.mbean;

public class HelloMBeanImpl implements HelloMBean
{
    @Override
    public String getIdentification()
    {
        return "HelloMBeanImpl";
    }

    @Override
    public String sayHello(String name)
    {
        return "Hello " + name + "!";
    }
}

We need some xml which describes what our MBean is all about.

<?xml version=”1.0″ encoding=”UTF-8″?>
<!DOCTYPE MBean SYSTEM “MbeanDescriptor.dtd”>
<MBean type=”HelloMBean” version=”5.0″ platform=”dynamicproxy” description=”Simple Hello MBean example.”>
<attribute name=”identification” getMethod=”getIdentification” type=”java.lang.String”>

<operation name=”sayHello” role=”operation” type=”void” targetObjectType=”objectReference” impact=”ACTION”>
<signature>
<parameter name=”name” description=”The name to say hello too” type=”java.lang.String”>
<signature>
<operation>
<MBean>

We are nearly there now. The next thing to do is package up our MBean for deployment onto our server. Which I will leave for Part 2!