Class TransmittableThreadLocal.Transmitter
- Enclosing class:
- TransmittableThreadLocal<T>
Transmitter
transmit all TransmittableThreadLocal
and registered ThreadLocal
values of the current thread to other thread.
Transmittance is completed by static methods capture()
=>
replay(Object)
=> restore(Object)
(aka CRR
operations);
ThreadLocal
instances are registered by method Transmitter#registerThreadLocal
.
Transmitter
is 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 TtlRunnable
and TtlCallable
for more actual code samples.
Of course, replay(Object)
and restore(Object)
operations can be simplified by util methods
runCallableWithCaptured(Object, Callable)
or runSupplierWithCaptured(Object, Supplier)
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
to satisfy your biz logic(lambda
):
runCallableWithCaptured(Object, Callable)
:throws Exception
runSupplierWithCaptured(Object, Supplier)
: Nothrows
If you need the different throws Exception
type,
you can define your own util method(function interface(lambda
))
with your own throws Exception
type.
ThreadLocal Integration
If you can not rewrite the existed code which useThreadLocal
to TransmittableThreadLocal
,
register the ThreadLocal
instances via the methods
registerThreadLocal(ThreadLocal, TtlCopier)
/registerThreadLocalWithShadowCopier(ThreadLocal)
to enhance the Transmittable ability for the existed ThreadLocal
instances.
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 ThreadLocal
instances are generally private static
,
so the ThreadLocal
instances 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 registered
ThreadLocal
instance is not InheritableThreadLocal
,
the instance can NOT inherit
value 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:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
The transmittee is the extension point for otherThreadLocal
s which are registered byregisterTransmittee
method. -
Method Summary
Modifier and TypeMethodDescriptionstatic Object
capture()
Capture allTransmittableThreadLocal
and registeredThreadLocal
values in the current thread.static Object
clear()
Clear allTransmittableThreadLocal
and registeredThreadLocal
values in the current thread, and return the backupTransmittableThreadLocal
values in the current thread before clear.static <T> boolean
registerThreadLocal
(ThreadLocal<T> threadLocal, TtlCopier<T> copier) Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.static <T> boolean
registerThreadLocal
(ThreadLocal<T> threadLocal, TtlCopier<T> copier, boolean force) Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.static <T> boolean
registerThreadLocalWithShadowCopier
(ThreadLocal<T> threadLocal) Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.static <T> boolean
registerThreadLocalWithShadowCopier
(ThreadLocal<T> threadLocal, boolean force) Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.static <C,
B> boolean registerTransmittee
(TransmittableThreadLocal.Transmitter.Transmittee<C, B> transmittee) Register the transmittee(CRR
), the extension point for otherThreadLocal
.static Object
Replay the capturedTransmittableThreadLocal
and registeredThreadLocal
values fromcapture()
, and return the backupTransmittableThreadLocal
values in the current thread before replay.static void
Restore the backupTransmittableThreadLocal
and registeredThreadLocal
values fromreplay(Object)
/clear()
.static <R> R
runCallableWithCaptured
(Object captured, Callable<R> bizLogic) Util method for simplifyingreplay(Object)
andrestore(Object)
operations.static <R> R
runCallableWithClear
(Callable<R> bizLogic) Util method for simplifyingclear()
andrestore(Object)
operations.static <R> R
runSupplierWithCaptured
(Object captured, Supplier<R> bizLogic) Util method for simplifyingreplay(Object)
andrestore(Object)
operations.static <R> R
runSupplierWithClear
(Supplier<R> bizLogic) Util method for simplifyingclear()
andrestore(Object)
operations.static <T> boolean
unregisterThreadLocal
(ThreadLocal<T> threadLocal) Unregister theThreadLocal
instances to remove the Transmittable ability for theThreadLocal
instances.static <C,
B> boolean unregisterTransmittee
(TransmittableThreadLocal.Transmitter.Transmittee<C, B> transmittee) Unregister the transmittee(CRR
), the extension point for otherThreadLocal
.
-
Method Details
-
capture
Capture allTransmittableThreadLocal
and registeredThreadLocal
values in the current thread.- Returns:
- the captured
TransmittableThreadLocal
values - Since:
- 2.3.0
-
replay
Replay the capturedTransmittableThreadLocal
and registeredThreadLocal
values fromcapture()
, and return the backupTransmittableThreadLocal
values in the current thread before replay.- Parameters:
captured
- capturedTransmittableThreadLocal
values from other thread fromcapture()
- Returns:
- the backup
TransmittableThreadLocal
values before replay - Since:
- 2.3.0
- See Also:
-
clear
Clear allTransmittableThreadLocal
and registeredThreadLocal
values in the current thread, and return the backupTransmittableThreadLocal
values 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
TransmittableThreadLocal
values before clear - Since:
- 2.9.0
- See Also:
-
restore
Restore the backupTransmittableThreadLocal
and registeredThreadLocal
values fromreplay(Object)
/clear()
.- Parameters:
backup
- the backupTransmittableThreadLocal
values fromreplay(Object)
/clear()
- Since:
- 2.3.0
- See Also:
-
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
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:
-
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
- capturedTransmittableThreadLocal
values from other thread fromcapture()
bizLogic
- biz logic- Returns:
- the return value of biz logic
- Since:
- 2.3.1
- See Also:
-
runSupplierWithClear
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:
-
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
- capturedTransmittableThreadLocal
values 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:
-
runCallableWithClear
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:
-
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 existedThreadLocal
instances.If the registered
ThreadLocal
instance isTransmittableThreadLocal
just ignores and returntrue
. since aTransmittableThreadLocal
instance itself has theTransmittable
ability, it is unnecessary to register aTransmittableThreadLocal
instance.Caution:
If the registeredThreadLocal
instance is notInheritableThreadLocal
, the instance can NOTinherit
value from parent thread(aka. the inheritable ability)!- Parameters:
threadLocal
- theThreadLocal
instance that to enhance the Transmittable abilitycopier
- theTtlCopier
- Returns:
true
if register theThreadLocal
instance and setcopier
, otherwisefalse
- Since:
- 2.11.0
- See Also:
-
registerThreadLocalWithShadowCopier
Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.Use the shadow copier(transmit the reference directly), and should use method
registerThreadLocal(ThreadLocal, TtlCopier)
to pass a customizedTtlCopier
explicitly if a different behavior is desired.If the registered
ThreadLocal
instance isTransmittableThreadLocal
just ignores and returntrue
. since aTransmittableThreadLocal
instance itself has theTransmittable
ability, it is unnecessary to register aTransmittableThreadLocal
instance.Caution:
If the registeredThreadLocal
instance is notInheritableThreadLocal
, the instance can NOTinherit
value from parent thread(aka. the inheritable ability)!- Parameters:
threadLocal
- theThreadLocal
instance that to enhance the Transmittable ability- Returns:
true
if register theThreadLocal
instance and setcopier
, otherwisefalse
- Since:
- 2.11.0
- See Also:
-
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 existedThreadLocal
instances.If the registered
ThreadLocal
instance isTransmittableThreadLocal
just ignores and returntrue
. since aTransmittableThreadLocal
instance itself has theTransmittable
ability, it is unnecessary to register aTransmittableThreadLocal
instance.Caution:
If the registeredThreadLocal
instance is notInheritableThreadLocal
, the instance can NOTinherit
value from parent thread(aka. the inheritable ability)!- Parameters:
threadLocal
- theThreadLocal
instance that to enhance the Transmittable abilitycopier
- theTtlCopier
force
- iftrue
, updatecopier
toThreadLocal
instance when aThreadLocal
instance is already registered; otherwise, ignore.- Returns:
true
if register theThreadLocal
instance and setcopier
, otherwisefalse
- Since:
- 2.11.0
- See Also:
-
registerThreadLocalWithShadowCopier
public static <T> boolean registerThreadLocalWithShadowCopier(@NonNull ThreadLocal<T> threadLocal, boolean force) Register theThreadLocal
(including subclassInheritableThreadLocal
) instances to enhance the Transmittable ability for the existedThreadLocal
instances.Use the shadow copier(transmit the reference directly), and should use method
registerThreadLocal(ThreadLocal, TtlCopier, boolean)
to pass a customizedTtlCopier
explicitly if a different behavior is desired.If the registered
ThreadLocal
instance isTransmittableThreadLocal
just ignores and returntrue
. since aTransmittableThreadLocal
instance itself has theTransmittable
ability, it is unnecessary to register aTransmittableThreadLocal
instance.Caution:
If the registeredThreadLocal
instance is notInheritableThreadLocal
, the instance can NOTinherit
value from parent thread(aka. the inheritable ability)!- Parameters:
threadLocal
- theThreadLocal
instance that to enhance the Transmittable abilityforce
- iftrue
, updatecopier
toThreadLocal
instance when aThreadLocal
instance is already registered; otherwise, ignore.- Returns:
true
if register theThreadLocal
instance and setcopier
, otherwisefalse
- Since:
- 2.11.0
- See Also:
-
unregisterThreadLocal
Unregister theThreadLocal
instances to remove the Transmittable ability for theThreadLocal
instances.If the
ThreadLocal
instance isTransmittableThreadLocal
just ignores and returntrue
.- Since:
- 2.11.0
- See Also:
-