JBoss Community Archive (Read Only)

Infinispan 6.0

Listeners and Notifications

Infinispan offers a listener API, where clients can register for and get notified when events take place.  This annotation-driven API applies to 2 different levels: cache level events and cache manager level events.

Events trigger a notification which are dispatched to listeners.   Listeners are simple POJOs annotated with @Listener and registered using the methods defined in the Listenable interface.

Both Cache and CacheManager implement Listenable, which means you can attach listeners to either a cache or a cache manager, to receive either cache-level or cache manager-level notifications.

For example, the following class defines a listener to print out some information every time a new entry is added to the cache:

public class PrintWhenAdded {

  public void print(CacheEntryCreatedEvent event) {
    System.out.println("New entry " + event.getKey() + " created in the cache");

For more comprehensive examples, please see the Javadocs for @Listener.

Cache-level notifications

Cache-level events occur on a per-cache basis, and is global and cluster-wide.  Examples of cache-level events are entries being added, removed, modified, etc.  These events trigger notifications to listeners registered to a specific cache.

Please see the Javadocs on the org.infinispan.notifications.cachelistener.annotation package for a comprehensive list of all cache-level notifications, and their respective method-level annotations.

NOTE: In Infinispan 5.0, additional events were added.  Please refer to the <a href="http://docs.jboss.org/infinispan/5.0/apidocs/org/infinispan/notifications/cachelistener/annotation/package-summary.html">Javadocs on the org.infinispan.notifications.cachelistener.annotation package</a> for Infinispan 5.0 for the list of cache-level notifications available in Infinispan 5.0.

Cache manager-level notifications

Cache manager-level events occur on a cache manager.  These too are global and  cluster-wide, but involve events that affect all caches created by a single cache manager.  Examples of cache manager-level events are nodes joining or leaving a cluster, or caches starting or stopping.

Please see the Javadocs  on the org.infinispan.notifications.cachemanagerlistener.annotation package for a comprehensive list of all cache manager-level notifications,  and their respective method-level annotations.


By default, all notifications are dispatched in the same thread that generates the event.  This means that you must write your listener such that it does not block or do anything that takes too long, as it would prevent the thread from progressing.  Alternatively, you could annotate your listener as asynchronous , in which case a separate thread pool will be used to dispatch the notification and prevent blocking the event originating thread.  To do this, simply annotate your listener such:

@Listener (sync = false)
public class MyAsyncListener { .... }

Asynchronous thread pool

To tune the thread pool used to dispatch such asynchronous notifications, use the <asyncListenerExecutor /> XML element in your configuration file.

Listeners on RemoteCacheManager

At the moment there is no support for registering listeners for the RemoteCacheManager. Whenever calling one of the add/remove/getListener methods on the RemoteCacheManager an UnsupportedOperationException will be thrown. There are plans to support remote listeners in future versions; in order to be notified when this feature will be added you can watch/vote for ISPN-374.

JBoss.org Content Archive (Read Only), exported from JBoss Community Documentation Editor at 2020-03-11 09:39:16 UTC, last content change 2011-06-27 12:23:01 UTC.