Class OpenEvent

java.lang.Object
cloud.opencode.base.event.OpenEvent
All Implemented Interfaces:
AutoCloseable

public final class OpenEvent extends Object implements AutoCloseable
OpenEvent - Event Bus 事件总线

The main entry point for event-driven architecture support.

事件驱动架构支持的主入口点。

Features | 主要功能:

  • Publish/Subscribe pattern - 发布/订阅模式
  • Sync/Async event processing - 同步/异步事件处理
  • Event priority - 事件优先级
  • Event cancellation - 事件取消
  • Event sourcing support - 事件溯源支持
  • Virtual thread async processing - 虚拟线程异步处理

Usage Examples | 使用示例:

// Get default instance
OpenEvent eventBus = OpenEvent.getDefault();

// Register annotation-based listener
eventBus.register(new MyEventHandler());

// Register lambda listener
eventBus.on(UserRegisteredEvent.class, event -> {
    System.out.println("User registered: " + event.getUserId());
});

// Publish event
eventBus.publish(new UserRegisteredEvent(userId, email));

// Publish async
eventBus.publishAsync(event).thenRun(() -> log.info("Done"));

// Create custom instance
OpenEvent custom = OpenEvent.builder()
    .eventStore(new InMemoryEventStore(10000))
    .exceptionHandler(new LoggingExceptionHandler())
    .build();

Security | 安全性:

  • Thread-safe: Yes (concurrent data structures) - 线程安全: 是(并发数据结构)
Since:
JDK 25, opencode-base-event V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
    Builder for OpenEvent OpenEvent构建器
  • Method Summary

    Modifier and Type
    Method
    Description
    Create a builder for custom configuration 创建用于自定义配置的构建器
    void
    Shuts down the async executor and dispatchers, releasing resources.
    static OpenEvent
    Create a new OpenEvent instance 创建新的OpenEvent实例
    static OpenEvent
    Get the default singleton instance 获取默认单例实例
    Get the event store 获取事件存储
    <E extends Event>
    void
    on(Class<E> eventType, EventListener<E> listener)
    Register a lambda listener for an event type 为事件类型注册Lambda监听器
    <E extends Event>
    void
    on(Class<E> eventType, EventListener<E> listener, boolean async)
    Register a lambda listener with async option 使用异步选项注册Lambda监听器
    <E extends Event>
    void
    on(Class<E> eventType, EventListener<E> listener, boolean async, int priority)
    Register a lambda listener with async and priority options 使用异步和优先级选项注册Lambda监听器
    void
    publish(Event event)
    Publish an event synchronously 同步发布事件
    <T> void
    publish(T data)
    Publish data as a DataEvent 将数据作为DataEvent发布
    <T> void
    publish(T data, String source)
    Publish data as a DataEvent with source 将数据作为带来源的DataEvent发布
    boolean
    publishAndWait(Event event, Duration timeout)
    Publish and wait for processing to complete 发布并等待处理完成
    Publish an event asynchronously 异步发布事件
    void
    register(Object subscriber)
    Register an object's @Subscribe methods as event listeners 将对象的@Subscribe方法注册为事件监听器
    void
    Set the event store 设置事件存储
    void
    Set the exception handler 设置异常处理器
    void
    unregister(Object subscriber)
    Unregister all listeners from a subscriber 从订阅者注销所有监听器

    Methods inherited from class Object

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

    • getDefault

      public static OpenEvent getDefault()
      Get the default singleton instance 获取默认单例实例
      Returns:
      the default OpenEvent instance | 默认OpenEvent实例
    • create

      public static OpenEvent create()
      Create a new OpenEvent instance 创建新的OpenEvent实例
      Returns:
      new OpenEvent instance | 新的OpenEvent实例
    • builder

      public static OpenEvent.Builder builder()
      Create a builder for custom configuration 创建用于自定义配置的构建器
      Returns:
      new Builder | 新的Builder
    • register

      public void register(Object subscriber)
      Register an object's @Subscribe methods as event listeners 将对象的@Subscribe方法注册为事件监听器
      Parameters:
      subscriber - the subscriber object | 订阅者对象
      Throws:
      EventException - if method signature is invalid | 如果方法签名无效
    • on

      public <E extends Event> void on(Class<E> eventType, EventListener<E> listener)
      Register a lambda listener for an event type 为事件类型注册Lambda监听器
      Type Parameters:
      E - the event type parameter | 事件类型参数
      Parameters:
      eventType - the event type class | 事件类型类
      listener - the event listener | 事件监听器
    • on

      public <E extends Event> void on(Class<E> eventType, EventListener<E> listener, boolean async)
      Register a lambda listener with async option 使用异步选项注册Lambda监听器
      Type Parameters:
      E - the event type parameter | 事件类型参数
      Parameters:
      eventType - the event type class | 事件类型类
      listener - the event listener | 事件监听器
      async - true for async execution | 异步执行为true
    • on

      public <E extends Event> void on(Class<E> eventType, EventListener<E> listener, boolean async, int priority)
      Register a lambda listener with async and priority options 使用异步和优先级选项注册Lambda监听器
      Type Parameters:
      E - the event type parameter | 事件类型参数
      Parameters:
      eventType - the event type class | 事件类型类
      listener - the event listener | 事件监听器
      async - true for async execution | 异步执行为true
      priority - listener priority (higher = earlier) | 监听器优先级(越高越早)
    • unregister

      public void unregister(Object subscriber)
      Unregister all listeners from a subscriber 从订阅者注销所有监听器
      Parameters:
      subscriber - the subscriber to unregister | 要注销的订阅者
    • publish

      public void publish(Event event)
      Publish an event synchronously 同步发布事件
      Parameters:
      event - the event to publish | 要发布的事件
    • publishAsync

      public CompletableFuture<Void> publishAsync(Event event)
      Publish an event asynchronously 异步发布事件
      Parameters:
      event - the event to publish | 要发布的事件
      Returns:
      CompletableFuture that completes when processing is done | 处理完成时完成的CompletableFuture
    • publish

      public <T> void publish(T data)
      Publish data as a DataEvent 将数据作为DataEvent发布
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      data - the data to publish | 要发布的数据
    • publish

      public <T> void publish(T data, String source)
      Publish data as a DataEvent with source 将数据作为带来源的DataEvent发布
      Type Parameters:
      T - the data type | 数据类型
      Parameters:
      data - the data to publish | 要发布的数据
      source - the event source | 事件来源
    • publishAndWait

      public boolean publishAndWait(Event event, Duration timeout)
      Publish and wait for processing to complete 发布并等待处理完成
      Parameters:
      event - the event to publish | 要发布的事件
      timeout - max time to wait | 最大等待时间
      Returns:
      true if completed within timeout | 如果在超时内完成返回true
    • setEventStore

      public void setEventStore(EventStore store)
      Set the event store 设置事件存储
      Parameters:
      store - the event store | 事件存储
    • getEventStore

      public EventStore getEventStore()
      Get the event store 获取事件存储
      Returns:
      the event store or null | 事件存储或null
    • setExceptionHandler

      public void setExceptionHandler(EventExceptionHandler handler)
      Set the exception handler 设置异常处理器
      Parameters:
      handler - the exception handler | 异常处理器
    • close

      public void close()
      Shuts down the async executor and dispatchers, releasing resources. 关闭异步执行器和分发器,释放资源。
      Specified by:
      close in interface AutoCloseable