public class SttlInterceptor
可传递ScopedTransferableThreadLocal的拦截器, 只传递ScopedTransferableThreadLocal, 无关ThreadLocal
特性: worker thread 在执行任务时, 会传递 caller thread的 ScopedTransferableThreadLocal数据
目标: 主要是为了解决异步执行时, 线程状态(ScopedTransferableThreadLocal)的传递问题, 如 jkmvc 将当前 Db/HttpRequest 等对象都是记录到 ScopedTransferableThreadLocal对象中, 以方便访问, 但是一旦异步执行后就丢失了
实现: 在执行之前传递一下 ScopedTransferableThreadLocal对象, 在执行后就恢复一下 ScopedTransferableThreadLocal对象
优化: 所有 intercept()方法的 caller thread 的 ScopedTransferableThreadLocal对象引用都是使用 ScopedTransferableThreadLocal.weakCopyLocal2Value(), 为 WeakHashMap, GC会回收, 但不频繁, 适用于短时间引用
| 限定符和类型 | 字段和说明 |
|---|---|
static SttlInterceptor |
INSTANCE
可传递ScopedTransferableThreadLocal的拦截器, 只传递ScopedTransferableThreadLocal, 无关ThreadLocal
特性: worker thread 在执行任务时, 会传递 caller thread的 ScopedTransferableThreadLocal数据
目标: 主要是为了解决异步执行时, 线程状态(ScopedTransferableThreadLocal)的传递问题, 如 jkmvc 将当前 Db/HttpRequest 等对象都是记录到 ScopedTransferableThreadLocal对象中, 以方便访问, 但是一旦异步执行后就丢失了
实现: 在执行之前传递一下 ScopedTransferableThreadLocal对象, 在执行后就恢复一下 ScopedTransferableThreadLocal对象
优化: 所有 intercept()方法的 caller thread 的 ScopedTransferableThreadLocal对象引用都是使用
ScopedTransferableThreadLocal.weakCopyLocal2Value(), 为 WeakHashMap, GC会回收, 但不频繁, 适用于短时间引用 |
| 限定符和类型 | 方法和说明 |
|---|---|
<T> java.util.concurrent.CompletableFuture<T> |
intercept(java.util.concurrent.CompletableFuture<T> future,
NonExistentClass callerLocals)
拦截 CompletableFuture
|
java.lang.Runnable |
intercept(NonExistentClass callerLocals,
java.lang.Runnable command)
拦截 Runnable
|
<T,U> java.util.function.Function<T,U> |
intercept(NonExistentClass callerLocals,
java.util.function.Function<T,U> fn)
拦截 Function
|
<T,U,V> java.util.function.BiFunction<T,U,? extends V> |
intercept(NonExistentClass callerLocals,
java.util.function.BiFunction<T,U,? extends V> fn)
拦截 BiFunction
|
<T> java.util.function.Consumer<T> |
intercept(NonExistentClass callerLocals,
java.util.function.Consumer<T> consumer)
拦截 Consumer
|
<T,U> java.util.function.BiConsumer<T,U> |
intercept(NonExistentClass callerLocals,
java.util.function.BiConsumer<T,U> consumer)
拦截 BiConsumer
|
kotlin.jvm.functions.Function0<java.lang.Object> |
intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function0<? extends java.lang.Object> command)
拦截 command lambda
|
kotlin.jvm.functions.Function1<java.lang.Object,java.lang.Object> |
intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<java.lang.Object,? extends java.lang.Object> fn)
拦截 fn lambda
|
kotlin.jvm.functions.Function2<java.lang.Object,java.lang.Throwable,java.lang.Object> |
intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<java.lang.Object,? super java.lang.Throwable,? extends java.lang.Object> complete)
拦截 complete lambda
|
<T,U> java.util.function.BiConsumer<T,U> |
interceptToBiConsumer(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<? super T,? super U,? extends T> consumer)
拦截 BiConsumer
|
<T,U,V> java.util.function.BiFunction<T,U,? extends V> |
interceptToBiFunction(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<? super T,? super U,? extends V> fn)
拦截 BiFunction
|
<T> java.util.function.Consumer<T> |
interceptToConsumer(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<? super T,kotlin.Unit> consumer)
拦截 Consumer
|
<T,U> java.util.function.Function<T,U> |
interceptToFunction(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<? super T,? extends U> fn)
拦截 Function
|
java.lang.Runnable |
interceptToRunnable(NonExistentClass callerLocals,
kotlin.jvm.functions.Function0<kotlin.Unit> command)
拦截 Runnable
|
public static SttlInterceptor INSTANCE
可传递ScopedTransferableThreadLocal的拦截器, 只传递ScopedTransferableThreadLocal, 无关ThreadLocal
特性: worker thread 在执行任务时, 会传递 caller thread的 ScopedTransferableThreadLocal数据
目标: 主要是为了解决异步执行时, 线程状态(ScopedTransferableThreadLocal)的传递问题, 如 jkmvc 将当前 Db/HttpRequest 等对象都是记录到 ScopedTransferableThreadLocal对象中, 以方便访问, 但是一旦异步执行后就丢失了
实现: 在执行之前传递一下 ScopedTransferableThreadLocal对象, 在执行后就恢复一下 ScopedTransferableThreadLocal对象
优化: 所有 intercept()方法的 caller thread 的 ScopedTransferableThreadLocal对象引用都是使用 ScopedTransferableThreadLocal.weakCopyLocal2Value(), 为 WeakHashMap, GC会回收, 但不频繁, 适用于短时间引用
public <T> java.util.concurrent.CompletableFuture<T> intercept(java.util.concurrent.CompletableFuture<T> future,
NonExistentClass callerLocals)
拦截 CompletableFuture
不能使用 wrap() 来包装回调,因为回调完就恢复之前 ScopedTransferableThreadLocal, 改写的 ScopedTransferableThreadLocal 就丢了
CompletableFuture 只写不删(恢复), 但是 ScopedTransferableThreadLocal 自己有作用域管理, 会自己删除
记住这个方法是有副作用的, 会改写 调用 CompletableFuture.complete()/completeExceptionally() 的线程的 ScopedTransferableThreadLocal, 不过一般都是通用线程池, 自身不具有 ScopedTransferableThreadLocal
public java.lang.Runnable intercept(NonExistentClass callerLocals,
java.lang.Runnable command)
拦截 Runnable
public <T,U> java.util.function.Function<T,U> intercept(NonExistentClass callerLocals,
java.util.function.Function<T,U> fn)
拦截 Function
public <T,U,V> java.util.function.BiFunction<T,U,? extends V> intercept(NonExistentClass callerLocals,
java.util.function.BiFunction<T,U,? extends V> fn)
拦截 BiFunction
public <T> java.util.function.Consumer<T> intercept(NonExistentClass callerLocals,
java.util.function.Consumer<T> consumer)
拦截 Consumer
public <T,U> java.util.function.BiConsumer<T,U> intercept(NonExistentClass callerLocals,
java.util.function.BiConsumer<T,U> consumer)
拦截 BiConsumer
public kotlin.jvm.functions.Function0<java.lang.Object> intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function0<? extends java.lang.Object> command)
拦截 command lambda
public kotlin.jvm.functions.Function1<java.lang.Object,java.lang.Object> intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<java.lang.Object,? extends java.lang.Object> fn)
拦截 fn lambda
public kotlin.jvm.functions.Function2<java.lang.Object,java.lang.Throwable,java.lang.Object> intercept(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<java.lang.Object,? super java.lang.Throwable,? extends java.lang.Object> complete)
拦截 complete lambda
public java.lang.Runnable interceptToRunnable(NonExistentClass callerLocals,
kotlin.jvm.functions.Function0<kotlin.Unit> command)
拦截 Runnable
public <T,U> java.util.function.Function<T,U> interceptToFunction(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<? super T,? extends U> fn)
拦截 Function
public <T,U,V> java.util.function.BiFunction<T,U,? extends V> interceptToBiFunction(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<? super T,? super U,? extends V> fn)
拦截 BiFunction
public <T> java.util.function.Consumer<T> interceptToConsumer(NonExistentClass callerLocals,
kotlin.jvm.functions.Function1<? super T,kotlin.Unit> consumer)
拦截 Consumer
public <T,U> java.util.function.BiConsumer<T,U> interceptToBiConsumer(NonExistentClass callerLocals,
kotlin.jvm.functions.Function2<? super T,? super U,? extends T> consumer)
拦截 BiConsumer