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:
Creating MBeanServer
Registering MBeans with the MBeanServer
Creating adaptors and required services
Creating MBeanServer
The factory class MBeanServerFactory is used to create an instance of the
MBeanServer.
... // 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.
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.
// 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.