{
ObjectName name = new ObjectName(":mbean=target");
ObjectName monitorName = new ObjectName(":monitor=gauge");
MBeanServer server = newMBeanServer();
StringMonitor monitor = (StringMonitor)createMonitor();
String reference = "XYZ";
monitor.setStringToCompare(reference);
monitor.setNotifyMatch(true);
monitor.setNotifyDiffer(true);
monitor.addObservedObject(name);
monitor.setObservedAttribute("String");
int period = 1000;
monitor.setGranularityPeriod(period);
server.registerMBean(monitor, monitorName);
MonitorTarget target = new MonitorTarget();
target.setString(reference);
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
{
sleep(period * 3);
assertEquals(times.get(), 1);
MonitorNotification notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
times.set(0);
holder.set(null);
target.setString("xx");
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
times.set(0);
holder.set(null);
target.setString(reference);
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);
times.set(0);
holder.set(null);
target.setString("yyyy");
sleep(period * 3);
assertEquals(times.get(), 1);
notification = (MonitorNotification)holder.get();
assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);
times.set(0);
holder.set(null);
target.setString("zzzzz");
sleep(period * 3);
assertEquals(times.get(), 0);
assertNull(holder.get());
}
finally
{
monitor.stop();
}
}