mirror of
https://github.com/hs-web/hsweb-framework.git
synced 2026-09-03 06:35:25 +08:00
refactor: 优化上下文处理
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package org.hswebframework.web.context;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import reactor.util.context.Context;
|
||||
import reactor.util.context.ContextView;
|
||||
|
||||
@@ -9,6 +10,7 @@ import java.io.Closeable;
|
||||
* 基于 ThreadLocal 的上下文持有器支持实现
|
||||
* 适用于传统平台线程环境
|
||||
*/
|
||||
@Slf4j
|
||||
public class ThreadLocalContextHolderSupport implements ContextHolder.ContextHolderSupport {
|
||||
|
||||
private static final ThreadLocal<Context> contextHolder = ThreadLocal.withInitial(Context::empty);
|
||||
@@ -23,8 +25,16 @@ public class ThreadLocalContextHolderSupport implements ContextHolder.ContextHol
|
||||
Context previous = contextHolder.get();
|
||||
Context newContext = previous.putAll(context);
|
||||
contextHolder.set(newContext);
|
||||
Thread bound = Thread.currentThread();
|
||||
|
||||
return () -> contextHolder.set(previous);
|
||||
return () -> {
|
||||
Thread current = Thread.currentThread();
|
||||
if (current != bound) {
|
||||
log.warn("Context holder is cross thread {}=>{} {}", bound, current, context);
|
||||
} else {
|
||||
contextHolder.set(previous);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user