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 ExceptionrunSupplierWithCaptured(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 interfaceThe transmittee is the extension point for otherThreadLocals which are registered byregisterTransmitteemethod. -
Method Summary
Modifier and TypeMethodDescriptionstatic 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> boolean registerTransmittee(TransmittableThreadLocal.Transmitter.Transmittee<C, B> transmittee) Register the transmittee(CRR), the extension point for otherThreadLocal.static ObjectReplay the capturedTransmittableThreadLocaland registeredThreadLocalvalues fromcapture(), and return the backupTransmittableThreadLocalvalues in the current thread before replay.static voidRestore 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> boolean unregisterTransmittee(TransmittableThreadLocal.Transmitter.Transmittee<C, B> transmittee) Unregister the transmittee(CRR), the extension point for otherThreadLocal.
-
Method Details
-
capture
Capture allTransmittableThreadLocaland registeredThreadLocalvalues in the current thread.- Returns:
- the captured
TransmittableThreadLocalvalues - Since:
- 2.3.0
-
replay
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:
-
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:
-
restore
Restore the backupTransmittableThreadLocaland registeredThreadLocalvalues fromreplay(Object)/clear().- Parameters:
backup- the backupTransmittableThreadLocalvalues 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- capturedTransmittableThreadLocalvalues 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- 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:
-
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 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:
-
registerThreadLocalWithShadowCopier
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
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:
-
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:
-
unregisterThreadLocal
Unregister theThreadLocalinstances to remove the Transmittable ability for theThreadLocalinstances.If the
ThreadLocalinstance isTransmittableThreadLocaljust ignores and returntrue.- Since:
- 2.11.0
- See Also:
-