Class OpenEvent
java.lang.Object
cloud.opencode.base.event.OpenEvent
- All Implemented Interfaces:
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 ClassesModifier and TypeClassDescriptionstatic classBuilder for OpenEvent OpenEvent构建器 -
Method Summary
Modifier and TypeMethodDescriptionstatic OpenEvent.Builderbuilder()Create a builder for custom configuration 创建用于自定义配置的构建器voidclose()Shuts down the async executor and dispatchers, releasing resources.static OpenEventcreate()Create a new OpenEvent instance 创建新的OpenEvent实例static OpenEventGet the default singleton instance 获取默认单例实例Get the event store 获取事件存储<E extends Event>
voidon(Class<E> eventType, EventListener<E> listener) Register a lambda listener for an event type 为事件类型注册Lambda监听器<E extends Event>
voidon(Class<E> eventType, EventListener<E> listener, boolean async) Register a lambda listener with async option 使用异步选项注册Lambda监听器<E extends Event>
voidon(Class<E> eventType, EventListener<E> listener, boolean async, int priority) Register a lambda listener with async and priority options 使用异步和优先级选项注册Lambda监听器voidPublish an event synchronously 同步发布事件<T> voidpublish(T data) Publish data as a DataEvent 将数据作为DataEvent发布<T> voidPublish data as a DataEvent with source 将数据作为带来源的DataEvent发布booleanpublishAndWait(Event event, Duration timeout) Publish and wait for processing to complete 发布并等待处理完成publishAsync(Event event) Publish an event asynchronously 异步发布事件voidRegister an object's @Subscribe methods as event listeners 将对象的@Subscribe方法注册为事件监听器voidsetEventStore(EventStore store) Set the event store 设置事件存储voidsetExceptionHandler(EventExceptionHandler handler) Set the exception handler 设置异常处理器voidunregister(Object subscriber) Unregister all listeners from a subscriber 从订阅者注销所有监听器
-
Method Details
-
getDefault
Get the default singleton instance 获取默认单例实例- Returns:
- the default OpenEvent instance | 默认OpenEvent实例
-
create
Create a new OpenEvent instance 创建新的OpenEvent实例- Returns:
- new OpenEvent instance | 新的OpenEvent实例
-
builder
Create a builder for custom configuration 创建用于自定义配置的构建器- Returns:
- new Builder | 新的Builder
-
register
Register an object's @Subscribe methods as event listeners 将对象的@Subscribe方法注册为事件监听器- Parameters:
subscriber- the subscriber object | 订阅者对象- Throws:
EventException- if method signature is invalid | 如果方法签名无效
-
on
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
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 | 异步执行为truepriority- listener priority (higher = earlier) | 监听器优先级(越高越早)
-
unregister
Unregister all listeners from a subscriber 从订阅者注销所有监听器- Parameters:
subscriber- the subscriber to unregister | 要注销的订阅者
-
publish
Publish an event synchronously 同步发布事件- Parameters:
event- the event to publish | 要发布的事件
-
publishAsync
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
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
-
setEventStore
Set the event store 设置事件存储- Parameters:
store- the event store | 事件存储
-
getEventStore
Get the event store 获取事件存储- Returns:
- the event store or null | 事件存储或null
-
setExceptionHandler
Set the exception handler 设置异常处理器- Parameters:
handler- the exception handler | 异常处理器
-
close
public void close()Shuts down the async executor and dispatchers, releasing resources. 关闭异步执行器和分发器,释放资源。- Specified by:
closein interfaceAutoCloseable
-