Class ScopedContext
java.lang.Object
cloud.opencode.base.parallel.structured.ScopedContext
Scoped Context - Scoped Values Context Management (JDK 25 JEP 501)
作用域上下文 - 作用域值上下文管理 (JDK 25 JEP 501)
Provides scoped values for context propagation in structured concurrency. ScopedValue is a modern replacement for ThreadLocal with better performance and automatic cleanup.
为结构化并发中的上下文传播提供作用域值。ScopedValue 是 ThreadLocal 的现代替代, 具有更好的性能和自动清理。
Features | 特性:
- Immutable values within scope - 作用域内值不可变
- Automatic inheritance by child virtual threads - 子虚拟线程自动继承
- No memory leaks - scope ends, value is released - 无内存泄漏 - 作用域结束,值释放
Example | 示例:
// Bind trace ID and execute
ScopedContext.runWithTraceId("trace-123", () -> {
String traceId = ScopedContext.getTraceId();
// All child threads inherit the trace ID
});
// Multiple bindings
ScopedContext.run("trace-123", "user-456", () -> {
// Both trace ID and user ID available
});
Security | 安全性:
- Thread-safe: Yes (ScopedValue is inherently thread-safe) - 线程安全: 是(ScopedValue天然线程安全)
- Since:
- JDK 25, opencode-base-parallel V1.0.0
- Author:
- Leon Soo www.LeonSoo.com
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ScopedValue<String> Request ID scoped value - 请求 ID 作用域值static final ScopedValue<String> Tenant ID scoped value - 租户 ID 作用域值static final ScopedValue<String> Trace ID scoped value - 追踪 ID 作用域值static final ScopedValue<String> User ID scoped value - 用户 ID 作用域值 -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TCalls a task with trace ID and user ID bound.static <T,R> R callWith(ScopedValue<T> scopedValue, T value, Callable<R> task) Calls a task with a custom scoped value bound.static <T> TcallWithTraceId(String traceId, Callable<T> task) Calls a task with trace ID bound.static <T> TcallWithUserId(String userId, Callable<T> task) Calls a task with user ID bound.static <T> TgetOrNull(ScopedValue<T> scopedValue) Gets a scoped value or null.static StringGets the current request ID.static StringGets the current tenant ID.static StringGets the current trace ID.static StringgetTraceIdOrDefault(String defaultValue) Gets the current trace ID or default.static StringGets the current user ID.static <T> booleanisBound(ScopedValue<T> scopedValue) Checks if a scoped value is bound.static <T> ScopedValue<T> Creates a new scoped value.static voidRuns a task with trace ID and user ID bound.static voidRuns a task with trace ID, user ID, and tenant ID bound.static <T> voidrunWith(ScopedValue<T> scopedValue, T value, Runnable task) Runs a task with a custom scoped value bound.static voidrunWithRequestId(String requestId, Runnable task) Runs a task with request ID bound.static voidrunWithTenantId(String tenantId, Runnable task) Runs a task with tenant ID bound.static voidrunWithTraceId(String traceId, Runnable task) Runs a task with trace ID bound.static voidrunWithUserId(String userId, Runnable task) Runs a task with user ID bound.
-
Field Details
-
TRACE_ID
Trace ID scoped value - 追踪 ID 作用域值 -
USER_ID
User ID scoped value - 用户 ID 作用域值 -
TENANT_ID
Tenant ID scoped value - 租户 ID 作用域值 -
REQUEST_ID
Request ID scoped value - 请求 ID 作用域值
-
-
Method Details
-
runWithTraceId
-
callWithTraceId
-
getTraceId
Gets the current trace ID. 获取当前追踪 ID。- Returns:
- the trace ID or null - 追踪 ID 或 null
-
getTraceIdOrDefault
-
runWithUserId
-
callWithUserId
-
getUserId
Gets the current user ID. 获取当前用户 ID。- Returns:
- the user ID or null - 用户 ID 或 null
-
runWithTenantId
-
getTenantId
Gets the current tenant ID. 获取当前租户 ID。- Returns:
- the tenant ID or null - 租户 ID 或 null
-
runWithRequestId
-
getRequestId
Gets the current request ID. 获取当前请求 ID。- Returns:
- the request ID or null - 请求 ID 或 null
-
run
-
call
Calls a task with trace ID and user ID bound. 使用绑定的追踪 ID 和用户 ID 调用任务。- Type Parameters:
T- the result type - 结果类型- Parameters:
traceId- the trace ID - 追踪 IDuserId- the user ID - 用户 IDtask- the task - 任务- Returns:
- the result - 结果
-
run
-
newScopedValue
Creates a new scoped value. 创建新的作用域值。- Type Parameters:
T- the value type - 值类型- Returns:
- the scoped value - 作用域值
-
runWith
Runs a task with a custom scoped value bound. 使用绑定的自定义作用域值运行任务。- Type Parameters:
T- the value type - 值类型- Parameters:
scopedValue- the scoped value - 作用域值value- the value to bind - 要绑定的值task- the task - 任务
-
callWith
Calls a task with a custom scoped value bound. 使用绑定的自定义作用域值调用任务。- Type Parameters:
T- the value type - 值类型R- the result type - 结果类型- Parameters:
scopedValue- the scoped value - 作用域值value- the value to bind - 要绑定的值task- the task - 任务- Returns:
- the result - 结果
-
isBound
Checks if a scoped value is bound. 检查作用域值是否已绑定。- Type Parameters:
T- the value type - 值类型- Parameters:
scopedValue- the scoped value - 作用域值- Returns:
- true if bound - 如果已绑定返回 true
-
getOrNull
Gets a scoped value or null. 获取作用域值或 null。- Type Parameters:
T- the value type - 值类型- Parameters:
scopedValue- the scoped value - 作用域值- Returns:
- the value or null - 值或 null
-