@Retention(value=RUNTIME) @Target(value=TYPE) public @interface Listener
Listenable.addListener(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.getGlobalTransaction()
can be used, along with TransactionCompletedEvent.isTransactionSuccessful()
to record events and later process them once the transaction has been successfully committed. Example 4 demonstrates
this. org.infinispan.config.GlobalConfiguration#setAsyncListenerExecutorProperties(java.util.Properties)
and org.infinispan.config.GlobalConfiguration#setAsyncListenerExecutorFactoryClass(String)
.
Summary of Notification Annotations Annotation | Event | Description |
---|---|---|
CacheStarted | CacheStartedEvent | A cache was started |
CacheStopped |
CacheStoppedEvent | A cache was stopped |
CacheEntryModified |
CacheEntryModifiedEvent | A cache entry was modified |
CacheEntryCreated | CacheEntryCreatedEvent | A cache entry was created |
CacheEntryRemoved |
CacheEntryRemovedEvent | A cache entry was removed |
CacheEntryVisited |
CacheEntryVisitedEvent | A cache entry was visited |
CacheEntryLoaded |
CacheEntryLoadedEvent | A cache entry was loaded |
CacheEntriesEvicted |
CacheEntriesEvictedEvent | A cache entries were evicted |
CacheEntryActivated |
CacheEntryActivatedEvent | A cache entry was activated |
CacheEntryPassivated | CacheEntryPassivatedEvent | One or more cache entries were passivated |
ViewChanged |
ViewChangedEvent | A view change 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 |
CacheEntryInvalidated | CacheEntryInvalidatedEvent | A cache entry was invalidated by a remote cache. Only if cache mode is INVALIDATION_SYNC or INVALIDATION_ASYNC. |
@Listener public class SingleEventListener { @CacheStarted public void doSomething(Event event) { System.out.println("Cache started. Details = " + event); } }
@Listener 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); } }
@Listener 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.
@Listener public class EventHandler { private ConcurrentMap<GlobalTransaction, Queue<Event>> map = new ConcurrentHashMap<GlobalTransaction, Queue<Event>>(); @TransactionRegistered public void startTransaction(TransactionRegisteredEvent event) { map.put(event.getGlobalTransaction(), new ConcurrentLinkedQueue<Event>()); } @CacheEntryCreated @CacheEntryModified @CacheEntryRemoved public void addEvent(TransactionalEvent event) { map.get(event.getGlobalTransaction()).add(event); } @TransactionCompleted public void endTransaction(TransactionCompletedEvent event) { Queue<Event> events = map.get(event.getGlobalTransaction()); map.remove(event.getGlobalTransaction()); System.out.println("Ended transaction " + event.getGlobalTransaction().getId()); if(event.isTransactionSuccessful()) { for(Event e : events) { System.out.println("Event " + e); } } } }
CacheStarted
,
CacheStopped
,
CacheEntryModified
,
CacheEntryCreated
,
CacheEntryRemoved
,
CacheEntryVisited
,
CacheEntryLoaded
,
CacheEntriesEvicted
,
CacheEntryActivated
,
CacheEntryPassivated
,
ViewChanged
,
TransactionCompleted
,
TransactionRegistered
,
CacheEntryInvalidated
,
DataRehashed
,
TopologyChanged
,
PartitionStatusChanged
,
PersistenceAvailabilityChanged
Modifier and Type | Optional Element and Description |
---|---|
boolean |
clustered
Defines whether the annotated listener is clustered or not.
|
boolean |
includeCurrentState
If set to true then the entire existing state within the cluster is
evaluated.
|
Listener.Observation |
observation
Returns the type of observation level this listener defines.
|
boolean |
primaryOnly
Specifies whether the event should be fired on the primary data owner of the affected key, or all nodes that see
the update.
|
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
public abstract boolean primaryOnly
Note that is value is ignored when clustered()
is true.
public abstract boolean clustered
public abstract boolean includeCurrentState
clustered()
.
If using a distributed clustered cache it is possible to retrieve new events before the initial transfer is completed. This is handled since only new events are queued until the segment it belongs to is completed for iteration. This also will help reduce memory strain since a distributed clustered listener will need to eventually retrieve all values from the cache.
public abstract Listener.Observation observation
Listener.Observation
Copyright © 2020 JBoss, a division of Red Hat. All rights reserved.