TransmittableThreadLocal.Transmitter to capture all TTL values of current thread and replay them in another thread.
There are following methods:
capture: capture allTTLvalues in current threadreplay: replay the capturedTTLvalues in the current thread, and return the backupTTLvalues before replayrestore: restoreTTLvalues before replay
Sample code:
// ===========================================================================
// Thread A
// ===========================================================================
TransmittableThreadLocal<String> context = new TransmittableThreadLocal<String>();
context.set("value-set-in-parent");
// 1. capture all TTL values in current thread
final Object captured = TransmittableThreadLocal.Transmitter.capture();
// ===========================================================================
// Thread B
// ===========================================================================
// 2. replay the captured TTL values in current thread, and return the backup TTL values before replay
final Object backup = TransmittableThreadLocal.Transmitter.replay(captured);
try {
// Your biz code, you can get the TTL value from here
String value = context.get();
...
} finally {
// 3. restore TTL values before replay
TransmittableThreadLocal.Transmitter.restore(backup);
}- For more info about
TransmittableThreadLocal.Transmitter, see its Javadoc. - For more actual implementation code of
TTLtransmittance, seeTtlRunnable.javaandTtlCallable.java.