@Retention(value=RUNTIME) @Target(value=TYPE) public @interface CacheListener
Cache.addCacheListener(Object) and related APIs.
Note that even if a class is annotated with this annotation, it still needs method-level annotation (such as
CacheStarted) to actually receive notifications.
Objects annotated with this annotation - listeners - can be attached to a running Cache so
users can be notified of Cache events.
There can be multiple methods that are annotated to receive the same event,
and a method may receive multiple events by using a super type.
TransactionalEvent.getTransaction() can be used,
along with TransactionCompletedEvent.isSuccessful() to record events and
later process them once the transaction has been successfully committed.
Example 4 demonstrates this.
Configuration.setListenerAsyncPoolSize(int).
Summary of Notification Annotations
| Annotation | Event | Description |
|---|---|---|
CacheStarted |
CacheStartedEvent |
A cache was started |
CacheStopped |
CacheStoppedEvent |
A cache was stopped |
NodeModified |
NodeModifiedEvent |
A node was modified |
NodeMoved |
NodeMovedEvent |
A node was moved |
NodeCreated |
NodeCreatedEvent |
A node was created |
NodeRemoved |
NodeRemovedEvent |
A node was removed |
NodeVisited |
NodeVisitedEvent |
A node was visited |
NodeLoaded |
NodeLoadedEvent |
A node was loaded |
NodeEvicted |
NodeEvictedEvent |
A node was evicted |
NodeActivated |
NodeActivatedEvent |
A node was activated |
NodePassivated |
NodePassivatedEvent |
A node was passivated |
ViewChanged |
ViewChangedEvent |
A view change event was detected |
CacheBlocked |
CacheBlockedEvent |
A cache block event was detected |
CacheUnblocked |
CacheUnblockedEvent |
A cache unblock event was detected |
TransactionRegistered |
TransactionRegisteredEvent |
The cache has started to participate in a transaction |
TransactionCompleted |
TransactionCompletedEvent |
The cache has completed its participation in a transaction |
BuddyGroupChanged |
BuddyGroupChangedEvent |
Buddy replication is enabled and one of the buddy groups that the instance is a member of has changed its membership. |
NodeInvalidated |
NodeInvalidatedEvent |
A node was invalidated by a remote cache. Only if cache mode is INVALIDATION_SYNC or INVALIDATION_ASYNC. |
@CacheListener
public class SingleEventListener
{
@CacheStarted
public void doSomething(Event event)
{
System.out.println("Cache started. Details = " + event);
}
}
@CacheListener
public class MultipleEventListener
{
@CacheStarted
@CacheStopped
public void doSomething(Event event)
{
if (event.getType() == Event.Type.CACHE_STARTED)
System.out.println("Cache started. Details = " + event);
else if (event.getType() == Event.Type.CACHE_STOPPED)
System.out.println("Cache stopped. Details = " + event);
}
}
@CAcheListener
public class SingleEventListener
{
@CacheStarted
public void handleStart(Event event)
{
System.out.println("Cache started");
}
@CacheStarted
@CacheStopped
@CacheBlocked
@CacheUnblocked
@ViewChanged
public void logEvent(Event event)
{
logSystem.logEvent(event.getType());
}
}
Example 4 - Processing only events with a committed transaction.
@CacheListener
public class TxGuaranteedListener
{
private class TxEventQueue
{
private ConcurrentMap<Transaction, Queue<Event>> map = new ConcurrentHashMap<Transaction, Queue<Event>>();
public void offer(Event event)
{
Queue<Event> queue = getQueue(event.getContext().getTransaction());
queue.offer(event);
}
private Queue<Event> getQueue(Transaction transaction)
{
Queue<Event> queue = map.get(transaction);
if (queue == null)
{
queue = new ConcurrentLinkedQueue<Event>();
map.putIfAbsent(transaction, queue);
}
return queue;
}
public Queue<Event> takeAll(Transaction transaction)
{
return map.remove(transaction);
}
}
private TxEventQueue events = new TxEventQueue();
@NodeModified
@NodeMoved
@NodeCreated
@NodeRemoved
public void handle(Event event)
{
events.offer(event);
}
@TransactionCompleted
public void handleTx(TransactionCompletedEvent event)
{
Queue<Event> completed = events.takeAll(event.getTransaction());
if (completed != null && event.isSuccessful())
System.out.println("Comitted events = " + completed);
}
}
CacheStarted,
CacheStopped,
NodeModified,
NodeMoved,
NodeCreated,
NodeRemoved,
NodeVisited,
NodeLoaded,
NodeEvicted,
NodeActivated,
NodePassivated,
ViewChanged,
CacheBlocked,
CacheUnblocked,
TransactionCompleted,
TransactionRegistered,
BuddyGroupChanged,
NodeInvalidated,
Cache.addCacheListener(Object),
Cache.removeCacheListener(Object),
Cache.getCacheListeners()| Modifier and Type | Optional Element and Description |
|---|---|
boolean |
sync
Specifies whether callbacks on any class annotated with this annotation happens synchronously (in the caller's thread)
or asynchronously (using a separate thread).
|
public abstract boolean sync
Copyright © 2012 JBoss, a division of Red Hat. All Rights Reserved.