Worker界面
interface Processor {
Mono<Context> process(Context context);
}
工作流程
List<Processor[]> processorsList = Lists.list(
new Processor[] {processorImpl1},
new Processor[] {processorImpl2}
);
这是我的目标。
/*
List
sequential work
start -> Processor[] -> Processor[]
Array[]
parallel work
start -> Processor
-> Processor
-> Processor
*/
我已经尝试过了。
Flux
.fromIterable(processorsList)
.reduce(
Mono.subscriberContext(),
(contextMono, processors) -> contextMono
.flatMap(c -> Flux
.fromArray(processors)
.parallel()
.flatMap(processor -> processor.process(c))
.reduce(Context::putAll)
.map(c::putAll)))
.flatMap(Function.identity())
.subscriberContext(context)
.subscribe(c -> log.info("end={}", JSON.toJSONString(
c.stream().map(e -> e.getKey() + ":" + e.getValue()).collect(Collectors.toList()))));
我认为这样处理Context有点尴尬。
还有更优雅的方式吗?