12.3. Receiving Cache Notifications
JBoss Cache users can register a listener to receive cache events as described in the Eviction Policies chapter. Users can alternatively utilize the cache's management information infrastructure to receive these events via JMX notifications. Cache events are accessible as notifications by registering a NotificationListener for the CacheMgmtInterceptor MBean. This functionality is only available if cache statistics are enabled as described in the previous section.
The following table depicts the JMX notifications available for JBoss Cache as well as the cache events to which they correspond. These are the notifications that can be received through the CacheMgmtInterceptor MBean. Each notification represents a single event published by JBoss Cache and provides user data corresponding to the parameters of the event.
| Notification Type | Notification Data | TreeCacheListener Event | |||
|---|---|---|---|---|---|
| org.jboss.cache.CacheStarted | String : cache service name | cacheStarted | |||
| org.jboss.cache.CacheStopped | String : cache service name | cacheStopped | |||
| org.jboss.cache.NodeCreated | String : fqn | NodeCreated | |||
| org.jboss.cache.NodeEvicted | String : fqn | NodeEvicted | |||
| org.jboss.cache.NodeLoaded | String : fqn | NodeLoaded | |||
| org.jboss.cache.NodeModifed | String : fqn | NodeModifed | |||
| org.jboss.cache.NodeRemoved | String : fqn | NodeRemoved | |||
| org.jboss.cache.NodeVisited | String : fqn | NodeVisited | |||
| org.jboss.cache.ViewChange | String : view | ViewChange | |||
| org.jboss.cache.NodeActivate |
|
NodeActivate | |||
| org.jboss.cache.NodeEvict |
|
NodeEvict | |||
| org.jboss.cache.NodeModify |
|
NodeModify | |||
| org.jboss.cache.NodePassivate |
|
NodePassivate | |||
| org.jboss.cache.NodeRemove |
|
NodeRemove |
Table 12.2. JBoss Cache MBean Notifications
The following is an example of how to programmatically receive cache notifications when running in a JBoss application server environment. In this example, the client uses a filter to specify which events are of interest.
MyListener listener = new MyListener();
NotificationFilterSupport filter = null;
// get reference to MBean server
Context ic = new InitialContext();
MBeanServerConnection server = (MBeanServerConnection)ic.lookup("jmx/invoker/RMIAdaptor");
// get reference to CacheMgmtInterceptor MBean
String cache_service = "jboss.cache:service=TomcatClusteringCache";
String mgmt_service = cache_service + ",treecache-interceptor=CacheMgmtInterceptor";
ObjectName mgmt_name = new ObjectName(mgmt_service);
// configure a filter to only receive node created and removed events
filter = new NotificationFilterSupport();
filter.disableAllTypes();
filter.enableType(CacheMgmtInterceptor.NOTIF_NODE_CREATED);
filter.enableType(CacheMgmtInterceptor.NOTIF_NODE_REMOVED);
// register the listener with a filter
// leave the filter null to receive all cache events
server.addNotificationListener(mgmt_name, listener, filter, null);
// ...
// on completion of processing, unregister the listener
server.removeNotificationListener(mgmt_name, listener, filter, null);
The following is the simple notification listener implementation used in the previous example.
private class MyListener implements NotificationListener, Serializable {
public void handleNotification(Notification notification, Object handback) {
String message = notification.getMessage();
String type = notification.getType();
Object userData = notification.getUserData();
System.out.println(type + ": "+message);
if (userData == null) {
System.out.println("notification data is null");
}
else if (userData instanceof String) {
System.out.println("notification data: "+(String)userData);
}
else if (userData instanceof Object[]) {
Object[] ud = (Object[])userData;
for (int i = 0; i > ud.length; i++) {
System.out.println("notification data: "+ud[i].toString());
}
}
else {
System.out.println("notification data class: " + userData.getClass().getName());
}
}
}
Note: the JBoss Cache management implementation only listens to cache events after a client registers to receive MBean notifications. As soon as no clients are registered for notifications, the MBean will remove itself as a cache listener.