Package com.alibaba.ttl
Class TransmittableThreadLocal.Transmitter
- java.lang.Object
-
- com.alibaba.ttl.TransmittableThreadLocal.Transmitter
-
- Enclosing class:
- TransmittableThreadLocal<T>
public static class TransmittableThreadLocal.Transmitter extends Object
TransmittableThreadLocal.Transmittertransmit allTransmittableThreadLocalvalues of 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!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.- 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 allTransmittableThreadLocalvalues in current thread.static Objectclear()Clear allTransmittableThreadLocalvalues in current thread, and return the backupTransmittableThreadLocalvalues in current thread before clear.static Objectreplay(Object captured)Replay the capturedTransmittableThreadLocalvalues fromcapture(), and return the backupTransmittableThreadLocalvalues in current thread before replay.static voidrestore(Object backup)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.
-
-
-
Method Detail
-
capture
@Nonnull public static Object capture()
Capture allTransmittableThreadLocalvalues in current thread.- Returns:
- the captured
TransmittableThreadLocalvalues - Since:
- 2.3.0
-
replay
@Nonnull public static Object replay(@Nonnull Object captured)
Replay the capturedTransmittableThreadLocalvalues fromcapture(), and return the backupTransmittableThreadLocalvalues in 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 allTransmittableThreadLocalvalues in current thread, and return the backupTransmittableThreadLocalvalues in current thread before clear.- Returns:
- the backup
TransmittableThreadLocalvalues before clear - Since:
- 2.9.0
-
restore
public static void restore(@Nonnull Object backup)
- 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)
-
-