The XMOJO Project
<< Prev Chapter 3.2.2.1 Counter Monitor Service Next >>

Counter Monitor


CounterMonitor is a class capable of observing attributes of Java integer types (Byte, Integer, Short, and Long) which behave like a counter, that is, their value is always greater than or equal to zero, and they can only be incremented. Some examples for counters are server up time (number of hours a Web server is running) and number of requests processed by the server.

A counter monitor sends a notification of type jmx.monitor.counter.threshold when the value of the counter (observed attribute) reaches or exceeds the comparison value. This comparison value is called threshold value.

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

How CounterMonitor works

To monitor an attribute Name of type counter in some MBean, an instance of counter monitor has to be created. For this counter monitor instance, the required details, such as the MBean name, the attribute that has to be observed, the time interval, the threshold condition, and offset value have to be specified.

 Creating a Counter Monitor 

For better understanding, this section is split up into steps:

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

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

Step 4 : Registering the NotificationListeners so that the Notifications sent by the CounterMonitor MBean can be handled.

Step 5 : Activating the CounterMonitor service so that it starts observing the attribute.

 A Sample Code snippet 

Let us assume that there is an mbean which represents an instance of a thermometer. The thermometer MBean is defined with an attribute called Temperature, and we assume that the temperature value increases over a period, that is, the Temperature attribute acts like a counter. The below code snippet shows how to create a counter monitor instance for observing the Temperature attribute in the Thermometer MBean with the Threshold value -- 100, Offset value -- 5, Granularity Period -- 5 seconds and DifferenceMode -- false.

import javax.management.monitor.CounterMonitor; 

CounterMonitor cm = new CounterMonitor();

try
{   
  cm.setObservedObject(new ObjectName("Thermometer:unit=Fahrenheit"))
;
  cm.setObservedAttribute("Temperature")
;
  cm.setGranularityPeriod(5000)
  cm.setDifferenceMode(false)
  cm.setThreshold(new Integer(100))
  cm.setOffset(new Integer(5));
  cm.setNotify(true);
  server.registerMBean(cm, new ObjectName("Services:type=CounterMonitor,name=CounterMonitor_0"))
  server.addNotificationListener(new ObjectName("Services:type=CounterMonitor,name=CounterMonitor_0"), notifImpl, null, new Object())
  cm.start();
}
catch (Exception e)
{
  e.printStackTrace();
}

 How the Above Code Works 

The CounterMonitor MBean observes the Temperature attribute of the Thermometer MBean for every 5 seconds. Let us assume the initial Temperature value as 40. Also, let this Temperature value gradually increase from 40 to 95 after an interval of 40 seconds. During this 40 seconds interval, the CounterMonitor MBean would have observed the value for 8 times (approximately). It checks whether the value is greater than the threshold value (100). Since the value does not exceed the threshold value, no notification will be sent.

Let the Temperature value reach 101 when the CounterMonitor observes it next time. Since the value exceeded 100, a notification of type jmx.monitor.counter.threshold will be triggered by the CounterMonitor MBean. Once the notification is triggered, the CounterMonitor MBean checks if any Offset value is specified. In the above case, it is specified as 5. If Offset value is specified, the CounterMonitor MBean updates the threshold value with the new value (Observed value + Offset value). The threshold value will be increased to 106 (101 + 5).

Let the Temperature value remain at 101 itself for 30 seconds and suddenly the Temperature value reaches the value 138. If the CounterMonitor observes the Temperature value at this instant, a notification of type jmx.monitor.counter.threshold will be triggered and the new threshold value now becomes 143 (138 + 5).

 Modulus 

The CounterMonitor class defines a field called Modulus. This Modulus defines the maximum value that can be set to the threshold.

Let us consider the above example. Whenever the value of the temperature reaches or exceeds the threshold value, a new threshold value will be set. Even if the temperature reaches 1200, the new threshold value will be 1205. We can restrict this and can define a maximum value up to which threshold can be set. This is achieved by defining the Modulus field of the CounterMonitor class.

To define the Modulus field, the setModulus method can be used. In the above example, let us add the following line of code in configuring the counter monitor details:
cm.setModulus(150);

If the temperature exceeds 150 then the threshold value rolls back and sets to the initial value with which the threshold is configured, i.e, 100.

<< Prev Home Next >>
Monitor Service
Gauge Monitor Service