README-EN.md 10.9 KB
Newer Older
oldratlee's avatar
oldratlee 已提交
1
# 📌 Transmittable ThreadLocal(TTL) 📌
oldratlee's avatar
oldratlee 已提交
2

oldratlee's avatar
oldratlee 已提交
3
[![Build Status](https://travis-ci.org/alibaba/transmittable-thread-local.svg?branch=master)](https://travis-ci.org/alibaba/transmittable-thread-local)
4
[![Windows Build Status](https://img.shields.io/appveyor/ci/oldratlee/transmittable-thread-local/master.svg?label=windows%20build)](https://ci.appveyor.com/project/oldratlee/transmittable-thread-local)
oldratlee's avatar
oldratlee 已提交
5
[![Coverage Status](https://img.shields.io/codecov/c/github/alibaba/transmittable-thread-local/master.svg)](https://codecov.io/gh/alibaba/transmittable-thread-local/branch/master)
oldratlee's avatar
oldratlee 已提交
6
[![Maintainability](https://api.codeclimate.com/v1/badges/de6af6136e538cf1557c/maintainability)](https://codeclimate.com/github/alibaba/transmittable-thread-local/maintainability)
J
Jerry Lee 已提交
7
[![Maven Central](https://img.shields.io/maven-central/v/com.alibaba/transmittable-thread-local.svg)](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.alibaba%22%20AND%20a%3A%22transmittable-thread-local%22)
oldratlee's avatar
oldratlee 已提交
8
[![GitHub release](https://img.shields.io/github/release/alibaba/transmittable-thread-local.svg)](https://github.com/alibaba/transmittable-thread-local/releases)  
T
The Gitter Badger 已提交
9
[![Join the chat at https://gitter.im/alibaba/transmittable-thread-local](https://badges.gitter.im/alibaba/transmittable-thread-local.svg)](https://gitter.im/alibaba/transmittable-thread-local?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
oldratlee's avatar
oldratlee 已提交
10
[![GitHub issues](https://img.shields.io/github/issues/alibaba/transmittable-thread-local.svg)](https://github.com/alibaba/transmittable-thread-local/issues)
oldratlee's avatar
oldratlee 已提交
11 12
[![Percentage of issues still open](http://isitmaintained.com/badge/open/alibaba/transmittable-thread-local.svg)](http://isitmaintained.com/project/alibaba/transmittable-thread-local "Percentage of issues still open")
[![Average time to resolve an issue](http://isitmaintained.com/badge/resolution/alibaba/transmittable-thread-local.svg)](http://isitmaintained.com/project/alibaba/transmittable-thread-local "Average time to resolve an issue")
oldratlee's avatar
oldratlee 已提交
13
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
oldratlee's avatar
oldratlee 已提交
14

oldratlee's avatar
oldratlee 已提交
15
📖 English Documentation | [📖 中文文档](README.md)
oldratlee's avatar
oldratlee 已提交
16

oldratlee's avatar
oldratlee 已提交
17 18 19 20 21 22
----------------------------------------

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->


oldratlee's avatar
oldratlee 已提交
23 24 25
- [🔧 Functions](#-functions)
- [🎨 Requirements](#-requirements)
- [👥 User Guide](#-user-guide)
oldratlee's avatar
oldratlee 已提交
26 27 28 29
    - [1. simple usage](#1-simple-usage)
    - [2. Transmit value even using thread pool](#2-transmit-value-even-using-thread-pool)
        - [2.1 Decorate `Runnable` and `Callable`](#21-decorate-runnable-and-callable)
        - [2.2 Decorate thread pool](#22-decorate-thread-pool)
30
        - [2.3 Use Java Agent to decorate thread pool implementation class](#23-use-java-agent-to-decorate-thread-pool-implementation-class)
oldratlee's avatar
oldratlee 已提交
31 32 33 34 35
- [🔌 Java API Docs](#-java-api-docs)
- [🍪 Maven dependency](#-maven-dependency)
- [🗿 More documentation](#-more-documentation)
- [📚 Related resources](#-related-resources)
    - [JDK core classes](#jdk-core-classes)
oldratlee's avatar
oldratlee 已提交
36 37
    - [Java Agent](#java-agent)
    - [Javassist](#javassist)
oldratlee's avatar
oldratlee 已提交
38
- [👷 Contributors](#-contributors)
oldratlee's avatar
oldratlee 已提交
39 40 41 42 43

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

----------------------------------------

oldratlee's avatar
oldratlee 已提交
44
# 🔧 Functions
oldratlee's avatar
oldratlee 已提交
45

oldratlee's avatar
oldratlee 已提交
46
👉 The missing std Java™ lib(simple &amp; 0-dependency) for framework/middleware,
J
Jerry Lee 已提交
47
transmitting ThreadLocal value between threads even using thread pool like components.
oldratlee's avatar
oldratlee 已提交
48
Support `Java` 11/10/9/8/7/6.
oldratlee's avatar
oldratlee 已提交
49

50
Class [`InheritableThreadLocal`](https://docs.oracle.com/javase/8/docs/api/java/lang/InheritableThreadLocal.html) in `JDK`
51
can transmit value to child thread from parent thread.
oldratlee's avatar
oldratlee 已提交
52

53 54
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.
oldratlee's avatar
oldratlee 已提交
55

oldratlee's avatar
oldratlee 已提交
56
If you have problem or question, please [submit Issue](https://github.com/alibaba/transmittable-thread-local/issues) or play [fork](https://github.com/alibaba/transmittable-thread-local/fork) and pull request dance.
oldratlee's avatar
oldratlee 已提交
57

oldratlee's avatar
oldratlee 已提交
58
# 🎨 Requirements
oldratlee's avatar
oldratlee 已提交
59

oldratlee's avatar
oldratlee 已提交
60
The Requirements listed below is also why I sort out `TTL` in my work.
oldratlee's avatar
oldratlee 已提交
61

oldratlee's avatar
oldratlee 已提交
62 63
- Application container or high layer framework transmit information to low layer sdk.
- Transmit context to logging without application code aware.
oldratlee's avatar
oldratlee 已提交
64

oldratlee's avatar
oldratlee 已提交
65
# 👥 User Guide
oldratlee's avatar
oldratlee 已提交
66

oldratlee's avatar
oldratlee 已提交
67
## 1. simple usage
oldratlee's avatar
oldratlee 已提交
68 69 70

```java
// set in parent thread
71
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
oldratlee's avatar
oldratlee 已提交
72 73 74 75 76
parent.set("value-set-in-parent");

// =====================================================

// read in child thread, value is "value-set-in-parent"
oldratlee's avatar
oldratlee 已提交
77
String value = parent.get();
oldratlee's avatar
oldratlee 已提交
78 79
```

80
This is the function of class [`InheritableThreadLocal`](https://docs.oracle.com/javase/8/docs/api/java/lang/InheritableThreadLocal.html), should use class [`InheritableThreadLocal`](https://docs.oracle.com/javase/8/docs/api/java/lang/InheritableThreadLocal.html) instead.
oldratlee's avatar
oldratlee 已提交
81

82 83
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.
oldratlee's avatar
oldratlee 已提交
84 85 86

The solution is below usage.

oldratlee's avatar
oldratlee 已提交
87
## 2. Transmit value even using thread pool
oldratlee's avatar
oldratlee 已提交
88 89 90

### 2.1 Decorate `Runnable` and `Callable`

oldratlee's avatar
oldratlee 已提交
91 92
Decorate input `Runnable` and `Callable` by [`TtlRunnable`](/src/main/java/com/alibaba/ttl/TtlRunnable.java)
and [`TtlCallable`](src/main/java/com/alibaba/ttl/TtlCallable.java).
oldratlee's avatar
oldratlee 已提交
93 94 95 96

Sample code:

```java
97
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
oldratlee's avatar
oldratlee 已提交
98 99 100
parent.set("value-set-in-parent");

Runnable task = new Task("1");
101
// extra work, create decorated ttlRunnable object
oldratlee's avatar
oldratlee 已提交
102
Runnable ttlRunnable = TtlRunnable.get(task);
103
executorService.submit(ttlRunnable);
oldratlee's avatar
oldratlee 已提交
104 105 106 107

// =====================================================

// read in task, value is "value-set-in-parent"
oldratlee's avatar
oldratlee 已提交
108
String value = parent.get();
oldratlee's avatar
oldratlee 已提交
109 110 111 112 113
```

above code show how to dealing with `Runnable`, `Callable` is similar:

```java
114
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
oldratlee's avatar
oldratlee 已提交
115 116 117
parent.set("value-set-in-parent");

Callable call = new Call("1");
118
// extra work, create decorated ttlCallable object
oldratlee's avatar
oldratlee 已提交
119
Callable ttlCallable = TtlCallable.get(call);
120
executorService.submit(ttlCallable);
oldratlee's avatar
oldratlee 已提交
121 122 123 124

// =====================================================

// read in call, value is "value-set-in-parent"
oldratlee's avatar
oldratlee 已提交
125
String value = parent.get();
oldratlee's avatar
oldratlee 已提交
126 127 128 129 130 131 132
```

### 2.2 Decorate thread pool

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
133
[`com.alibaba.ttl.threadpool.TtlExecutors`](src/main/java/com/alibaba/ttl/threadpool/TtlExecutors.java)
oldratlee's avatar
oldratlee 已提交
134 135
to decorate thread pool.

136
Util class `com.alibaba.ttl.threadpool.TtlExecutors` has below methods:
oldratlee's avatar
oldratlee 已提交
137

oldratlee's avatar
oldratlee 已提交
138 139 140
- `getTtlExecutor`: decorate interface `Executor`
- `getTtlExecutorService`: decorate interface `ExecutorService`
- `getTtlScheduledExecutorService`: decorate interface `ScheduledExecutorService`
oldratlee's avatar
oldratlee 已提交
141 142 143 144 145 146

Sample code:

```java
ExecutorService executorService = ...
// extra work, create decorated executorService object
oldratlee's avatar
oldratlee 已提交
147
executorService = TtlExecutors.getTtlExecutorService(executorService);
oldratlee's avatar
oldratlee 已提交
148

149
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
oldratlee's avatar
oldratlee 已提交
150 151 152 153 154 155 156 157 158 159
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"
oldratlee's avatar
oldratlee 已提交
160
String value = parent.get();
oldratlee's avatar
oldratlee 已提交
161 162
```

163
### 2.3 Use Java Agent to decorate thread pool implementation class
oldratlee's avatar
oldratlee 已提交
164

165
In this usage, transmission is transparent\(no decoration operation\).
oldratlee's avatar
oldratlee 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178

Sample code:

```java
ExecutorService executorService = Executors.newFixedThreadPool(3);

Runnable task = new Task("1");
Callable call = new Call("2");
executorService.submit(task);
executorService.submit(call);

// =====================================================

oldratlee's avatar
oldratlee 已提交
179
// read in Task or Callable, value is "value-set-in-parent"
oldratlee's avatar
oldratlee 已提交
180 181 182
String value = parent.get();
```

183
See demo [`AgentDemo.java`](src/test/java/com/alibaba/demo/agent/AgentDemo.java).
oldratlee's avatar
oldratlee 已提交
184 185

Agent decorate 2 thread pool implementation classes
186
\(implementation code [`TtlTransformer.java`](src/main/java/com/alibaba/ttl/threadpool/agent/TtlTransformer.java)\):
oldratlee's avatar
oldratlee 已提交
187 188 189 190

- `java.util.concurrent.ThreadPoolExecutor`
- `java.util.concurrent.ScheduledThreadPoolExecutor`

oldratlee's avatar
oldratlee 已提交
191
Add start options on Java command:
oldratlee's avatar
oldratlee 已提交
192

193 194
- `-Xbootclasspath/a:/path/to/transmittable-thread-local-2.x.x.jar`
- `-javaagent:/path/to/transmittable-thread-local-2.x.x.jar`
oldratlee's avatar
oldratlee 已提交
195

oldratlee's avatar
oldratlee 已提交
196
**NOTE**
oldratlee's avatar
oldratlee 已提交
197

oldratlee's avatar
oldratlee 已提交
198 199
- Agent modify the `JDK` classes, add code refer to the class of `TTL`, so the jar of `TTL Agent` should add to `bootclasspath`.
- `TTL Agent` modify the class by `javassist`, so the Jar of `javassist` should add to `bootclasspath` too.
oldratlee's avatar
oldratlee 已提交
200 201 202 203

Java command example:

```bash
204 205
java -Xbootclasspath/a:transmittable-thread-local-2.0.0.jar \
    -javaagent:transmittable-thread-local-2.0.0.jar \
oldratlee's avatar
oldratlee 已提交
206
    -cp classes \
207
    com.alibaba.ttl.threadpool.agent.demo.AgentDemo
oldratlee's avatar
oldratlee 已提交
208 209
```

oldratlee's avatar
oldratlee 已提交
210
Run the script [`scripts/run-agent-demo.sh`](scripts/run-agent-demo.sh)
oldratlee's avatar
oldratlee 已提交
211 212
to start demo of "Use Java Agent to decorate thread pool implementation class".

oldratlee's avatar
oldratlee 已提交
213
# 🔌 Java API Docs
214

oldratlee's avatar
oldratlee 已提交
215
The current version Java API documentation: <http://alibaba.github.io/transmittable-thread-local/apidocs/>
216

oldratlee's avatar
oldratlee 已提交
217
# 🍪 Maven dependency
oldratlee's avatar
oldratlee 已提交
218 219 220

```xml
<dependency>
oldratlee's avatar
oldratlee 已提交
221 222 223
    <groupId>com.alibaba</groupId>
    <artifactId>transmittable-thread-local</artifactId>
    <version>2.5.1</version>
oldratlee's avatar
oldratlee 已提交
224 225 226
</dependency>
```

227
Check available version at [search.maven.org](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.alibaba%22%20AND%20a%3A%22transmittable-thread-local%22).
oldratlee's avatar
oldratlee 已提交
228

oldratlee's avatar
oldratlee 已提交
229
# 🗿 More documentation
oldratlee's avatar
oldratlee 已提交
230

oldratlee's avatar
oldratlee 已提交
231
- [🎓 Developer Guide](docs/developer-guide-en.md)
oldratlee's avatar
oldratlee 已提交
232

oldratlee's avatar
oldratlee 已提交
233
# 📚 Related resources
oldratlee's avatar
oldratlee 已提交
234

oldratlee's avatar
oldratlee 已提交
235
## JDK core classes
oldratlee's avatar
oldratlee 已提交
236

oldratlee's avatar
oldratlee 已提交
237 238
- [WeakHashMap](https://docs.oracle.com/javase/8/docs/api/java/util/WeakHashMap.html)
- [InheritableThreadLocal](https://docs.oracle.com/javase/8/docs/api/java/lang/InheritableThreadLocal.html)
oldratlee's avatar
oldratlee 已提交
239

oldratlee's avatar
oldratlee 已提交
240
## Java Agent
oldratlee's avatar
oldratlee 已提交
241

oldratlee's avatar
oldratlee 已提交
242 243 244 245
- [Java Agent规范](https://docs.oracle.com/javase/8/docs/api/java/lang/instrument/package-summary.html)
- [Java SE 6 新特性: Instrumentation 新功能](http://www.ibm.com/developerworks/cn/java/j-lo-jse61/)
- [Creation, dynamic loading and instrumentation with javaagents](http://dhruba.name/2010/02/07/creation-dynamic-loading-and-instrumentation-with-javaagents/)
- [JavaAgent加载机制分析](http://alipaymiddleware.com/jvm/javaagent%E5%8A%A0%E8%BD%BD%E6%9C%BA%E5%88%B6%E5%88%86%E6%9E%90/)
oldratlee's avatar
oldratlee 已提交
246

oldratlee's avatar
oldratlee 已提交
247
## Javassist
oldratlee's avatar
oldratlee 已提交
248

oldratlee's avatar
oldratlee 已提交
249 250 251 252 253 254 255 256 257
- [Getting Started with Javassist](http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/tutorial/tutorial.html)

# 👷 Contributors

- Jerry Lee \<oldratlee at gmail dot com> [@oldratlee](https://github.com/oldratlee)
- Yang Fang \<snoop.fy at gmail dot com> [@driventokill](https://github.com/driventokill)
- wuwen \<wuwen.55 at aliyun dot com> [@wuwen5](https://github.com/wuwen5)
- 牧瑾 \<351450944 at qq dot com> [@LNAmp](https://github.com/LNAmp)
- Your name here :-)