Interface Subscription

All Superinterfaces:
AutoCloseable

public interface Subscription extends AutoCloseable
Subscription Handle - Represents an active event subscription 订阅句柄 - 表示一个活跃的事件订阅

Returned by event bus registration methods to allow precise lifecycle management of individual subscriptions. Implements AutoCloseable for try-with-resources support.

由事件总线注册方法返回,允许精确管理单个订阅的生命周期。 实现 AutoCloseable 以支持 try-with-resources。

Features | 主要功能:

  • Precise unsubscription - 精确取消订阅
  • AutoCloseable support - 自动关闭支持
  • Active state checking - 活跃状态检查

Usage Examples | 使用示例:

// Manual lifecycle management
Subscription sub = eventBus.subscribe(MyEvent.class, e -> handle(e));
// ... later
sub.unsubscribe();

// Try-with-resources
try (var sub = eventBus.subscribe(MyEvent.class, e -> handle(e))) {
    eventBus.publish(new MyEvent());
} // auto-unsubscribed

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
Since:
JDK 25, opencode-base-event V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    default void
    Close this subscription (delegates to unsubscribe) 关闭此订阅(委托给 unsubscribe)
    Class<? extends Event>
    Get the event type this subscription is for 获取此订阅的事件类型
    boolean
    Check if this subscription is still active 检查此订阅是否仍然活跃
    void
    Unsubscribe this listener from the event bus 从事件总线取消订阅此监听器
  • Method Details

    • unsubscribe

      void unsubscribe()
      Unsubscribe this listener from the event bus 从事件总线取消订阅此监听器

      Idempotent: calling multiple times has no additional effect.

      幂等:多次调用没有额外效果。

    • isActive

      boolean isActive()
      Check if this subscription is still active 检查此订阅是否仍然活跃
      Returns:
      true if the subscription is active | 如果订阅活跃返回 true
    • getEventType

      Class<? extends Event> getEventType()
      Get the event type this subscription is for 获取此订阅的事件类型
      Returns:
      the event type class | 事件类型类
    • close

      default void close()
      Close this subscription (delegates to unsubscribe) 关闭此订阅(委托给 unsubscribe)
      Specified by:
      close in interface AutoCloseable