In this section we demonstrate the use of JMX Monitors and the Timer service to manage the operation of the HTTP daemon at runtime.
The JMX specification defines three types of Monitors: Counter,
Gauge, and String. A counter monitor is used to observe the
value of a counter attribute. A counter attribute is one that is always
positive and increasing, .e.g., the LogFileMBean's Size
attribute or the
ListenerMBean's Request
attribute. Gauge monitors are used to observe
attributes that vary continuously with time. The Length
of the RequestQueue
and Size
of the HandlerPool are examples of gauge attributes. String monitors
are used to observe a string attribute.
Each of the three Monitor types mentioned above is a concrete subclass of
the abstract class javax.management.monitor.Monitor
. As such you
can create instances directly via their constructors. For example:
GaugeMonitor handlerPoolGauge = new GaugeMonitor();
If you take this approach the Monitor, which is also an MBean, should be registered with the MBeanServer. Alternatively, you could choose to create the MBean via the MBeanServer:
mbs.createMBean("javax.management.monitor.CounterMonitor",
lfMonitorObjectName,
null,
null);
Once a monitor has been created it has to be configured. Basic monitor configuration consists of setting the object to be monitored, the observed object, the attribute to monitor, the observed attribute, and the granularity period (in milliseconds) of the observations. To complete a monitor's configuration various type specifc monitor attributes must be set as well.
Here is the basic configuration for a log file counter monitor:
handlerPoolGauge.setObservedObject(handlersObjectNam);
handlerPoolGauge.setObservedAttribute("Size");
handlerPoolGauge.setGranularityPeriod(1000);
Counter and gauge monitors have associated thresholds. When the value of the observed attribute crosses a threshold a notification may be sent. Notifications will be discussed in detail below.
The gauge monitor has two thresholds a high and a low threshold. We have to decide how to set each of them. The size of the handler pool tells us how many incoming requests can be processed immediately at any given moment in time. Therefore we need to be concerned about that value being too low. Let's assume that we always want to be able to handle at least two simultaneous requests and set the handlerPoolGauge's low threshold to two.
Now the handlerPoolGauge will generate a notification the first time the size of the handler pool becomes less than or equal to two. To prevent a series of suprious notification that would result from the value of the size attribute bouncing around the low threshold no further "low threshold" notifications will be sent until the handler pool size crosses the high threshold. Therefore we need to choose a value for the high threshold that represents the point at which the size attribute is once again considered acceptable. Since the handler pool has a default size of eight let's say that we will feel comfortable when the size of the pool returns to four after a low threshold notification.
Here's the code to configure the handlerPoolGauge as described:
Integer highThreshold = new Integer(4);
Integer lowThreshold = new Integer(2);
handlerPoolGauge.setThresholds(highThreshold, lowThreshold);
Finally the monitor has to be started. Starting and stopping a monitor is accomplished via the respective start() and stop() methods. The code below will start the handlerPoolGauge monitor:
handlerPoolGauge.start();
Here is a version of Httpd that illustrates the creation and configuration of log file and handler pool monitors.