Class EventDispatcher<K,V>
- java.lang.Object
-
- com.github.benmanes.caffeine.jcache.event.EventDispatcher<K,V>
-
public final class EventDispatcher<K,V> extends java.lang.Object
A dispatcher that publishes cache events to listeners for asynchronous execution.A
CacheEntryListener
is required to receive events in the order of the actions being performed on the associated key. This implementation supports this through an actor-like model by using a dispatch queue per listener. A listener is never executed in parallel on different events, but may be executed sequentially on different threads. Batch processing of the dispatch queue is not presently supported.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()
.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) java.util.Map<Registration<K,V>,java.util.concurrent.CompletableFuture<java.lang.Void>>
dispatchQueues
(package private) java.util.concurrent.Executor
executor
(package private) static java.util.logging.Logger
logger
(package private) static java.lang.ThreadLocal<java.util.List<java.util.concurrent.CompletableFuture<java.lang.Void>>>
pending
-
Constructor Summary
Constructors Constructor Description EventDispatcher(java.util.concurrent.Executor executor)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
awaitSynchronous()
Blocks until all of the synchronous listeners have finished processing the events this thread published.void
deregister(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> configuration)
Deregisters a cache entry listener based on the supplied configuration.void
ignoreSynchronous()
Ignores and clears the queued futures to the synchronous listeners that are processing events this thread published.private void
publish(javax.cache.Cache<K,V> cache, javax.cache.event.EventType eventType, K key, boolean hasOldValue, @Nullable V oldValue, @Nullable V newValue, boolean quiet)
Broadcasts the event to all of the interested listener's dispatch queues.void
publishCreated(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a creation event for the entry to all of the interested listeners.void
publishExpired(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a expire event for the entry to all of the interested listeners.void
publishExpiredQuietly(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a expire event for the entry to all of the interested listeners.void
publishRemoved(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a remove event for the entry to all of the interested listeners.void
publishRemovedQuietly(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a remove event for the entry to all of the interested listeners.void
publishUpdated(javax.cache.Cache<K,V> cache, K key, V oldValue, V newValue)
Publishes a update event for the entry to all of the interested listeners.void
register(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> configuration)
Registers a cache entry listener based on the supplied configuration.java.util.Set<Registration<K,V>>
registrations()
Returns the cache entry listener registrations.
-
-
-
Field Detail
-
logger
static final java.util.logging.Logger logger
-
pending
static final java.lang.ThreadLocal<java.util.List<java.util.concurrent.CompletableFuture<java.lang.Void>>> pending
-
executor
final java.util.concurrent.Executor executor
-
dispatchQueues
final java.util.Map<Registration<K,V>,java.util.concurrent.CompletableFuture<java.lang.Void>> dispatchQueues
-
-
Method Detail
-
registrations
public java.util.Set<Registration<K,V>> registrations()
Returns the cache entry listener registrations.
-
register
public void register(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> configuration)
Registers a cache entry listener based on the supplied configuration.- Parameters:
configuration
- the listener's configuration.
-
deregister
public void deregister(javax.cache.configuration.CacheEntryListenerConfiguration<K,V> configuration)
Deregisters a cache entry listener based on the supplied configuration.- Parameters:
configuration
- the listener's configuration.
-
publishCreated
public void publishCreated(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a creation event for the entry to all of the interested listeners.- Parameters:
cache
- the cache where the entry was createdkey
- the entry's keyvalue
- the entry's value
-
publishUpdated
public void publishUpdated(javax.cache.Cache<K,V> cache, K key, V oldValue, V newValue)
Publishes a update event for the entry to all of 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(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a remove event for the entry to all of the interested listeners.- Parameters:
cache
- the cache where the entry was removedkey
- the entry's keyvalue
- the entry's value
-
publishRemovedQuietly
public void publishRemovedQuietly(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a remove event for the entry to all of 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(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a expire event for the entry to all of the interested listeners.- Parameters:
cache
- the cache where the entry expiredkey
- the entry's keyvalue
- the entry's value
-
publishExpiredQuietly
public void publishExpiredQuietly(javax.cache.Cache<K,V> cache, K key, V value)
Publishes a expire event for the entry to all of 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.
-
-