Class RequestContextHolder
java.lang.Object
cloud.opencode.base.web.context.RequestContextHolder
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 Summary
Modifier and TypeMethodDescriptionstatic voidclear()Clear the request context 清除请求上下文static voidexecute(RequestContext context, Runnable runnable) Execute runnable with context 使用上下文执行Runnablestatic <T> Texecute(RequestContext context, Supplier<T> supplier) Execute supplier with context 使用上下文执行Supplierstatic RequestContextGet the request context 获取请求上下文static RequestContextgetOrCreate(Supplier<RequestContext> supplier) Get the request context, creating if absent 获取请求上下文,如果不存在则创建static StringGet trace ID from current context 从当前上下文获取追踪IDstatic UserContextgetUser()Get user context from current context 从当前上下文获取用户上下文static StringGet user ID from current context 从当前上下文获取用户IDstatic booleanCheck if context exists 检查上下文是否存在static voidsetContext(RequestContext context) Set the request context 设置请求上下文static RunnableWrap runnable with current context 使用当前上下文包装Runnablestatic <T> Callable<T> Wrap callable with current context 使用当前上下文包装Callablestatic <T> Supplier<T> Wrap supplier with current context 使用当前上下文包装Supplier
-
Method Details
-
setContext
Set the request context 设置请求上下文- Parameters:
context- the context to set | 要设置的上下文
-
getContext
Get the request context 获取请求上下文- Returns:
- the current context or null | 当前上下文或null
-
getOrCreate
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
Get trace ID from current context 从当前上下文获取追踪ID- Returns:
- the trace ID or null | 追踪ID或null
-
getUser
Get user context from current context 从当前上下文获取用户上下文- Returns:
- the user context or null | 用户上下文或null
-
getUserId
Get user ID from current context 从当前上下文获取用户ID- Returns:
- the user ID or null | 用户ID或null
-
wrap
-
wrap
-
wrap
-
execute
Execute runnable with context 使用上下文执行Runnable- Parameters:
context- the context to use | 要使用的上下文runnable- the runnable to execute | 要执行的Runnable
-
execute
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 | 结果
-