The XMOJO Project
<< Prev 6. JMX Agent Creation Next >>

 Creating a JMX agent 

JMX agent comprises an MBeanServer, a set of MBeans representing the managed resources, a minimum number of agent services implemented as MBeans, and typically at least one protocol adaptpor or connector.

The creation of a simple JMX agent can be split into following sub-divisions:
  1. Creating MBeanServer
  2. Registering MBeans with the MBeanServer
  3. Creating adaptors and required services

 Creating MBeanServer 

The factory class MBeanServerFactory is used to create an instance of the MBeanServer.

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;

...


// create an instance of MBeanServer
MBeanServer mbs = MBeanServerFactory.createMBeanServer("MyServer");

The above code snippet, creates an MBeanServer instance with the domain name MyServer.

 Registering MBeans 

In the previous sections you have seen how to create MBeans to represent the managed resource, i.e, the Shopping Cart Application.  You register those MBeans with the above instance of MBeanServer.  To register an MBean, either the registerMBean method or the createMBean method can be used.  If the registerMBean method is used, an instance of the MBean class has to be passed.  If the createMBean method is used, the String representation of the class name is passed.


import javax.management.ObjectName;

// registering InventoryManager MBean

InventoryManager im = 
new InventoryManager("Parrot")
;

try
{
    mbs.registerMBean(im , new ObjectName
("MyServer:type=inventory"));

}
catch(Exception 
e)
{
    e.printStackTrace()
;
}

// registering Cart MBean

Cart crt = new Cart()
;

try 
{
    mbs.registerMBean ( crt , new ObjectName
("ShoppingCart:type=MBean") )
;
}
catch(Exception e)
{
    e.printStackTrace();

}

The above code snippet registers both the MBeans with the MBeanServer instance mbs, which you created.

The CartMBean will be registered with the MBeanServer and will be identified by its object name ShoppingCart:type=MBean (the object name passed while registering this MBean).   In the case of InventoryManager MBean,  the MBean will not be registered with the object name MyDomain:type=inventory.  This is because the InventoryManager implements the javax.management.MBeanRegistration interface, and in the preRegister method, it returns an object name InventoryMBean:Pet=<itemName> where itemName is the String argument passed while instantiating this InventoryManager class.  In your case, this argument is passed as Parrot, and the InventoryManager MBean will be registered with the MBeanServer and will be identified by the object name InventoryMBean:Pet=Parrot.

 Creating Adaptors and Required Services 

The below given code snippet shows how to create an instance of RMI Adaptor and register it with the MBeanServer.

import com.adventnet.adaptors.rmi.RMIAdaptor;

Integer rmiPort = 1099;

// instantiating and registering an RMI adaptor instance
// with the MBeanServer


try 
{
    RMIAdaptor rmiadaptor = new RMIAdaptor()
;
    rmiadaptor.setPort(rmiPort);
    mbs.registerMBean(rmiadaptor, new ObjectName("Adaptors:name=RMI,port=" + rmiPort));
}
catch(Exception e)
{
    e.printStackTrace();

}

The below given code snippet shows how to create an instance of HTML Adaptor and register it with the MBeanServer.

import com.adventnet.adaptors.html.HtmlAdaptor;
import com.adventnet.adaptors.html.HtmlAdaptorServerImpl;


private int htmlPort = 8030;


// instantiating and registering an HTML adaptor instance
// with the MBeanServer

try 
{
   
HtmlAdaptor htmladaptor = new HtmlAdaptor();
        htmladaptor.addHttpServerInterface(new HtmlAdaptorServerImpl());
    mbs.registerMBean(htmladaptor, new ObjectName("Adaptors:name=HTML,port=" + htmlPort));
        htmladaptor.setParentDir(".");
}
catch(Exception e)
{
    e.printStackTrace();

}

You also create a GaugeMonitor service which monitors the inventory level for the pet item Parrot.  Whenever the inventory level of Parrot reaches below 5, a notification will be sent.  The below given code snippet shows how to create a GaugeMonitor instance and set the required parameters that have to be monitored.

import javax.management.monitor.GaugeMonitor;

GaugeMonitor gm = 
new GaugeMonitor( );

try 
{
   
  gm.setObservedObject(new ObjectName(
"InventoryMBean:Pet=Parrot"))
;
  gm.setObservedAttribute("InventoryLevel")
;
  gm.setGranularityPeriod(5000);
  gm.setDifferenceMode(false);
  gm.setNotifyHigh(false)
;
  gm.setNotifyLow(true);
  gm.setThresholds(new Integer(50) , new Integer (5));
  mbs.registerMBean(gm ,
new ObjectName("Services:type=GaugeMonitor"))
;
  gm.start()
;
}

catch(Exception e)
{
    e.printStackTrace
();
}


Download the source file, which contains the implementation details of the above JMX agent.

<< Prev Home Next >>
Shop Keeper MBean Creation
Running and Testing the JMX Agent