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

    • 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 created
        key - the entry's key
        value - 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 updated
        key - the entry's key
        oldValue - the entry's old value
        newValue - 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 removed
        key - the entry's key
        value - 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 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​(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 expired
        key - the entry's key
        value - 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 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.