Search code examples
javaspring-webfluxreactor

How do I compare the values in 'Mono'?


Comparing the values in Mono<Integer> a and Mono<Integer> b, if the value in Mono<Integer> a is larger, I want to throw an error.

Mono<Integer> a = getA();
Mono<integer> b = getB();

if(a > b) {
 throw new RuntimeException();
}

Solution

  • Mono<Integer> a = Mono.just(12);
    Mono<Integer> b = Mono.just(10);
    
    a.zipWith(b)
        .doOnNext(tuple -> {
            if (tuple.getT1() > tuple.getT2()) {
                throw new RuntimeException();
            }
        }).subscribe();