JBoss.org Community Documentation

2.7.  Adding a CacheListener

The @org.jboss.cache.notifications.annotation.CacheListener annotation is a convenient mechanism for receiving notifications from a cache about events that happen in the cache. Classes annotated with @CacheListener need to be public classes. In addition, the class needs to have one or more methods annotated with one of the method-level annotations (in the org.jboss.cache.notifications.annotation package). Methods annotated as such need to be public, have a void return type, and accept a single parameter of type org.jboss.cache.notifications.event.Event or one of it's subtypes.

  • @CacheStarted - methods annotated such receive a notification when the cache is started. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.CacheStartedEvent .

  • @CacheStopped - methods annotated such receive a notification when the cache is stopped. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.CacheStoppedEvent .

  • @NodeCreated - methods annotated such receive a notification when a node is created. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeCreatedEvent .

  • @NodeRemoved - methods annotated such receive a notification when a node is removed. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeRemovedEvent .

  • @NodeModified - methods annotated such receive a notification when a node is modified. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeModifiedEvent .

  • @NodeMoved - methods annotated such receive a notification when a node is moved. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeMovedEvent .

  • @NodeVisited - methods annotated such receive a notification when a node is started. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeVisitedEvent .

  • @NodeLoaded - methods annotated such receive a notification when a node is loaded from a CacheLoader . Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeLoadedEvent .

  • @NodeEvicted - methods annotated such receive a notification when a node is evicted from memory. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeEvictedEvent .

  • @NodeActivated - methods annotated such receive a notification when a node is activated. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodeActivatedEvent .

  • @NodePassivated - methods annotated such receive a notification when a node is passivated. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.NodePassivatedEvent .

  • @TransactionRegistered - methods annotated such receive a notification when the cache registers a javax.transaction.Synchronization with a registered transaction manager. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.TransactionRegisteredEvent .

  • @TransactionCompleted - methods annotated such receive a notification when the cache receives a commit or rollback call from a registered transaction manager. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.TransactionCompletedEvent .

  • @ViewChanged - methods annotated such receive a notification when the group structure of the cluster changes. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.ViewChangedEvent .

  • @CacheBlocked - methods annotated such receive a notification when the cluster requests that cache operations are blocked for a state transfer event. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.CacheBlockedEvent .

  • @CacheUnblocked - methods annotated such receive a notification when the cluster requests that cache operations are unblocked after a state transfer event. Methods need to accept a parameter type which is assignable from org.jboss.cache.notifications.event.CacheUnblockedEvent .

Refer to the javadocs on the annotations as well as the Event subtypes for details of what is passed in to your method, and when.

Example:



   @CacheListener
   public class MyListener
   {

      @CacheStarted
      @CacheStopped
      public void cacheStartStopEvent(Event e)
      {
         switch (e.getType())
         {
            case Event.Type.CACHE_STARTED:
               System.out.println("Cache has started");
               break;
            case Event.Type.CACHE_STOPPED:
               System.out.println("Cache has stopped");
               break;
         }
      }

      @NodeCreated
      @NodeRemoved
      @NodeVisited
      @NodeModified
      @NodeMoved
      public void logNodeEvent(NodeEvent ne)
      {
         log("An event on node " + ne.getFqn() + " has occured");
      }
   }