The XMOJO Project
<< Prev Chapter 3.2.2.3 String Monitor Service Next >>

String Monitor


StringMonitor is a class capable of monitoring attributes of type string and can detect two conditions:
  1. The observed attribute's value matches a specific string
  2. The observed attribute's value differs from the specified string
Also, the StringMonitor class defines two boolean read-write attributes:
  1. NotifyMatch
  2. NotifyDiffer
If the NotifyMatch is set true, and if the value of the observed attribute matches the comparison value, StringMonitor sends a notification of type jmx.monitor.string.matches. If the NotifyDiffer is set true, and if the value of the observed attribute differs from the comparison value, StringMonitor sends a notification of type jmx.monitor.string.differs.

The String Monitor service is depicted pictorially below to give you a better understanding about how this service works:

How String Monitor works

To monitor an attribute Name of type string in some MBean, an instance of string monitor has to be created. For this string monitor instance, the required details, such as the MBean name, the attribute that has to be observed, the time interval, the comparison string, the notifymatch flag, and the notifydiffer flag has to be specified.

 Creating a String Monitor 

For better understanding, this section is split into steps:

Step 1 : Creating an instance of the StringMonitor class
StringMonitor can be instantiated by using the no argument constructor.

Step 2 : Configuring the monitor details for the created StringMonitor instance
The required details are configured in this step. Some of the methods provided in the StringMonitor class are listed below :
Step 3 : Registering the StringMonitor instance with the MBeanServer

Step 4 : Registering the NotificationListeners with the StringMonitor MBean so that the emitted Notifications can be handled.

Step 5 : Activating the StringMonitor service so that it starts observing the attribute.
Invoke the start method of the StringMonitor instance.

 A Sample Code Snippet 

The below code snippet shows how to create a StringMonitor instance for monitoring an attribute named Status. The attribute will be observed for every five seconds. If the value of the Status matches the comparison value "stopped" a notification of type jmx.monitor.string.matches will be triggered. No notification will be triggered if the attribute value differs from the comparison value.

import javax.management.monitor.StringMonitor; 

StringMonitor sm = new StringMonitor();

try
{   
  sm.setObservedObject(new ObjectName("Server:name=machineA,port=8080"))
  sm.setObservedAttribute("Status");
  sm.setGranularityPeriod(5000);
  sm.setStringToCompare("stopped");
  sm.setNotifyMatch(true);
  sm.setNotifyDiffer(false);
  server.registerMBean(sm, new ObjectName("Services:type=StringMonitor,name=StringMonitor_0"))
  server.addNotificationListener(new ObjectName("Services:type=StringMonitor,name=StringMonitor_0"), notifImpl, null, new Object())
  sm.start();
}
catch (Exceptione)
{
  e.printStackTrace();
}

<< Prev Home Next >>
Gauge Monitor Service
M-Let Service