Java Reference
In-Depth Information
@ManagedOperation(description = "Replicate files")
public void replicate() throws IOException {
notificationPublisher.sendNotification(
new Notification("replication.start", this, sequenceNumber));
...
notificationPublisher.sendNotification(
new Notification("replication.complete", this, sequenceNumber));
sequenceNumber++;
}
}
In this file replicator, you send a JMX notification whenever a replication starts or completes. The
notification is visible both in the standard output in the console as well as in the Notifications node for
your service in JConsole. To see them, you must click Subscribe. Then invoke the replicate() method,
and you'll see two new notifications arrive, much like your e-mail's inbox. The first argument in the
Notification constructor is the notification type, while the second is the notification source. Each
notification requires a sequence number. You can use the same sequence for a notification pair to keep
track of them.
Listening to JMX Notifications
Now let's create a notification listener to listen to JMX notifications. Because a listener will be notified of
many different types of notifications, such as javax.management.AttributeChangeNotification when an
MBean's attribute has changed, you have to filter those notifications that you are interested in handling.
package com.apress.springenterpriserecipes.replicator;
import javax.management.Notification;
import javax.management.NotificationListener;
public class ReplicationNotificationListener implements NotificationListener {
public void handleNotification(Notification notification, Object handback) {
if (notification.getType().startsWith("replication")) {
System.out.println(
notification.getSource() + " " +
notification.getType() + " #" +
notification.getSequenceNumber());
}
}
}
Then you can register this notification listener with your MBean exporter to listen to notifications
emitted from certain MBeans.
<bean id="mbeanExporter"
class="org.springframework.jmx.export.annotation.AnnotationMBeanExporter">
<property name="defaultDomain" value="bean" />
<property name="notificationListenerMappings">
<map>
Search WWH ::




Custom Search