public static class TransmittableThreadLocal.Transmitter extends Object
TransmittableThreadLocal.Transmitter transmit all TransmittableThreadLocal of current thread to
any other thread by static method capture() => replay(Object) => restore(Object) (aka CRR operation).
TransmittableThreadLocal.Transmitter is 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 1
///////////////////////////////////////////////////////////////////////////
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 TtlRunnable and TtlCallable for more actual code sample.
replay(Object) and restore(Object) operation can be simplified
by util methods runCallableWithCaptured(Object, Callable) or runCallableWithCaptured(Object, Callable)
and the adorable Java 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 Exception type from biz logic(@{code lambda}):
runCallableWithCaptured(Object, Callable): No throwsrunSupplierWithCaptured(Object, Supplier): throws {@link Exception}
If you has the different throws Exception,
you can define your own util method with your own throws Exception type function interface(@{code lambda}).
TtlRunnable,
TtlCallable| Constructor and Description |
|---|
Transmitter() |
| Modifier and Type | Method and Description |
|---|---|
static Object |
capture()
Capture all
TransmittableThreadLocal values in current thread. |
static Object |
replay(Object captured)
Replay the captured
TransmittableThreadLocal values from capture(),
and return the backup TransmittableThreadLocal values in current thread before replay. |
static void |
restore(Object backup)
Restore the backup
TransmittableThreadLocal values from replay(Object). |
static <R> R |
runCallableWithCaptured(Object captured,
Callable<R> bizLogic)
Util method for simplifying
replay(Object) and restore(Object) operation. |
static <R> R |
runSupplierWithCaptured(Object captured,
Supplier<R> bizLogic)
Util method for simplifying
replay(Object) and restore(Object) operation. |
public static Object capture()
TransmittableThreadLocal values in current thread.TransmittableThreadLocal valuespublic static Object replay(Object captured)
TransmittableThreadLocal values from capture(),
and return the backup TransmittableThreadLocal values in current thread before replay.captured - captured TransmittableThreadLocal values from other thread from capture()TransmittableThreadLocal values before replaycapture()public static void restore(Object backup)
TransmittableThreadLocal values from replay(Object).backup - the backup TransmittableThreadLocal values from replay(Object)public static <R> R runSupplierWithCaptured(Object captured, Supplier<R> bizLogic)
replay(Object) and restore(Object) operation.R - the return type of biz logiccaptured - captured TransmittableThreadLocal values from other thread from capture()bizLogic - biz logiccapture(),
replay(Object),
restore(Object)public static <R> R runCallableWithCaptured(Object captured, Callable<R> bizLogic) throws Exception
replay(Object) and restore(Object) operation.R - the return type of biz logiccaptured - captured TransmittableThreadLocal values from other thread from capture()bizLogic - biz logicException - exception threw by biz logiccapture(),
replay(Object),
restore(Object)Copyright © 2013–2018 Alibaba. All rights reserved.