// Register an MBean Emitter
ObjectName emitterName = ObjectName.getInstance(":mbean=emitter");
MBeanEmitter emitter = new MBeanEmitter();
server.registerMBean(emitter, emitterName);
final MutableInteger counter1 = new MutableInteger(0);
NotificationListener listener1 = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
counter1.set(counter1.get() + 1);
}
};
final MutableInteger counter2 = new MutableInteger(0);
NotificationListener listener2 = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
counter2.set(counter2.get() + 1);
}
};
JMXServiceURL url = createJMXConnectorServerAddress();
cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
cntorServer.start();
sleep(5000);
cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
MBeanServerConnection mbsc = cntor.getMBeanServerConnection();
String type = "notification.type";
// First listener
mbsc.addNotificationListener(emitterName, listener1, null, null);
// Second listener
NotificationFilterSupport filter = new NotificationFilterSupport();
filter.enableType(type);
mbsc.addNotificationListener(emitterName, listener1, filter, null);
// Third listener
Object handback = new Object();
mbsc.addNotificationListener(emitterName, listener1, null, handback);
// Fourth listener
mbsc.addNotificationListener(emitterName, listener2, null, null);
// Wait for notification threads to start
sleep(1000);
Notification notification = new Notification(type, this, 0);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
// Be sure we got all notifications
assertEquals(counter1.get(), 3);
assertEquals(counter2.get(), 1);
counter1.set(0);
counter2.set(0);
// Remove one listener
mbsc.removeNotificationListener(emitterName, listener1, null, handback);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
assertEquals(counter1.get(), 2);
assertEquals(counter2.get(), 1);
counter1.set(0);
counter2.set(0);
// Remove all listeners
mbsc.removeNotificationListener(emitterName, listener1);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
assertEquals(counter1.get(), 0);
assertEquals(counter2.get(), 1);
counter1.set(0);
counter2.set(0);
mbsc.removeNotificationListener(emitterName, listener2);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
assertEquals(counter1.get(), 0);
assertEquals(counter2.get(), 0);
} catch (Exception x)
{
x.printStackTrace();
throw x;
} finally