The XMOJO Project
<< Prev Chapter 2.3.1 Writing a Model MBean Next >>

Writing Your Own Model MBean


We have seen in our ModelMBean section, that any JMX implementation should provide a default implementation of the ModelMBean interface, called RequiredModelMBean. A model MBean can be created with just a few lines of code by using this RequiredModelMBean.

The RequiredModelMBean class provides two constructors:

RequiredModelMBean() -- A constructor taking no arguments. This constructs a RequiredModelMBean with an empty ModelMBeanInfo. If this constructor is used, setModelMBeanInfo has to be used to construct the model MBean information.
RequiredModelMBean(ModelMBeanInfo mbi) -- This constructs a RequiredModelMBean object using the ModelMBeanInfo passed in.

The following section discusses how to create this ModelMBeanInfo object so that it can be passed in the RequiredModelMBean constructor and a RequiredModelMBean object can be obtained. Once the RequiredModelMBean object is obtained, the managed resource can be set using the method setManagedResource available in the RequiredModelMBean class.

 Creation of ModelMBeanInfo Object 

There are two options to create a ModelMBeanInfo object:
  1. Constructing the ModelMBeanInfo using the available API.
  2. Writing an XML file and converting this XML file into a ModelMBeanInfo using some utility method.

 Using the available API 

In this option, a lot of coding is required. For every attribute, operation, constructor, and so on, Descriptor object has to be created. A sample code snippet is available which explains how this approach can be achieved. The sample code snippet creates a ModelMBeanInfo for managing the information of a web server. View the management information the ModelMBean exposes.

Since the code snippet is large, it is provided in a separate HTML file. The link for the HTML file is View the code snippet

The getMBeanInfo method in the code snippet returns the ModelMBeanInfo object, which can be passed in the RequiredModelMBean constructor to obtain a RequiredModelMBean object.

 Writing an XML file 

This option saves a lot of time when you write more model MBeans. Write a simple XML file which contains all the management information details, such as attributes, operations, notifications, and so on, including the Descriptor fields for each of the management information. Use the convertXMLToModelMBeanInfo utility method, which converts the XML file into a ModelMBeanInfo object. This utility method is available in com.adventnet.agent.utilities.common.Utilities class.

View the xml file

The following code snippet describes converting the XML file into a ModelMBeanInfo object using the utility method and creating a RequiredModelMBean object out of the ModelMBeanInfo returned by the utility method.

import com.adventnet.agent.utilities.common.Utilities;
import javax.management.modelmbean.RequiredModelMBean;
import javax.management.modelmbean.ModelMBeanInfo;

ModelMBeanInfo mbi = null;

mbi = Utilities.convertXMLToModelMBeanInfo( path_to_xml_file );

RequiredModelMBean rmm = new RequiredModelMBean(mbi);

 Setting the Managed Resource 

Once the Required Model MBean is created, the managed resource is set using the setManagedResource method.


import examples.application.server.ServerInfo;

rmm.setManagedResource(new ServerInfo(), "ObjectReference");

In the above code snippet, rmm is the RequiredModelMBean obtained by passing a ModelMBeanInfo. ServerInfo is the class that contains the required implementation details for exposing the management information.

To see a working example of a model MBean, try out the example.

<< Prev Home Next >>
Model MBean Notification Model