}
}
public void testAddRemoveMBeanListener() throws Exception
{
JMXConnectorServer cntorServer = null;
JMXConnector cntor = null;
try
{
MBeanServer server = newMBeanServer();
// Register an MBean Emitter
ObjectName emitterName = ObjectName.getInstance(":mbean=emitter");
MBeanEmitter emitter = new MBeanEmitter();
server.registerMBean(emitter, emitterName);
// Register an MBean Listener
MutableObject notificationHolder = new MutableObject(null);
MutableObject handbackHolder = new MutableObject(null);
ObjectName listenerName = ObjectName.getInstance(":mbean=listener");
MBeanListener listener = new MBeanListener(notificationHolder, handbackHolder);
server.registerMBean(listener, listenerName);
JMXServiceURL url = createJMXConnectorServerAddress();
cntorServer = JMXConnectorServerFactory.newJMXConnectorServer(url, getEnvironment(), server);
cntorServer.start();
sleep(5000);
cntor = JMXConnectorFactory.connect(cntorServer.getAddress(), getEnvironment());
MBeanServerConnection mbsc = cntor.getMBeanServerConnection();
// Non-serializable filter
try
{
mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return false;
}
}, null);
fail();
} catch (IOException x)
{
}
// Non-serializable handback
try
{
mbsc.addNotificationListener(emitterName, listenerName, null, new Object());
fail();
} catch (IOException x)
{
}
// Non-serializable filter and non serializable handback
try
{
mbsc.addNotificationListener(emitterName, listenerName, new NotificationFilter()
{
public boolean isNotificationEnabled(Notification notification)
{
return false;
}
}, new Object());
fail();
} catch (IOException x)
{
}
// Everything is serializable
ObjectName name = ObjectName.getInstance(":mbean=dummy");
MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
filter.disableObjectName(name);
Object handback = new Integer(13);
mbsc.addNotificationListener(emitterName, listenerName, filter, handback);
// Wait for notifications threads to start
sleep(1000);
Notification notification = new MBeanServerNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION, this, 0, name);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
// Be sure the notification has been filtered
assertNull(notificationHolder.get());
assertNull(handbackHolder.get());
// Disable filtering
filter.enableAllObjectNames();
// Remove and readd: on server side there is a serialized copy of the filter
mbsc.removeNotificationListener(emitterName, listenerName);
mbsc.addNotificationListener(emitterName, listenerName, filter, handback);
// Wait for notifications threads to start
sleep(1000);
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
// Be sure we got it
assertEquals(handbackHolder.get(), handback);
Notification emitted = (Notification) notificationHolder.get();
assertNotNull(emitted);
if (!(notification instanceof MBeanServerNotification)) fail();
assertEquals(((MBeanServerNotification) emitted).getMBeanName(), name);
notificationHolder.set(null);
handbackHolder.set(null);
mbsc.removeNotificationListener(emitterName, listenerName, filter, handback);
// Be sure we don't get notifications anymore
emitter.emit(notification);
// Wait for notification to arrive
sleep(1000);
assertNull(notificationHolder.get());
assertNull(handbackHolder.get());
} catch (Exception x)
{
x.printStackTrace();
throw x;
} finally
{
if (cntor != null) cntor.close();
if (cntorServer != null) cntorServer.stop();
}
}