Class RequestContextHolder

java.lang.Object
cloud.opencode.base.web.context.RequestContextHolder

public final class RequestContextHolder extends Object
Request Context Holder 请求上下文持有者

ThreadLocal-based holder for request context.

基于ThreadLocal的请求上下文持有者。

Features | 主要功能:

  • Thread-safe context storage - 线程安全的上下文存储
  • Async task support - 异步任务支持
  • Auto cleanup - 自动清理

Usage Examples | 使用示例:

// Set context
RequestContextHolder.setContext(context);

// Get context
RequestContext ctx = RequestContextHolder.getContext();
String traceId = RequestContextHolder.getTraceId();

// Wrap async task with context
Runnable wrapped = RequestContextHolder.wrap(task);
Since:
JDK 25, opencode-base-web V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • setContext

      public static void setContext(RequestContext context)
      Set the request context 设置请求上下文
      Parameters:
      context - the context to set | 要设置的上下文
    • getContext

      public static RequestContext getContext()
      Get the request context 获取请求上下文
      Returns:
      the current context or null | 当前上下文或null
    • getOrCreate

      public static RequestContext getOrCreate(Supplier<RequestContext> supplier)
      Get the request context, creating if absent 获取请求上下文,如果不存在则创建
      Parameters:
      supplier - the supplier to create context | 创建上下文的供应者
      Returns:
      the context | 上下文
    • clear

      public static void clear()
      Clear the request context 清除请求上下文
    • hasContext

      public static boolean hasContext()
      Check if context exists 检查上下文是否存在
      Returns:
      true if exists | 如果存在返回true
    • getTraceId

      public static String getTraceId()
      Get trace ID from current context 从当前上下文获取追踪ID
      Returns:
      the trace ID or null | 追踪ID或null
    • getUser

      public static UserContext getUser()
      Get user context from current context 从当前上下文获取用户上下文
      Returns:
      the user context or null | 用户上下文或null
    • getUserId

      public static String getUserId()
      Get user ID from current context 从当前上下文获取用户ID
      Returns:
      the user ID or null | 用户ID或null
    • wrap

      public static Runnable wrap(Runnable runnable)
      Wrap runnable with current context 使用当前上下文包装Runnable
      Parameters:
      runnable - the runnable to wrap | 要包装的Runnable
      Returns:
      the wrapped runnable | 包装后的Runnable
    • wrap

      public static <T> Callable<T> wrap(Callable<T> callable)
      Wrap callable with current context 使用当前上下文包装Callable
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      callable - the callable to wrap | 要包装的Callable
      Returns:
      the wrapped callable | 包装后的Callable
    • wrap

      public static <T> Supplier<T> wrap(Supplier<T> supplier)
      Wrap supplier with current context 使用当前上下文包装Supplier
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      supplier - the supplier to wrap | 要包装的Supplier
      Returns:
      the wrapped supplier | 包装后的Supplier
    • execute

      public static void execute(RequestContext context, Runnable runnable)
      Execute runnable with context 使用上下文执行Runnable
      Parameters:
      context - the context to use | 要使用的上下文
      runnable - the runnable to execute | 要执行的Runnable
    • execute

      public static <T> T execute(RequestContext context, Supplier<T> supplier)
      Execute supplier with context 使用上下文执行Supplier
      Type Parameters:
      T - the result type | 结果类型
      Parameters:
      context - the context to use | 要使用的上下文
      supplier - the supplier to execute | 要执行的Supplier
      Returns:
      the result | 结果