Class PoolContext

java.lang.Object
cloud.opencode.base.pool.PoolContext

public final class PoolContext extends Object
Pool Context - ScopedValue-based Pool Context 池上下文 - 基于 ScopedValue 的池上下文

Uses JDK 25 ScopedValue for efficient context propagation in Virtual Threads.

使用 JDK 25 ScopedValue 实现虚拟线程中高效的上下文传播。

Features | 主要功能:

  • ScopedValue-based context - 基于 ScopedValue 的上下文
  • Virtual Thread friendly - 虚拟线程友好
  • Borrow tracking - 借用追踪
  • Context attributes - 上下文属性

Usage Examples | 使用示例:

PoolContext context = PoolContext.create("connectionPool");
PoolContext.run(context, () -> {
    // Access current context
    PoolContext current = PoolContext.current().orElseThrow();
    System.out.println("Pool: " + current.poolName());

    // Do work with pooled objects
    return result;
});

Security | 安全性:

  • Thread-safe: Partially (ConcurrentHashMap attributes, volatile fields) - 线程安全: 部分(ConcurrentHashMap属性,volatile字段)
  • Null-safe: No - 空值安全: 否
Since:
JDK 25, opencode-base-pool V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Field Details

    • CURRENT

      public static final ScopedValue<PoolContext> CURRENT
      Current pool context (ScopedValue) 当前池上下文 (ScopedValue)
  • Method Details

    • create

      public static PoolContext create(String poolName)
      Creates a new pool context 创建新的池上下文
      Parameters:
      poolName - the pool name | 池名称
      Returns:
      new context | 新上下文
    • create

      public static PoolContext create()
      Creates a new pool context with default name 使用默认名称创建新的池上下文
      Returns:
      new context | 新上下文
    • run

      public static <T, X extends Throwable> T run(PoolContext context, ScopedValue.CallableOp<T,X> task) throws X
      Runs a callable within this context 在此上下文中运行可调用对象
      Type Parameters:
      T - return type | 返回类型
      X - exception type | 异常类型
      Parameters:
      context - the pool context | 池上下文
      task - the task to run | 要运行的任务
      Returns:
      task result | 任务结果
      Throws:
      X - if task throws | 如果任务抛出异常
    • run

      public static void run(PoolContext context, Runnable task)
      Runs a runnable within this context 在此上下文中运行可运行对象
      Parameters:
      context - the pool context | 池上下文
      task - the task to run | 要运行的任务
    • current

      public static Optional<PoolContext> current()
      Gets the current pool context 获取当前池上下文
      Returns:
      current context or empty | 当前上下文或空
    • currentOrCreate

      public static PoolContext currentOrCreate()
      Gets the current context or creates a new one 获取当前上下文或创建新的
      Returns:
      current or new context | 当前或新的上下文
    • recordBorrow

      public void recordBorrow(Object object)
      Records object borrow 记录对象借用
      Parameters:
      object - the borrowed object | 借用的对象
    • recordReturn

      public void recordReturn()
      Records object return 记录对象归还
    • hasBorrowedObject

      public boolean hasBorrowedObject()
      Checks if an object is currently borrowed 检查是否有对象当前被借用
      Returns:
      true if borrowed | 如果已借用返回 true
    • setAttribute

      public PoolContext setAttribute(String key, Object value)
      Sets an attribute 设置属性
      Parameters:
      key - attribute key | 属性键
      value - attribute value | 属性值
      Returns:
      this context | 此上下文
    • getAttribute

      public <T> Optional<T> getAttribute(String key)
      Gets an attribute 获取属性
      Type Parameters:
      T - value type | 值类型
      Parameters:
      key - attribute key | 属性键
      Returns:
      attribute value or empty | 属性值或空
    • hasAttribute

      public boolean hasAttribute(String key)
      Checks if attribute exists 检查属性是否存在
      Parameters:
      key - attribute key | 属性键
      Returns:
      true if exists | 如果存在返回 true
    • poolName

      public String poolName()
      Gets the pool name 获取池名称
      Returns:
      pool name | 池名称
    • createdAt

      public Instant createdAt()
      Gets creation time 获取创建时间
      Returns:
      creation time | 创建时间
    • thread

      public Thread thread()
      Gets the thread 获取线程
      Returns:
      thread | 线程
    • borrowTime

      public Optional<Instant> borrowTime()
      Gets borrow time 获取借用时间
      Returns:
      borrow time or empty | 借用时间或空
    • returnTime

      public Optional<Instant> returnTime()
      Gets return time 获取归还时间
      Returns:
      return time or empty | 归还时间或空
    • isVirtualThread

      public boolean isVirtualThread()
      Checks if on virtual thread 检查是否在虚拟线程上
      Returns:
      true if virtual thread | 如果是虚拟线程返回 true
    • toString

      public String toString()
      Overrides:
      toString in class Object