Class CacheEventDispatcher<K,V>

java.lang.Object
cloud.opencode.base.cache.event.CacheEventDispatcher<K,V>
Type Parameters:
K - key type | 键类型
V - value type | 值类型

Security | 安全性:

  • Thread-safe: Yes (uses CopyOnWriteArrayList and atomic operations) - 线程安全: 是(使用 CopyOnWriteArrayList 和原子操作)
  • Null-safe: Yes - 空值安全: 是
All Implemented Interfaces:
AutoCloseable

public final class CacheEventDispatcher<K,V> extends Object implements AutoCloseable
Cache Event Dispatcher - Manages event listeners and dispatches events 缓存事件分发器 - 管理事件监听器并分发事件

Central component for the cache event system. Supports synchronous and asynchronous event dispatch with configurable error handling.

缓存事件系统的核心组件。支持同步和异步事件分发,可配置错误处理。

Features | 主要功能:

  • Synchronous and async dispatch - 同步和异步分发
  • Event filtering by type - 按类型过滤事件
  • Error isolation - 错误隔离
  • Metrics collection - 指标收集
  • Graceful shutdown - 优雅关闭

Usage Examples | 使用示例:

// Create dispatcher - 创建分发器
CacheEventDispatcher<String, User> dispatcher = CacheEventDispatcher.create();

// Register listeners - 注册监听器
dispatcher.addListener(event -> log.info("Event: {}", event));
dispatcher.addListener(CacheEventListener.onEvict(event ->
    metrics.recordEviction(event.cacheName())));

// Dispatch events - 分发事件
dispatcher.dispatch(CacheEvent.put("users", "user1", user));

// Async dispatch - 异步分发
dispatcher.dispatchAsync(CacheEvent.evict("users", "user2", oldUser, RemovalCause.SIZE));

// Shutdown - 关闭
dispatcher.close();
Since:
JDK 25, opencode-base-cache V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • create

      public static <K,V> CacheEventDispatcher<K,V> create()
      Create dispatcher with default settings 使用默认设置创建分发器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      dispatcher | 分发器
    • builder

      public static <K,V> CacheEventDispatcher.Builder<K,V> builder()
      Create dispatcher builder 创建分发器构建器
      Type Parameters:
      K - key type | 键类型
      V - value type | 值类型
      Returns:
      builder | 构建器
    • addListener

      public CacheEventDispatcher<K,V> addListener(CacheEventListener<K,V> listener)
      Add event listener 添加事件监听器
      Parameters:
      listener - the listener | 监听器
      Returns:
      this dispatcher | 此分发器
    • removeListener

      public boolean removeListener(CacheEventListener<K,V> listener)
      Remove event listener 移除事件监听器
      Parameters:
      listener - the listener | 监听器
      Returns:
      true if removed | 如果移除成功返回 true
    • clearListeners

      public void clearListeners()
      Remove all listeners 移除所有监听器
    • listenerCount

      public int listenerCount()
      Get number of registered listeners 获取注册的监听器数量
      Returns:
      listener count | 监听器数量
    • dispatch

      public void dispatch(CacheEvent<K,V> event)
      Dispatch event synchronously to all listeners 同步分发事件到所有监听器
      Parameters:
      event - the event | 事件
    • dispatchAsync

      public CompletableFuture<Void> dispatchAsync(CacheEvent<K,V> event)
      Dispatch event asynchronously 异步分发事件
      Parameters:
      event - the event | 事件
      Returns:
      future completed when all listeners processed | 所有监听器处理完成时完成的 Future
    • dispatchAll

      public void dispatchAll(Iterable<CacheEvent<K,V>> events)
      Dispatch multiple events synchronously 同步分发多个事件
      Parameters:
      events - the events | 事件集合
    • dispatchAllAsync

      public CompletableFuture<Void> dispatchAllAsync(Iterable<CacheEvent<K,V>> events)
      Dispatch multiple events asynchronously 异步分发多个事件
      Parameters:
      events - the events | 事件集合
      Returns:
      future completed when all events processed | 所有事件处理完成时完成的 Future
    • dispatchWithTimeout

      public boolean dispatchWithTimeout(CacheEvent<K,V> event, Duration timeout)
      Dispatch event with timeout (V2.0.4) 带超时分发事件
      Parameters:
      event - the event | 事件
      timeout - max wait time | 最大等待时间
      Returns:
      true if completed within timeout | 如果在超时内完成返回 true
      Since:
      V2.0.4
    • dispatchTo

      public void dispatchTo(CacheEvent<K,V> event, CacheEventListener<K,V> listener)
      Dispatch event to specific listener only (V2.0.4) 仅分发事件到特定监听器
      Parameters:
      event - the event | 事件
      listener - the target listener | 目标监听器
      Since:
      V2.0.4
    • hasListenersFor

      public boolean hasListenersFor(CacheEvent.EventType eventType)
      Check if any listener is interested in the event type (V2.0.4) 检查是否有任何监听器对事件类型感兴趣
      Parameters:
      eventType - the event type | 事件类型
      Returns:
      true if any listener interested | 如果有监听器感兴趣返回 true
      Since:
      V2.0.4
    • getMetrics

      public CacheEventDispatcher.Metrics getMetrics()
      Get dispatcher metrics 获取分发器指标
      Returns:
      metrics | 指标
    • resetMetrics

      public void resetMetrics()
      Reset metrics counters 重置指标计数器
    • isClosed

      public boolean isClosed()
      Check if dispatcher is closed 检查分发器是否已关闭
      Returns:
      true if closed | 如果已关闭返回 true
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable