README-EN.md 10.4 KB
Newer Older
1
Transmittable ThreadLocal(TTL)
oldratlee's avatar
oldratlee 已提交
2 3
=====================================

oldratlee's avatar
oldratlee 已提交
4
[![Build Status](https://travis-ci.org/alibaba/transmittable-thread-local.svg?branch=master)](https://travis-ci.org/alibaba/transmittable-thread-local)
oldratlee's avatar
oldratlee 已提交
5
[![JDK6 Build Status](https://img.shields.io/appveyor/ci/oldratlee/transmittable-thread-local/master.svg?label=jdk6%20build)](https://ci.appveyor.com/project/oldratlee/transmittable-thread-local)
oldratlee's avatar
oldratlee 已提交
6
[![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 已提交
7
[![Maven Central](https://maven-badges.herokuapp.com/maven-central/com.alibaba/transmittable-thread-local/badge.svg)](https://maven-badges.herokuapp.com/maven-central/com.alibaba/transmittable-thread-local/)
oldratlee's avatar
oldratlee 已提交
8 9
[![GitHub release](https://img.shields.io/github/release/alibaba/transmittable-thread-local.svg)](https://github.com/alibaba/transmittable-thread-local/releases)
[![Dependency Status](https://www.versioneye.com/user/projects/56c0a36218b271002c699dca/badge.svg)](https://www.versioneye.com/user/projects/56c0a36218b271002c699dca)  
T
The Gitter Badger 已提交
10
[![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 已提交
11
[![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 已提交
12 13
[![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 已提交
14
[![License](https://img.shields.io/badge/license-Apache%202-4EB1BA.svg)](https://www.apache.org/licenses/LICENSE-2.0.html)
oldratlee's avatar
oldratlee 已提交
15

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

oldratlee's avatar
oldratlee 已提交
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
----------------------------------------

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


- [:wrench: Functions](#wrench-functions)
- [:art: Requirements](#art-requirements)
- [:busts_in_silhouette: User Guide](#busts_in_silhouette-user-guide)
    - [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)
        - [2.3. Use Java Agent to decorate thread pool implementation class](#23-use-java-agent-to-decorate-thread-pool-implementation-class)
- [:electric_plug: Java API Docs](#electric_plug-java-api-docs)
- [:cookie: Maven dependency](#cookie-maven-dependency)
- [:books: Related resources](#books-related-resources)
    - [Jdk core classes](#jdk-core-classes)
    - [Java Agent](#java-agent)
    - [Javassist](#javassist)

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

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

# :wrench: Functions
oldratlee's avatar
oldratlee 已提交
44

45
:point_right: Transmit `ThreadLocal` value between threads, even using thread cached components like thread pool.
oldratlee's avatar
oldratlee 已提交
46

47
Class [`InheritableThreadLocal`](http://docs.oracle.com/javase/7/docs/api/java/lang/InheritableThreadLocal.html) in `JDK`
48
can transmit value to child thread from parent thread.
oldratlee's avatar
oldratlee 已提交
49

50 51
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 已提交
52

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

oldratlee's avatar
oldratlee 已提交
55
# :art: Requirements
oldratlee's avatar
oldratlee 已提交
56

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

* Application container or high layer framework transmit information to low layer sdk.
* Transmit context to logging without application code aware.

oldratlee's avatar
oldratlee 已提交
62
# :busts_in_silhouette: User Guide
oldratlee's avatar
oldratlee 已提交
63

oldratlee's avatar
oldratlee 已提交
64
## 1. simple usage
oldratlee's avatar
oldratlee 已提交
65 66 67

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

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

// read in child thread, value is "value-set-in-parent"
String value = parent.get(); 
```

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

79 80
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 已提交
81 82 83

The solution is below usage.

oldratlee's avatar
oldratlee 已提交
84
## 2. Transmit value even using thread pool
oldratlee's avatar
oldratlee 已提交
85 86 87

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

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

Sample code:

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

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

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

// read in task, value is "value-set-in-parent"
String value = parent.get(); 
```

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

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

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

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

// read in call, value is "value-set-in-parent"
String value = parent.get(); 
```

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

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

135 136
* `getTtlExecutor`: decorate interface `Executor`
* `getTtlExecutorService`: decorate interface `ExecutorService`
oldratlee's avatar
oldratlee 已提交
137 138 139 140 141 142 143
* `ScheduledExecutorService`: decorate interface `ScheduledExecutorService`

Sample code:

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

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

### 2.3. Use Java Agent to decorate thread pool implementation class

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

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 已提交
176
// read in Task or Callable, value is "value-set-in-parent"
oldratlee's avatar
oldratlee 已提交
177 178 179
String value = parent.get();
```

180
See demo [`AgentDemo.java`](src/test/java/com/alibaba/ttl/threadpool/agent/AgentDemo.java).
oldratlee's avatar
oldratlee 已提交
181 182

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

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

oldratlee's avatar
oldratlee 已提交
188
Add start options on Java command: 
oldratlee's avatar
oldratlee 已提交
189

190 191
- `-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 已提交
192 193 194

**NOTE**

195 196
* 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 已提交
197 198 199 200

Java command example:

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

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

oldratlee's avatar
oldratlee 已提交
210
# :electric_plug: Java API Docs
211

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

oldratlee's avatar
oldratlee 已提交
214
# :cookie: Maven dependency
oldratlee's avatar
oldratlee 已提交
215 216 217 218

```xml
<dependency>
	<groupId>com.alibaba</groupId>
219
	<artifactId>transmittable-thread-local</artifactId>
oldratlee's avatar
oldratlee 已提交
220
	<version>2.1.1</version>
oldratlee's avatar
oldratlee 已提交
221 222 223
</dependency>
```

224
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 已提交
225

oldratlee's avatar
oldratlee 已提交
226
# :books: Related resources
oldratlee's avatar
oldratlee 已提交
227

oldratlee's avatar
oldratlee 已提交
228
## Jdk core classes
oldratlee's avatar
oldratlee 已提交
229 230 231 232

* [WeakHashMap](http://docs.oracle.com/javase/7/docs/api/java/util/WeakHashMap.html)
* [InheritableThreadLocal](http://docs.oracle.com/javase/7/docs/api/java/lang/InheritableThreadLocal.html)

oldratlee's avatar
oldratlee 已提交
233
## Java Agent
oldratlee's avatar
oldratlee 已提交
234

oldratlee's avatar
oldratlee 已提交
235
* [Java Agent规范](http://docs.oracle.com/javase/7/docs/api/java/lang/instrument/package-summary.html)
oldratlee's avatar
oldratlee 已提交
236 237 238
* [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 已提交
239

oldratlee's avatar
oldratlee 已提交
240
## Javassist
oldratlee's avatar
oldratlee 已提交
241

oldratlee's avatar
oldratlee 已提交
242
* [Getting Started with Javassist](http://www.csg.ci.i.u-tokyo.ac.jp/~chiba/javassist/tutorial/tutorial.html)