javareactive-programmingproject-reactor

Java Reactor function call issue


I am trying to work with various Mono operations, in the below code block

@Test
public void just() throws InterruptedException {
    Mono<Boolean> test = Mono.zip(testMono1(), testMono3())
                             .flatMap(tuple -> {
                                 System.out.println("test flatmap" + tuple.getT1());
                                 return Mono.just(tuple.getT2());
                             });
    test.block();

    // test.subscribe(val -> log.info("Subscriber received: {}", val));
}

public Mono<Void> testMono1() {
    return Mono.fromRunnable(() -> {
                   System.out.println("test1");
               })
               .then();
}

public Mono<Boolean> testMono3() {
    return Mono.fromRunnable(() -> {
                   System.out.println("test3");
               })
               .thenReturn(true);
}

test3 is never printed, and neither test flatmap gets printed.

I have tried to subscribe to the test mono, also I tried blocking calls as shown in the code, but I am still not able to figure out the issue.

My output

test1

Process finished with exit code 0

If I uncomment the subscribe call the output is:

test1

test1

Process finished with exit code 0

Solution

  • Mono<Void> doesn't produce any item because Void cannot be instantiated. So the Mono just completes if there is no error in the previous step.

    Because of that, Mono.zip cannot produce a pair of two elements, and just completes too. flatMap is not called as there is no items. And the result is an empty Mono<Boolean> (like null)