ObjectName cmn =
new ObjectName(domain +
":type=" + CounterMonitor.class.getName());
CounterMonitor m = new CounterMonitor();
mbs.registerMBean(m, cmn);
CounterMonitorMBean cm = (CounterMonitorMBean)
MBeanServerInvocationHandler.newProxyInstance(
mbs, cmn, CounterMonitorMBean.class, true);
((NotificationEmitter) cm).addNotificationListener(
new Listener(), null, null);
cm.setObservedAttribute("Counter");
cm.setGranularityPeriod(100);
cm.setInitThreshold(3);
cm.setNotify(true);
// Add observed object name1
//
System.out.println("\nObservedObject \"" + name1 +
"\" registered before starting the monitor");
cm.addObservedObject(name1);
// Start the monitor
//
System.out.println("\nStart monitoring...");
cm.start();
// Play with counter for name1
//
System.out.println("\nTest ObservedObject \"" + name1 + "\"");
for (int i = 0; i < 4; i++) {
mbean1.setCounter(i);
System.out.println("\nCounter = " + mbean1.getCounter());
Thread.sleep(300);
Number thresholdValue = cm.getThreshold(name1);
System.out.println("Threshold = " + thresholdValue);
if (thresholdValue.intValue() != 3) {
System.out.println("Wrong threshold! Current value = " +
thresholdValue + " Expected value = 3");
System.out.println("\nStop monitoring...");
cm.stop();
throw new IllegalArgumentException("wrong threshold");
}
Thread.sleep(300);
}
// Add observed object name2
//
System.out.println("\nObservedObject \"" + name2 +
"\" registered after starting the monitor");
cm.addObservedObject(name2);
// Play with counter for name2
//
System.out.println("\nTest ObservedObject \"" + name2 + "\"");
for (int i = 0; i < 4; i++) {
mbean2.setCounter(i);
System.out.println("\nCounter = " + mbean2.getCounter());
Thread.sleep(300);
Number thresholdValue = cm.getThreshold(name2);
System.out.println("Threshold = " + thresholdValue);
if (thresholdValue.intValue() != 3) {
System.out.println("Wrong threshold! Current value = " +
thresholdValue + " Expected value = 3");
System.out.println("\nStop monitoring...");
cm.stop();
throw new IllegalArgumentException("wrong threshold");
}
Thread.sleep(300);
}
// Stop the monitor
//
System.out.println("\nStop monitoring...");
cm.stop();
}