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 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() or ignoreSynchronous().

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    (package private) final Map<Registration<K,V>,CompletableFuture<Void>>
     
    (package private) final Executor
     
    (package private) static final Logger
     
    (package private) static final ThreadLocal<List<CompletableFuture<Void>>>
     
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    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
    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.
    Returns the cache entry listener registrations.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • EventDispatcher

      public EventDispatcher(Executor executor)
  • Method Details

    • registrations

      public 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 created
      key - the entry's key
      value - 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 updated
      key - the entry's key
      oldValue - the entry's old value
      newValue - 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 removed
      key - the entry's key
      value - 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 with awaitSynchronous().
      Parameters:
      cache - the cache where the entry was removed
      key - the entry's key
      value - 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 expired
      key - the entry's key
      value - 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 with awaitSynchronous().
      Parameters:
      cache - the cache where the entry expired
      key - the entry's key
      value - 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.
    • publish

      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.