Class AsyncDispatcher

java.lang.Object
cloud.opencode.base.event.dispatcher.AsyncDispatcher
All Implemented Interfaces:
EventDispatcher

public class AsyncDispatcher extends Object implements EventDispatcher
Asynchronous Event Dispatcher 异步事件分发器

Dispatches events asynchronously using virtual threads.

使用虚拟线程异步分发事件。

Features | 主要功能:

  • Virtual thread execution - 虚拟线程执行
  • Non-blocking processing - 非阻塞处理
  • Parallel listener execution - 并行监听器执行

Usage Examples | 使用示例:

AsyncDispatcher dispatcher = new AsyncDispatcher();
dispatcher.dispatch(event, listeners);

// With custom executor
AsyncDispatcher customDispatcher = new AsyncDispatcher(myExecutor);

Security | 安全性:

  • Thread-safe: Yes (delegates to thread-safe executor) - 线程安全: 是(委托给线程安全执行器)
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Constructor Details

    • AsyncDispatcher

      public AsyncDispatcher()
      Create async dispatcher with default virtual thread executor 使用默认虚拟线程执行器创建异步分发器
    • AsyncDispatcher

      public AsyncDispatcher(ExecutorService executor)
      Create async dispatcher with custom executor 使用自定义执行器创建异步分发器
      Parameters:
      executor - the executor service to use | 要使用的执行器服务
  • Method Details

    • dispatch

      public void dispatch(Event event, List<Consumer<Event>> listeners)
      Dispatch event to listeners asynchronously 异步将事件分发到监听器

      Each listener is executed in a separate virtual thread. Event cancellation is checked before each listener invocation.

      每个监听器在单独的虚拟线程中执行。 在每次监听器调用前检查事件是否被取消。

      Specified by:
      dispatch in interface EventDispatcher
      Parameters:
      event - the event to dispatch | 要分发的事件
      listeners - the list of listener handlers | 监听器处理器列表
    • dispatchAsync

      public CompletableFuture<Void> dispatchAsync(Event event, List<Consumer<Event>> listeners)
      Dispatch event and return a future for completion 分发事件并返回完成的Future
      Parameters:
      event - the event to dispatch | 要分发的事件
      listeners - the list of listener handlers | 监听器处理器列表
      Returns:
      CompletableFuture that completes when all listeners finish | 当所有监听器完成时完成的CompletableFuture
    • isAsync

      public boolean isAsync()
      Description copied from interface: EventDispatcher
      Check if this dispatcher supports async dispatching 检查此分发器是否支持异步分发
      Specified by:
      isAsync in interface EventDispatcher
      Returns:
      true if supports async | 如果支持异步返回true
    • shutdown

      public void shutdown()
      Shutdown the dispatcher and release resources 关闭分发器并释放资源
      Specified by:
      shutdown in interface EventDispatcher
    • getExecutor

      public ExecutorService getExecutor()
      Get the underlying executor service 获取底层执行器服务
      Returns:
      the executor service | 执行器服务