Class TransmittableThreadLocal.Transmitter
- java.lang.Object
-
- com.alibaba.ttl.TransmittableThreadLocal.Transmitter
-
- Enclosing class:
- TransmittableThreadLocal<T>
public static class TransmittableThreadLocal.Transmitter extends Object
TransmittableThreadLocal.Transmittertransmit allTransmittableThreadLocaland registeredThreadLocal(registered byregisterThreadLocal(java.lang.ThreadLocal<T>, com.alibaba.ttl.TtlCopier<T>)) values of the current thread to other thread by static methodscapture()=>replay(Object)=>restore(Object)(akaCRRoperation).TransmittableThreadLocal.Transmitteris internal manipulation api for framework/middleware integration; In general, you will never use it in the biz/application code!Framework/Middleware integration to TTL transmittance
Below is the example code://///////////////////////////////////////////////////////////////////////// // in thread A, capture all TransmittableThreadLocal values of thread A /////////////////////////////////////////////////////////////////////////// Object captured = Transmitter.capture(); // (1) /////////////////////////////////////////////////////////////////////////// // in thread B /////////////////////////////////////////////////////////////////////////// // replay all TransmittableThreadLocal values from thread A Object backup = Transmitter.replay(captured); // (2) try { // your biz logic, run with the TransmittableThreadLocal values of thread B System.out.println("Hello"); // ... return "World"; } finally { // restore the TransmittableThreadLocal of thread B when replay Transmitter.restore(backup); (3) }see the implementation code of
TtlRunnableandTtlCallablefor more actual code sample.Of course,
replay(Object)andrestore(Object)operation can be simplified by util methodsrunCallableWithCaptured(Object, Callable)orrunSupplierWithCaptured(Object, Supplier)and the adorableJava 8 lambda syntax.Below is the example code:
/////////////////////////////////////////////////////////////////////////// // in thread A, capture all TransmittableThreadLocal values of thread A /////////////////////////////////////////////////////////////////////////// Object captured = Transmitter.capture(); // (1) /////////////////////////////////////////////////////////////////////////// // in thread B /////////////////////////////////////////////////////////////////////////// String result = runSupplierWithCaptured(captured, () -> { // your biz logic, run with the TransmittableThreadLocal values of thread A System.out.println("Hello"); ... return "World"; }); // (2) + (3)The reason of providing 2 util methods is the different
throws Exceptiontype so as to satisfy your biz logic(lambda):runCallableWithCaptured(Object, Callable):throws ExceptionrunSupplierWithCaptured(Object, Supplier): Nothrows
If you need the different
throws Exceptiontype, you can define your own util method(function interface(lambda)) with your ownthrows Exceptiontype.ThreadLocal Integration
If you can not rewrite the existed code which useThreadLocaltoTransmittableThreadLocal, register theThreadLocalinstances via the methodsregisterThreadLocal(ThreadLocal, TtlCopier)/registerThreadLocalWithShadowCopier(ThreadLocal)to enhance the Transmittable ability for the existedThreadLocalinstances.Below is the example code:
Caution:// the value of this ThreadLocal instance will be transmitted after registered Transmitter.registerThreadLocal(aThreadLocal, copyLambda); // Then the value of this ThreadLocal instance will not be transmitted after unregistered Transmitter.unregisterThreadLocal(aThreadLocal);
If the registeredThreadLocalinstance is notInheritableThreadLocal, the instance can NOTinheritvalue from parent thread(aka. the inheritable ability)!- Since:
- 2.3.0
- Author:
- Yang Fang (snoop dot fy at gmail dot com), Jerry Lee (oldratlee at gmail dot com)
- See Also:
TtlRunnable,TtlCallable
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Objectcapture()Capture allTransmittableThreadLocaland registeredThreadLocalvalues in the current thread.static Objectclear()Clear allTransmittableThreadLocaland registeredThreadLocalvalues in the current thread, and return the backupTransmittableThreadLocalvalues in the current thread before clear.static <T> booleanregisterThreadLocal(ThreadLocal<T> threadLocal, TtlCopier<T> copier)Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.static <T> booleanregisterThreadLocal(ThreadLocal<T> threadLocal, TtlCopier<T> copier, boolean force)Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.static <T> booleanregisterThreadLocalWithShadowCopier(ThreadLocal<T> threadLocal)Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.static <T> booleanregisterThreadLocalWithShadowCopier(ThreadLocal<T> threadLocal, boolean force)Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.static Objectreplay(Object captured)Replay the capturedTransmittableThreadLocaland registeredThreadLocalvalues fromcapture(), and return the backupTransmittableThreadLocalvalues in the current thread before replay.static voidrestore(Object backup)Restore the backupTransmittableThreadLocaland registeredThreadLocalvalues fromreplay(Object)/clear().static <R> RrunCallableWithCaptured(Object captured, Callable<R> bizLogic)Util method for simplifyingreplay(Object)andrestore(Object)operation.static <R> RrunCallableWithClear(Callable<R> bizLogic)Util method for simplifyingclear()andrestore(Object)operation.static <R> RrunSupplierWithCaptured(Object captured, Supplier<R> bizLogic)Util method for simplifyingreplay(Object)andrestore(Object)operation.static <R> RrunSupplierWithClear(Supplier<R> bizLogic)Util method for simplifyingclear()andrestore(Object)operation.static <T> booleanunregisterThreadLocal(ThreadLocal<T> threadLocal)Unregister theThreadLocalinstances to remove the Transmittable ability for theThreadLocalinstances.
-
-
-
Method Detail
-
capture
@NonNull public static Object capture()
Capture allTransmittableThreadLocaland registeredThreadLocalvalues in the current thread.- Returns:
- the captured
TransmittableThreadLocalvalues - Since:
- 2.3.0
-
replay
@NonNull public static Object replay(@NonNull Object captured)
Replay the capturedTransmittableThreadLocaland registeredThreadLocalvalues fromcapture(), and return the backupTransmittableThreadLocalvalues in the current thread before replay.- Parameters:
captured- capturedTransmittableThreadLocalvalues from other thread fromcapture()- Returns:
- the backup
TransmittableThreadLocalvalues before replay - Since:
- 2.3.0
- See Also:
capture()
-
clear
@NonNull public static Object clear()
Clear allTransmittableThreadLocaland registeredThreadLocalvalues in the current thread, and return the backupTransmittableThreadLocalvalues in the current thread before clear.- Returns:
- the backup
TransmittableThreadLocalvalues before clear - Since:
- 2.9.0
-
restore
public static void restore(@NonNull Object backup)
Restore the backupTransmittableThreadLocaland registeredThreadLocalvalues fromreplay(Object)/clear().- Parameters:
backup- the backupTransmittableThreadLocalvalues fromreplay(Object)/clear()- Since:
- 2.3.0
- See Also:
replay(Object),clear()
-
runSupplierWithCaptured
public static <R> R runSupplierWithCaptured(@NonNull Object captured, @NonNull Supplier<R> bizLogic)
Util method for simplifyingreplay(Object)andrestore(Object)operation.- Type Parameters:
R- the return type of biz logic- Parameters:
captured- capturedTransmittableThreadLocalvalues from other thread fromcapture()bizLogic- biz logic- Returns:
- the return value of biz logic
- Since:
- 2.3.1
- See Also:
capture(),replay(Object),restore(Object)
-
runSupplierWithClear
public static <R> R runSupplierWithClear(@NonNull Supplier<R> bizLogic)
Util method for simplifyingclear()andrestore(Object)operation.- Type Parameters:
R- the return type of biz logic- Parameters:
bizLogic- biz logic- Returns:
- the return value of biz logic
- Since:
- 2.9.0
- See Also:
clear(),restore(Object)
-
runCallableWithCaptured
public static <R> R runCallableWithCaptured(@NonNull Object captured, @NonNull Callable<R> bizLogic) throws Exception
Util method for simplifyingreplay(Object)andrestore(Object)operation.- Type Parameters:
R- the return type of biz logic- Parameters:
captured- capturedTransmittableThreadLocalvalues from other thread fromcapture()bizLogic- biz logic- Returns:
- the return value of biz logic
- Throws:
Exception- exception threw by biz logic- Since:
- 2.3.1
- See Also:
capture(),replay(Object),restore(Object)
-
runCallableWithClear
public static <R> R runCallableWithClear(@NonNull Callable<R> bizLogic) throws Exception
Util method for simplifyingclear()andrestore(Object)operation.- Type Parameters:
R- the return type of biz logic- Parameters:
bizLogic- biz logic- Returns:
- the return value of biz logic
- Throws:
Exception- exception threw by biz logic- Since:
- 2.9.0
- See Also:
clear(),restore(Object)
-
registerThreadLocal
public static <T> boolean registerThreadLocal(@NonNull ThreadLocal<T> threadLocal, @NonNull TtlCopier<T> copier)
Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.If the registered
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue. since aTransmittableThreadLocalinstance itself has theTransmittableability, it is unnecessary to register aTransmittableThreadLocalinstance.- Parameters:
threadLocal- theThreadLocalinstance that to enhance the Transmittable abilitycopier- theTtlCopier- Returns:
trueif register theThreadLocalinstance and setcopier, otherwisefalse- Since:
- 2.11.0
- See Also:
registerThreadLocal(ThreadLocal, TtlCopier, boolean)
-
registerThreadLocalWithShadowCopier
public static <T> boolean registerThreadLocalWithShadowCopier(@NonNull ThreadLocal<T> threadLocal)
Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.Use the shadow copier(transmit the reference directly), and should use
registerThreadLocal(ThreadLocal, TtlCopier)to pass aTtlCopierexplicitly if a different behavior is desired.If the registered
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue. since aTransmittableThreadLocalinstance itself has theTransmittableability, it is unnecessary to register aTransmittableThreadLocalinstance.- Parameters:
threadLocal- theThreadLocalinstance that to enhance the Transmittable ability- Returns:
trueif register theThreadLocalinstance and setcopier, otherwisefalse- Since:
- 2.11.0
- See Also:
registerThreadLocal(ThreadLocal, TtlCopier),registerThreadLocal(ThreadLocal, TtlCopier, boolean)
-
registerThreadLocal
public static <T> boolean registerThreadLocal(@NonNull ThreadLocal<T> threadLocal, @NonNull TtlCopier<T> copier, boolean force)
Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.If the registered
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue. since aTransmittableThreadLocalinstance itself has theTransmittableability, it is unnecessary to register aTransmittableThreadLocalinstance.- Parameters:
threadLocal- theThreadLocalinstance that to enhance the Transmittable abilitycopier- theTtlCopierforce- iftrue, updatecopiertoThreadLocalinstance when aThreadLocalinstance is already registered; otherwise, ignore.- Returns:
trueif register theThreadLocalinstance and setcopier, otherwisefalse- Since:
- 2.11.0
- See Also:
registerThreadLocal(ThreadLocal, TtlCopier)
-
registerThreadLocalWithShadowCopier
public static <T> boolean registerThreadLocalWithShadowCopier(@NonNull ThreadLocal<T> threadLocal, boolean force)
Register theThreadLocal(including subclassInheritableThreadLocal) instances to enhance the Transmittable ability for the existedThreadLocalinstances.Use the shadow copier(transmit the reference directly), and should use
registerThreadLocal(ThreadLocal, TtlCopier, boolean)to pass aTtlCopierexplicitly if a different behavior is desired.If the registered
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue. since aTransmittableThreadLocalinstance itself has theTransmittableability, it is unnecessary to register aTransmittableThreadLocalinstance.- Parameters:
threadLocal- theThreadLocalinstance that to enhance the Transmittable abilityforce- iftrue, updatecopiertoThreadLocalinstance when aThreadLocalinstance is already registered; otherwise, ignore.- Returns:
trueif register theThreadLocalinstance and setcopier, otherwisefalse- Since:
- 2.11.0
- See Also:
registerThreadLocal(ThreadLocal, TtlCopier),registerThreadLocal(ThreadLocal, TtlCopier, boolean)
-
unregisterThreadLocal
public static <T> boolean unregisterThreadLocal(@NonNull ThreadLocal<T> threadLocal)
Unregister theThreadLocalinstances to remove the Transmittable ability for theThreadLocalinstances.If the
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue.- Since:
- 2.11.0
- See Also:
registerThreadLocal(ThreadLocal, TtlCopier),registerThreadLocalWithShadowCopier(ThreadLocal)
-
-