Skip to content

Latest commit

 

History

History
79 lines (53 loc) · 3.13 KB

developer-guide-en.md

File metadata and controls

79 lines (53 loc) · 3.13 KB

🎓 Developer Guide



📌 Framework/Middleware integration to TTL transmittance

TransmittableThreadLocal.Transmitter to capture all TTL values of current thread and replay them in another thread.

There are following methods:

  1. capture: capture all TTL values in current thread
  2. replay: replay the captured TTL values in the current thread, and return the backup TTL values before replay
  3. restore: restore TTL 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);
}

📚 Related material

JDK core classes

Java Agent

Javassist

Maven Shade plugin