{
MBeanServer server = newMBeanServer();
ObjectName name = new ObjectName(":type=test");
// Register a logger mbean
server.createMBean("mx4j.log.LoggerBroadcaster", name, null);
LoggerBroadcasterMBean redirector = (LoggerBroadcasterMBean)MBeanServerInvocationHandler.newProxyInstance(server, name, LoggerBroadcasterMBean.class, false);
try
{
// Register a notification listener
final MutableInteger notified = new MutableInteger(0);
NotificationListener listener = new NotificationListener()
{
public void handleNotification(Notification notification, Object handback)
{
notified.set(notified.get() + 1);
}
};
server.addNotificationListener(name, listener, null, null);
// Redirect logging
redirector.start();
// Create some log
ObjectName mlet = new ObjectName(":type=mlet");
server.createMBean("javax.management.loading.MLet", mlet, null);
// This should create some log
server.invoke(mlet, "addURL", new Object[]{new URL("http://mx4j.sourceforge.net")}, new String[]{"java.net.URL"});
// Test if redirection worked
if (notified.get() < 1) fail("Notification listener not called");
}
finally
{
redirector.stop();
}
}