Class TransmittableThreadLocal.Transmitter
- java.lang.Object
-
- com.alibaba.ttl.TransmittableThreadLocal.Transmitter
-
- Enclosing class:
- TransmittableThreadLocal<T>
public static class TransmittableThreadLocal.Transmitter extends Object
Transmittertransmit allTransmittableThreadLocaland registeredThreadLocalvalues of the current thread to other thread.Transmittance is completed by static methods
capture()=>replay(Object)=>restore(Object)(akaCRRoperations);ThreadLocalinstances are registered by methodTransmitter#registerThreadLocal.Transmitteris internal manipulation api for framework/middleware integration; In general, you will never use it in the biz/application codes!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 samples.Of course,
replay(Object)andrestore(Object)operations 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 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:
// 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);The fields stored the
ThreadLocalinstances are generallyprivate static, so theThreadLocalinstances need be got by reflection, for example:Field field = TheClassStoredThreadLocal.class.getDeclaredField(staticFieldName); field.setAccessible(true);
Caution:@SuppressWarnings("unchecked")ThreadLocal<T>threadLocal =(ThreadLocal<T>)field.get(null);
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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceTransmittableThreadLocal.Transmitter.Transmittee<C,B>The transmittee is the extension point for otherThreadLocals which are registered byregisterTransmitteemethod.
-
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 <C,B>
booleanregisterTransmittee(TransmittableThreadLocal.Transmitter.Transmittee<C,B> transmittee)Register the transmittee(CRR), the extension point for otherThreadLocal.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)operations.static <R> RrunCallableWithClear(Callable<R> bizLogic)Util method for simplifyingclear()andrestore(Object)operations.static <R> RrunSupplierWithCaptured(Object captured, Supplier<R> bizLogic)Util method for simplifyingreplay(Object)andrestore(Object)operations.static <R> RrunSupplierWithClear(Supplier<R> bizLogic)Util method for simplifyingclear()andrestore(Object)operations.static <T> booleanunregisterThreadLocal(ThreadLocal<T> threadLocal)Unregister theThreadLocalinstances to remove the Transmittable ability for theThreadLocalinstances.static <C,B>
booleanunregisterTransmittee(TransmittableThreadLocal.Transmitter.Transmittee<C,B> transmittee)Unregister the transmittee(CRR), the extension point for otherThreadLocal.
-
-
-
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.Semantically, the code
`Object backup = clear();`is same as`Object backup = replay(EMPTY_CAPTURE);`.The reason for providing this method is:
- lead to more readable code
- need not provide the constant
EMPTY_CAPTURE.
- Returns:
- the backup
TransmittableThreadLocalvalues before clear - Since:
- 2.9.0
- See Also:
replay(Object)
-
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()
-
registerTransmittee
public static <C,B> boolean registerTransmittee(@NonNull TransmittableThreadLocal.Transmitter.Transmittee<C,B> transmittee)
Register the transmittee(CRR), the extension point for otherThreadLocal.- Type Parameters:
C- the transmittee capture data typeB- the transmittee backup data type- Returns:
- true if the input transmittee is not registered
- Since:
- 2.14.0
- See Also:
unregisterTransmittee(Transmittee)
-
unregisterTransmittee
public static <C,B> boolean unregisterTransmittee(@NonNull TransmittableThreadLocal.Transmitter.Transmittee<C,B> transmittee)
Unregister the transmittee(CRR), the extension point for otherThreadLocal.- Type Parameters:
C- the transmittee capture data typeB- the transmittee backup data type- Returns:
- true if the input transmittee is registered
- Since:
- 2.14.0
- See Also:
registerTransmittee(Transmittee)
-
runSupplierWithCaptured
public static <R> R runSupplierWithCaptured(@NonNull Object captured, @NonNull Supplier<R> bizLogic)
Util method for simplifyingreplay(Object)andrestore(Object)operations.- 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)operations.- 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)operations.- 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- the 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)operations.- Type Parameters:
R- the return type of biz logic- Parameters:
bizLogic- biz logic- Returns:
- the return value of biz logic
- Throws:
Exception- the 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.Caution:
If the registeredThreadLocalinstance is notInheritableThreadLocal, the instance can NOTinheritvalue from parent thread(aka. the inheritable ability)!- 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 method
registerThreadLocal(ThreadLocal, TtlCopier)to pass a customizedTtlCopierexplicitly 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.Caution:
If the registeredThreadLocalinstance is notInheritableThreadLocal, the instance can NOTinheritvalue from parent thread(aka. the inheritable ability)!- 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.Caution:
If the registeredThreadLocalinstance is notInheritableThreadLocal, the instance can NOTinheritvalue from parent thread(aka. the inheritable ability)!- 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 method
registerThreadLocal(ThreadLocal, TtlCopier, boolean)to pass a customizedTtlCopierexplicitly 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.Caution:
If the registeredThreadLocalinstance is notInheritableThreadLocal, the instance can NOTinheritvalue from parent thread(aka. the inheritable ability)!- 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)
-
-