Skip to content

Latest commit

 

History

History
78 lines (52 loc) · 2.87 KB

developer-guide-en.md

File metadata and controls

78 lines (52 loc) · 2.87 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:

  • capture: capture all TTL values in current thread
  • replay: replay the captured TTL values in the current thread, and return the backup TTL values before replay
  • restore: restore TTL values before replay

Sample code:

// ===========================================================================
// Thread A
// ===========================================================================

TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.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 = parent.get();
    ...
} finally {
    // 3. restore TTL values before replay
    TransmittableThreadLocal.Transmitter.restore(backup);
}

For more actual implementation code of TTL transmittance, see TtlRunnable.java and TtlCallable.java

📚 Related material

Jdk core classes

Java Agent

Javassist

Shade Maven Plugin