【问题标题】:Project Reactor: Sum two Mono<Integer> without blockingProject Reactor:将两个 Mono<Integer> 相加而不会阻塞
【发布时间】:2021-09-28 09:53:21
【问题描述】:
假设我们有:
Mono<Integer> int1 = Mono.just(1) 和 Mono<Integer> int2 = Mono.just(10)。我想在不阻塞的情况下得到这两个整数的总和。
以阻塞方式,我会这样做:
Mono<Integer> result = Mono.just(int1.block() + int2.block())
提前致谢!
【问题讨论】:
标签:
project-reactor
nonblocking
reactor
【解决方案1】:
试试zip:
Mono<Integer> int1 = Mono.just(1); //or any other mono, including lazy one
Mono<Integer> int2 = Mono.just(10); //same
Mono<Integer> sum = Mono.zip(
int1, int2,
//zip defaults to producing a Tuple2
//but for the 2 args version you can provide a BiFunction
(a, b) -> a + b
);
//if you want to verify, eg. in a test:
StepVerifier.create(sum)
.expectNext(11)
.verifyComplete();