NotificationBroadcasterSupport mbean2 = new NotificationSupport.Emitter();
server.registerMBean(mbean1, name1);
server.registerMBean(mbean2, name2);
final MutableInteger integer = new MutableInteger(0);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
integer.set(integer.get() + 1);
}
};
server.addNotificationListener(name1, listener, null, null);
server.addNotificationListener(name2, listener, null, null);
mbean2.addNotificationListener(listener, null, null);
Notification notification = new Notification("test", mbean1, 1);
mbean1.sendNotification(notification);
// Be sure the listener is called
assertEquals("Listener is not called", integer.get(), 1);
mbean2.sendNotification(notification);
// Be sure the listeners are called
assertEquals("Listeners are not called", integer.get(), 3);
// Remove one listener
server.removeNotificationListener(name2, listener);
// Be sure the listener is called
mbean2.sendNotification(notification);
assertEquals("Listener is not called", integer.get(), 4);
// Be sure it is called
mbean1.sendNotification(notification);
assertEquals("Listener is not called", integer.get(), 5);
server.removeNotificationListener(name1, listener);
// Be sure it is not called
mbean1.sendNotification(notification);
assertEquals("Listener is called", integer.get(), 5);
// Be sure it is called
mbean2.sendNotification(notification);
assertEquals("Listener is not called", integer.get(), 6);
try
{
server.removeNotificationListener(name2, listener);
fail("Listener has been removed");
}
catch (ListenerNotFoundException ignored)
{
}
// Remove also the second listener
mbean2.removeNotificationListener(listener);
// Be sure it is not called
mbean2.sendNotification(notification);
assertEquals("Listener is called", integer.get(), 6);
}