我正在尝试使用春季webflux的上下文功能来携带一个简单的变量。我有一个WebFilter设置了一个具有此类变量的上下文,我尝试在通量/流的不同阶段在我的控制器中使用它。在某些时候,我在调用 Flux 类的方法“parallel()”后丢失了它。
过滤器:
public class TestFilter implements WebFilter {
private Logger LOG = LoggerFactory.getLogger(TestFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
return chain.filter(exchange)
.doOnEach(voidSignal -> System.out.println("filter:"+voidSignal.getContext().getOrEmpty("blob"))).subscriberContext(Context.of("blob", "kapoue"));
}
}
控制器:
@RestController
@RequestMapping(TestControllerWebFlux.ROOT)
public class TestControllerWebFlux {
static final String ROOT = "/flux";
static final String TEST = "/test";
private WebClient webClient = WebClient.create();
@GetMapping(
value = TEST,
produces = {MediaType.APPLICATION_JSON_VALUE})
public Mono<String> test() {
System.out.println("controller1:"+Thread.currentThread());
Flux<String> call = webClient.get().uri("http://localhost:" + 8080 + ROOT + "/test2").retrieve().bodyToFlux(Result.class).map(Result::getValue);
return call.map(s -> s+"0")
.doOnEach(stringSignal -> System.out.println("controller2:"+stringSignal.getContext().getOrEmpty("blob")))
.parallel()
.doOnEach(stringSignal -> System.out.println("controller3:"+stringSignal.getContext().getOrEmpty("blob")))
.map(s -> s+"0")
.doOnEach(stringSignal -> System.out.println("controller4:"+stringSignal.getContext().getOrEmpty("blob")))
.reduce((s, s2) -> s+s2)
.doOnEach(stringSignal -> System.out.println("controller5:"+stringSignal.getContext().getOrEmpty("blob")))
.map(s -> {
System.out.println("controller6:"+Thread.currentThread());
return s;
});
}
慕妹3146593
相关分类