📖 English Documentation | 📖 中文文档
- 🔧 Functions
- 🎨 Requirements
- 👥 User Guide
- 🔌 Java API Docs
- 🍪 Maven dependency
- 🗿 More documentation
- 📚 Related resources
- 👷 Contributors
👉 The missing std Java™ lib(simple & 0-dependency) for framework/middleware,
transmitting ThreadLocal value between threads even using thread pooling components.
Support Java
11/10/9/8/7/6.
Class InheritableThreadLocal
in JDK
can transmit value to child thread from parent thread.
But when use thread pool, thread is cached up and used repeatedly. Transmitting value from parent thread to child thread has no meaning. Application need transmit value from the time task is created to the time task is executed.
If you have problem or question, please submit Issue or play fork and pull request dance.
The Requirements listed below is also why I sort out TTL
in my work.
- Application container or high layer framework transmit information to low layer sdk.
- Transmit context to logging without application code aware.
// set in parent thread
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
// =====================================================
// read in child thread, value is "value-set-in-parent"
String value = parent.get();
This is the function of class InheritableThreadLocal
, should use class InheritableThreadLocal
instead.
But when use thread pool, thread is cached up and used repeatedly. Transmitting value from parent thread to child thread has no meaning. Application need transmit value from the time task is created to the point task is executed.
The solution is below usage.
Decorate input Runnable
and Callable
by TtlRunnable
and TtlCallable
.
Sample code:
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Runnable task = new Task("1");
// extra work, create decorated ttlRunnable object
Runnable ttlRunnable = TtlRunnable.get(task);
executorService.submit(ttlRunnable);
// =====================================================
// read in task, value is "value-set-in-parent"
String value = parent.get();
above code show how to dealing with Runnable
, Callable
is similar:
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Callable call = new Call("1");
// extra work, create decorated ttlCallable object
Callable ttlCallable = TtlCallable.get(call);
executorService.submit(ttlCallable);
// =====================================================
// read in call, value is "value-set-in-parent"
String value = parent.get();
Eliminating the work of Runnable
and Callable
Decoration every time it is submitted to thread pool. This work can completed in the thread pool.
Use util class
com.alibaba.ttl.threadpool.TtlExecutors
to decorate thread pool.
Util class com.alibaba.ttl.threadpool.TtlExecutors
has below methods:
getTtlExecutor
: decorate interfaceExecutor
getTtlExecutorService
: decorate interfaceExecutorService
getTtlScheduledExecutorService
: decorate interfaceScheduledExecutorService
Sample code:
ExecutorService executorService = ...
// extra work, create decorated executorService object
executorService = TtlExecutors.getTtlExecutorService(executorService);
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
Runnable task = new Task("1");
Callable call = new Call("2");
executorService.submit(task);
executorService.submit(call);
// =====================================================
// read in Task or Callable, value is "value-set-in-parent"
String value = parent.get();
In this usage, transmission is transparent(no decoration operation).
Sample code:
ExecutorService executorService = Executors.newFixedThreadPool(3);
Runnable task = new Task("1");
Callable call = new Call("2");
executorService.submit(task);
executorService.submit(call);
// =====================================================
// read in Task or Callable, value is "value-set-in-parent"
String value = parent.get();
See demo AgentDemo.kt
.
At present, TTL
agent has decorated below JDK
thread pool implementation:
java.util.concurrent.ThreadPoolExecutor
andjava.util.concurrent.ScheduledThreadPoolExecutor
decoration implemetation code is inTtlExecutorTransformlet.java
java.util.concurrent.ForkJoinTask
(corresponding thread pool isjava.util.concurrent.ForkJoinPool
)
decoration implemetation code is inTtlForkJoinTransformlet.java
java.util.TimerTask
(corresponding thread pool isjava.util.Timer
)
decoration implemetation code is inTtlTimerTaskTransformlet.java
NOTE: decoration forTimerTask
default is disable, enabled by agent argumentttl.agent.enable.timer.task
:-javaagent:path/to/transmittable-thread-local-2.x.x.jar:ttl.agent.enable.timer.task:true
.
more info aboutTTL
agent arguments, see the javadoc ofTtlAgent.java
.
Add start options on Java command:
-javaagent:path/to/transmittable-thread-local-2.x.x.jar
NOTE:
- Because TTL agent modified the
JDK
std lib classes, make code refer from std lib class to the TTL classes, so the TTL Agent jar must be added toboot classpath
. - Since
v2.6.0
, TTL agent jar will auto add self toboot classpath
. But you should NOT modify the downloaded TTL jar file name in the maven repo(eg:transmittable-thread-local-2.x.x.jar
).- if you modified the downloaded TTL jar file name(eg:
ttl-foo-name-changed.jar
), you must add TTL agent jar toboot classpath
manually by java option-Xbootclasspath/a:path/to/ttl-foo-name-changed.jar
.
- if you modified the downloaded TTL jar file name(eg:
The implementation of auto adding self agent jar to boot classpath
use the Boot-Class-Path
property of manifest file(META-INF/MANIFEST.MF
) in the TTL Java Agent Jar:
Boot-Class-Path
A list of paths to be searched by the bootstrap class loader. Paths represent directories or libraries (commonly referred to as JAR or zip libraries on many platforms). These paths are searched by the bootstrap class loader after the platform specific mechanisms of locating a class have failed. Paths are searched in the order listed.
More info:
Java Agent Specification
-JavaDoc
文档- JAR File Specification - JAR Manifest
- Working with Manifest Files - The Java™ TutorialsHide
Java command example:
java -javaagent:transmittable-thread-local-2.x.x.jar \
-cp classes \
com.alibaba.ttl.threadpool.agent.demo.AgentDemo
or
java -javaagent:path/to/ttl-foo-name-changed.jar \
-Xbootclasspath/a:path/to/ttl-foo-name-changed.jar \
-cp classes \
com.alibaba.ttl.threadpool.agent.demo.AgentDemo
Run the script scripts/run-agent-demo.sh
to start demo of "Use Java Agent to decorate thread pool implementation class".
The current version Java API documentation: http://alibaba.github.io/transmittable-thread-local/apidocs/
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.7.0</version>
</dependency>
Check available version at search.maven.org.
- Java Agent规范
- Java SE 6 新特性: Instrumentation 新功能
- Creation, dynamic loading and instrumentation with javaagents
- JavaAgent加载机制分析
- Jerry Lee <oldratlee at gmail dot com> @oldratlee
- Yang Fang <snoop.fy at gmail dot com> @driventokill
- wuwen <wuwen.55 at aliyun dot com> @wuwen5
- Xiaowei Shi <179969622 at qq dot com> @xwshiustc
- David Dai <351450944 at qq dot com> @LNAmp
- Your name here :-)