MonitorTarget target = new MonitorTarget();
int value = low.intValue() + 1;
target.setInteger(value);
server.registerMBean(target, name);
final MutableInteger times = new MutableInteger(0);
final MutableObject holder = new MutableObject(null);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
times.set(times.get() + 1);
holder.set(notification);
}
};
server.addNotificationListener(monitorName, listener, null, null);
monitor.start();
try
{
// Inside the thresholds, be sure low notification
sleep(period * 3);
assertEquals(times.get(), 1);
MonitorNotification notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);
times.set(0);
holder.set(null);
sleep(period * 3);
assertEquals(times.get(), 0);
// Monitoring takes time, so I disable low notification to be sure to get only the high one
// The monitor is in difference mode, so the first time will get the high notification, but
// the second time will get zero, since the gauge did not change, which will triggers a low notification
monitor.setNotifyLow(false);
// Set gauge above high threshold
value = value + high.intValue() + 1;
target.setInteger(value);
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.THRESHOLD_HIGH_VALUE_EXCEEDED);
times.set(0);
holder.set(null);
sleep(period * 3);
assertEquals(times.get(), 0);
monitor.setNotifyHigh(false);
monitor.setNotifyLow(true);
// Set gauge above high threshold, so just after goes below low threshold
value = value + high.intValue() + 1;
target.setInteger(value);
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.THRESHOLD_LOW_VALUE_EXCEEDED);
times.set(0);
holder.set(null);
sleep(period * 3);
assertEquals(times.get(), 0);
// Set gauge inside threshold
value = value + low.intValue() + 1;
target.setInteger(value);
sleep(period * 3);
assertEquals(times.get(), 0);
assertNull(holder.get());
}
finally
{
monitor.stop();