本文主要研究下reactor异步线程的变量传递html
在传统的请求/应答同步模式中,使用threadlocal来传递上下文变量是很是方便的,能够免得在每一个方法参数添加公用的变量,好比当前登陆用户。可是业务方法可能使用了async或者在其余线程池中异步执行,这个时候threadlocal的做用就失效了。java
这个时候的解决办法就是采起propagation模式,即在同步线程与异步线程衔接处传播这个变量。react
好比spring就提供了TaskDecorator,经过实现这个接口,能够本身控制传播那些变量。例如:web
class MdcTaskDecorator implements TaskDecorator { @Override public Runnable decorate(Runnable runnable) { // Right now: Web thread context ! // (Grab the current thread MDC data) Map<String, String> contextMap = MDC.getCopyOfContextMap(); return () -> { try { // Right now: @Async thread context ! // (Restore the Web thread context's MDC data) MDC.setContextMap(contextMap); runnable.run(); } finally { MDC.clear(); } }; } }
这里注意在finally里头clear
配置这个taskDecoratorspring
@EnableAsync @Configuration public class AsyncConfig implements AsyncConfigurer { @Override public Executor getAsyncExecutor() { ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); executor.setTaskDecorator(new MdcTaskDecorator()); executor.initialize(); return executor; } }
完整实例详见 Spring 4.3: Using a TaskDecorator to copy MDC data to @Async threads
spring5引入webflux,其底层是基于reactor,那么reactor如何进行上下文变量的传播呢?官方提供了Context对象来替代threadlocal。segmentfault
其特性以下:api
@Test public void testSubscriberContext(){ String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key))) .subscriberContext(ctx -> ctx.put(key, "World")); StepVerifier.create(r) .expectNext("Hello World") .verifyComplete(); }
这里从最底部的subscriberContext设置message值为World,而后flatMap里头经过subscriberContext来访问。
@Test public void testContextSequence(){ String key = "message"; Mono<String> r = Mono.just("Hello") //NOTE 这个subscriberContext设置的过高了 .subscriberContext(ctx -> ctx.put(key, "World")) .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.getOrDefault(key, "Stranger"))); StepVerifier.create(r) .expectNext("Hello Stranger") .verifyComplete(); }
因为这个例子的subscriberContext设置的过高了,不能做用在flatMap里头的Mono.subscriberContext()
@Test public void testContextImmutable(){ String key = "message"; Mono<String> r = Mono.subscriberContext() .map( ctx -> ctx.put(key, "Hello")) //这里返回了一个新的,所以上面的设置失效了 .flatMap( ctx -> Mono.subscriberContext()) .map( ctx -> ctx.getOrDefault(key,"Default")); StepVerifier.create(r) .expectNext("Default") .verifyComplete(); }
subscriberContext永远返回一个新的
@Test public void testReadOrder(){ String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key))) .subscriberContext(ctx -> ctx.put(key, "Reactor")) .subscriberContext(ctx -> ctx.put(key, "World")); StepVerifier.create(r) .expectNext("Hello Reactor") .verifyComplete(); }
operator只会读取离它最近的一个context
@Test public void testContextBetweenFlatMap(){ String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key))) .subscriberContext(ctx -> ctx.put(key, "Reactor")) .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key))) .subscriberContext(ctx -> ctx.put(key, "World")); StepVerifier.create(r) .expectNext("Hello Reactor World") .verifyComplete(); }
flatMap读取离它最近的context
@Test public void testContextInFlatMap(){ String key = "message"; Mono<String> r = Mono.just("Hello") .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key)) ) .flatMap( s -> Mono.subscriberContext() .map( ctx -> s + " " + ctx.get(key)) .subscriberContext(ctx -> ctx.put(key, "Reactor")) ) .subscriberContext(ctx -> ctx.put(key, "World")); StepVerifier.create(r) .expectNext("Hello World Reactor") .verifyComplete(); }
这里第一个flatMap没法读取第二个flatMap内部的context
reactor经过提供Context来实现了相似同步线程threadlocal的功能,很是强大,值得好好琢磨。异步