【问题标题】:Project Reactor: Sum two Mono<Integer> without blockingProject Reactor:将两个 Mono<Integer> 相加而不会阻塞
【发布时间】:2021-09-28 09:53:21
【问题描述】:

假设我们有:

Mono&lt;Integer&gt; int1 = Mono.just(1)Mono&lt;Integer&gt; int2 = Mono.just(10)。我想在不阻塞的情况下得到这两个整数的总和。

以阻塞方式,我会这样做: Mono&lt;Integer&gt; 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();
    

    【讨论】:

    • 在你偶然使用just而不是zip的代码中
    猜你喜欢
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-28
    • 2019-07-31
    • 1970-01-01
    相关资源
    最近更新 更多