提交 d342d6e5 编写于 作者: oldratlee's avatar oldratlee 🔥

update docs

上级 ed45b8c8
......@@ -56,10 +56,10 @@ If you have problem or question, please [submit Issue](https://github.com/alibab
# 🎨 Requirements
The Requirements listed below is also why I sort out `TTL` in my work.
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.
- Application container or high layer framework transmit information to low layer sdk.
- Transmit context to logging without application code aware.
# 👥 User Guide
......@@ -73,7 +73,7 @@ parent.set("value-set-in-parent");
// =====================================================
// read in child thread, value is "value-set-in-parent"
String value = parent.get();
String value = parent.get();
```
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.
......@@ -98,13 +98,13 @@ parent.set("value-set-in-parent");
Runnable task = new Task("1");
// extra work, create decorated ttlRunnable object
Runnable ttlRunnable = TtlRunnable.get(task);
Runnable ttlRunnable = TtlRunnable.get(task);
executorService.submit(ttlRunnable);
// =====================================================
// read in task, value is "value-set-in-parent"
String value = parent.get();
String value = parent.get();
```
above code show how to dealing with `Runnable`, `Callable` is similar:
......@@ -115,13 +115,13 @@ parent.set("value-set-in-parent");
Callable call = new Call("1");
// extra work, create decorated ttlCallable object
Callable ttlCallable = TtlCallable.get(call);
Callable ttlCallable = TtlCallable.get(call);
executorService.submit(ttlCallable);
// =====================================================
// read in call, value is "value-set-in-parent"
String value = parent.get();
String value = parent.get();
```
### 2.2 Decorate thread pool
......@@ -134,16 +134,16 @@ to decorate thread pool.
Util class `com.alibaba.ttl.threadpool.TtlExecutors` has below methods:
* `getTtlExecutor`: decorate interface `Executor`
* `getTtlExecutorService`: decorate interface `ExecutorService`
* `getTtlScheduledExecutorService`: decorate interface `ScheduledExecutorService`
- `getTtlExecutor`: decorate interface `Executor`
- `getTtlExecutorService`: decorate interface `ExecutorService`
- `getTtlScheduledExecutorService`: decorate interface `ScheduledExecutorService`
Sample code:
```java
ExecutorService executorService = ...
// extra work, create decorated executorService object
executorService = TtlExecutors.getTtlExecutorService(executorService);
executorService = TtlExecutors.getTtlExecutorService(executorService);
TransmittableThreadLocal<String> parent = new TransmittableThreadLocal<String>();
parent.set("value-set-in-parent");
......@@ -156,7 +156,7 @@ executorService.submit(call);
// =====================================================
// read in Task or Callable, value is "value-set-in-parent"
String value = parent.get();
String value = parent.get();
```
### 2.3 Use Java Agent to decorate thread pool implementation class
......@@ -187,15 +187,15 @@ Agent decorate 2 thread pool implementation classes
- `java.util.concurrent.ThreadPoolExecutor`
- `java.util.concurrent.ScheduledThreadPoolExecutor`
Add start options on Java command:
Add start options on Java command:
- `-Xbootclasspath/a:/path/to/transmittable-thread-local-2.x.x.jar`
- `-javaagent:/path/to/transmittable-thread-local-2.x.x.jar`
**NOTE**
**NOTE**
* 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.
- 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.
Java command example:
......@@ -217,9 +217,9 @@ The current version Java API documentation: <http://alibaba.github.io/transmitta
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.5.1</version>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.5.1</version>
</dependency>
```
......
......@@ -185,7 +185,8 @@ String value = parent.get();
### 2.3 使用`Java Agent`来修饰`JDK`线程池实现类
这种方式,实现线程池的传递是透明的,代码中没有修饰`Runnable`或是线程池的代码。即可以做到应用代码 **无侵入**,后面文档有结合实际场景的架构对这一点的说明。
这种方式,实现线程池的传递是透明的,代码中没有修饰`Runnable`或是线程池的代码。即可以做到应用代码 **无侵入**
\# 关于 **无侵入** 的更多说明参见文档[`Java Agent`方式对应用代码无侵入](docs/developer-guide.md#java-agent%E6%96%B9%E5%BC%8F%E5%AF%B9%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81%E6%97%A0%E4%BE%B5%E5%85%A5)
示例代码:
......@@ -240,8 +241,8 @@ java -Xbootclasspath/a:transmittable-thread-local-2.0.0.jar \
由于`Runnable``Callable`的修饰代码,是在线程池类中插入的。下面的情况会让插入的代码被绕过,传递会失效。
- 用户代码中继承`java.util.concurrent.ThreadPoolExecutor``java.util.concurrent.ScheduledThreadPoolExecutor`
覆盖了`execute``submit``schedule`等提交任务的方法,并且没有调用父类的方法。
修改线程池类的实现,`execute``submit``schedule`等提交任务的方法禁止这些被覆盖,可以规避这个问题。
覆盖了`execute``submit``schedule`等提交任务的方法,并且没有调用父类的方法。
修改线程池类的实现,`execute``submit``schedule`等提交任务的方法禁止这些被覆盖,可以规避这个问题。
- 目前,没有修饰`java.util.Timer`类,使用`Timer`时,`TTL`会有问题。
# 🔌 Java API Docs
......@@ -254,9 +255,9 @@ java -Xbootclasspath/a:transmittable-thread-local-2.0.0.jar \
```xml
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.5.1</version>
<groupId>com.alibaba</groupId>
<artifactId>transmittable-thread-local</artifactId>
<version>2.5.1</version>
</dependency>
```
......@@ -265,9 +266,9 @@ java -Xbootclasspath/a:transmittable-thread-local-2.0.0.jar \
# ❓ FAQ
- Mac OS X下,使用javaagent,可能会报`JavaLaunchHelper`的出错信息。
JDK Bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8021205
可以换一个版本的JDK。我的开发机上`1.7.0_40`有这个问题,`1.6.0_51``1.7.0_45`可以运行。
\# `1.7.0_45`还是有`JavaLaunchHelper`的出错信息,但不影响运行。
JDK Bug: <http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=8021205>
可以换一个版本的JDK。我的开发机上`1.7.0_40`有这个问题,`1.6.0_51``1.7.0_45`可以运行。
\# `1.7.0_45`还是有`JavaLaunchHelper`的出错信息,但不影响运行。
# 🗿 更多文档
......
......@@ -119,6 +119,8 @@ String result = runSupplierWithCaptured(captured, () -> {
使用`Java Agent`方式,应用无需修改代码,即做到 相对应用代码 透明地完成跨线程池的上下文传递。
更多关于应用场景的了解说明参见文档[需求场景](requirement-scenario.md)
## 如何权衡`Java Agent`方式的失效情况
把这些失效情况都解决了是最好的,但复杂化了实现。下面是一些权衡:
......
......@@ -50,9 +50,9 @@ PS: 多谢 [@wyzssw](https://github.com/https://github.com/wyzssw) 对分布
1. 请求进入`PAAS`容器,提取上下文信息并设置好上下文。
2. 进入用户应用处理业务,业务调用`SDK`(如`DB`、消息、etc)。
用户应用会使用线程池,所以调用`SDK`的线程可能不是请求的线程。
用户应用会使用线程池,所以调用`SDK`的线程可能不是请求的线程。
3. 进入`SDK`处理。
提取上下文的信息,决定是否符合拒绝处理。
提取上下文的信息,决定是否符合拒绝处理。
整个过程中,上下文的传递 对于 **用户应用代码** 期望是透明的。
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册