TransmittableThreadLocal.Transmitter
to capture all TTL
values of current thread and replay them in another thread.
There are following methods:
capture
: capture allTTL
values in current threadreplay
: replay the capturedTTL
values in the current thread, and return the backupTTL
values before replayrestore
: restoreTTL
values 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
TTL
transmittance, seeTtlRunnable.java
andTtlCallable.java
.