Class EventDispatcher<K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.jcache.event.EventDispatcher<K,V>
-
public final class EventDispatcher<K,V> extends Object
A dispatcher that publishes cache events to listeners for asynchronous execution.A
CacheEntryListeneris required to receive events in the order of the actions being performed on the associated key. This implementation supports this by using a dispatch queue for each listener and key pair, and provides the following characteristics:- A listener may be executed in parallel for events with different keys
- A listener is executed sequentially for events with the same key. This creates a dependency relationship between events and waiting dependents do not consume threads.
- A listener receives a single event per invocation; batch processing is not supported
- Multiple listeners may be executed in parallel for the same event
- Listeners process events at their own rate and do not explicitly block each other
- Listeners share a pool of threads for event processing. A slow listener may limit the throughput if all threads are busy handling distinct events, causing the execution of other listeners to be delayed until the executor is able to process the work.
Some listeners may be configured as synchronous, meaning that the publishing thread should wait until the listener has processed the event. The calling thread should publish within an atomic block that mutates the entry, and complete the operation by calling
awaitSynchronous()orignoreSynchronous().
-
-
Constructor Summary
Constructors Constructor Description EventDispatcher(Executor executor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidawaitSynchronous()Blocks until all of the synchronous listeners have finished processing the events this thread published.voidderegister(CacheEntryListenerConfiguration<K,V> configuration)Deregisters a cache entry listener based on the supplied configuration.voidignoreSynchronous()Ignores and clears the queued futures to the synchronous listeners that are processing events this thread published.voidpublishCreated(Cache<K,V> cache, K key, V value)Publishes a creation event for the entry to the interested listeners.voidpublishExpired(Cache<K,V> cache, K key, V value)Publishes an expiration event for the entry to the interested listeners.voidpublishExpiredQuietly(Cache<K,V> cache, K key, V value)Publishes an expiration event for the entry to the interested listeners.voidpublishRemoved(Cache<K,V> cache, K key, V value)Publishes a removal event for the entry to the interested listeners.voidpublishRemovedQuietly(Cache<K,V> cache, K key, V value)Publishes a removal event for the entry to the interested listeners.voidpublishUpdated(Cache<K,V> cache, K key, V oldValue, V newValue)Publishes an update event for the entry to the interested listeners.voidregister(CacheEntryListenerConfiguration<K,V> configuration)Registers a cache entry listener based on the supplied configuration.Set<Registration<K,V>>registrations()Returns the cache entry listener registrations.
-
-
-
Constructor Detail
-
EventDispatcher
public EventDispatcher(Executor executor)
-
-
Method Detail
-
registrations
public Set<Registration<K,V>> registrations()
Returns the cache entry listener registrations.
-
register
public void register(CacheEntryListenerConfiguration<K,V> configuration)
Registers a cache entry listener based on the supplied configuration.- Parameters:
configuration- the listener's configuration.
-
deregister
public void deregister(CacheEntryListenerConfiguration<K,V> configuration)
Deregisters a cache entry listener based on the supplied configuration.- Parameters:
configuration- the listener's configuration.
-
publishCreated
public void publishCreated(Cache<K,V> cache, K key, V value)
Publishes a creation event for the entry to the interested listeners.- Parameters:
cache- the cache where the entry was createdkey- the entry's keyvalue- the entry's value
-
publishUpdated
public void publishUpdated(Cache<K,V> cache, K key, V oldValue, V newValue)
Publishes an update event for the entry to the interested listeners.- Parameters:
cache- the cache where the entry was updatedkey- the entry's keyoldValue- the entry's old valuenewValue- the entry's new value
-
publishRemoved
public void publishRemoved(Cache<K,V> cache, K key, V value)
Publishes a removal event for the entry to the interested listeners.- Parameters:
cache- the cache where the entry was removedkey- the entry's keyvalue- the entry's value
-
publishRemovedQuietly
public void publishRemovedQuietly(Cache<K,V> cache, K key, V value)
Publishes a removal event for the entry to the interested listeners. This method does not register the synchronous listener's future withawaitSynchronous().- Parameters:
cache- the cache where the entry was removedkey- the entry's keyvalue- the entry's value
-
publishExpired
public void publishExpired(Cache<K,V> cache, K key, V value)
Publishes an expiration event for the entry to the interested listeners.- Parameters:
cache- the cache where the entry expiredkey- the entry's keyvalue- the entry's value
-
publishExpiredQuietly
public void publishExpiredQuietly(Cache<K,V> cache, K key, V value)
Publishes an expiration event for the entry to the interested listeners. This method does not register the synchronous listener's future withawaitSynchronous().- Parameters:
cache- the cache where the entry expiredkey- the entry's keyvalue- the entry's value
-
awaitSynchronous
public void awaitSynchronous()
Blocks until all of the synchronous listeners have finished processing the events this thread published.
-
ignoreSynchronous
public void ignoreSynchronous()
Ignores and clears the queued futures to the synchronous listeners that are processing events this thread published.
-
-