final MBeanServer server = MBeanServerFactory.createMBeanServer();
final ObjectName mbean = new ObjectName("ugly:type=cr.p");
final My my = new My();
final GaugeMonitor mon2 = new GaugeMonitor();
final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");
final CounterMonitor mon1 = new CounterMonitor();
final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");
server.registerMBean(my, mbean);
server.registerMBean(mon1, mon1n);
server.registerMBean(mon2, mon2n);
mon1.addObservedObject(mbean);
mon1.setGranularityPeriod(60000); // 60 sec...
mon1.setObservedAttribute(attr);
mon1.setDifferenceMode(true);
check(attr,server,mon1n,mbean);
mon2.addObservedObject(mbean);
mon2.setGranularityPeriod(60000); // 60 sec...
mon2.setObservedAttribute(attr);
mon2.setDifferenceMode(true);
check(attr,server,mon2n,mbean);
final long approxStart = System.currentTimeMillis();
mon1.start();
mon2.start();
try {
check(attr,server,mon1n,mbean,approxStart);
check(attr,server,mon2n,mbean,approxStart);
check(attr,server,mon1n,mbean,approxStart);
check(attr,server,mon2n,mbean,approxStart);
mon1.setGranularityPeriod(5);
mon2.setGranularityPeriod(5);
my.cdl.await(1000, TimeUnit.MILLISECONDS);
if (my.cdl.getCount() > 0)
throw new Exception(attr+": Count down not reached!");
// just check that we don't get an exception now...
System.err.println(attr+": [" + mon1n+
"] DerivedGauge is now "+
server.getAttribute(mon1n, "DerivedGauge"));
System.err.println(attr+": [" + mon2n+
"] DerivedGauge is now "+
server.getAttribute(mon2n, "DerivedGauge"));
} finally {
try {mon1.stop();} catch (Exception x) {}
try {mon2.stop();} catch (Exception x) {}
}
}