0
votes

In the below code logic, the someFunction() is called but the mono inside functionThree never gets executed. Even thought the "Valid" is logged but the returned mono from repository is never executed. Why is that, even though I have subscribed the mono in SomeFunction()?

SomeFunction() {
    return functionOne(param).flatMap(param -> functionTwo(param)).then(Boolean.TRUE::toString).subscribe();
}
Mono<Integer> functionOne(int param) {
    return Mono.just(param);
}
Mono<List<Integer>> functionTwo(int param) {
    return Mono.just(param)
    .filter(param -> param != 1)
    .switchIfEmpty(functionThree(param));
}

Mono<List<Integer>> functionThree(int param) {
    log.info("Valid");
    return reactiveStream.findById(param)
    .map(obj -> {
        log.info("INSIDE STREAM");
        return obj.getId;
    })
    .collectList();
}
1
Because that setup before it is not empty. It would be empty if param == 1. - akarnokd
@akarnokd yes param is 1 only. Still it is not getting executed. - Spring boot progammer
How do you tell it does not execute? Try putting the call to functionThree into a deferred source: .switchIfEmpty(Mono.defer(() -> functionThree(param))). - akarnokd
@akarnokd yes still not executed, I have tried with defer. And How do I tell it did not execute, please check the edited version now, the statement is not logged. - Spring boot progammer
Time to put doOnNexts all over the place with logging to see where it stops. - akarnokd

1 Answers

0
votes

As comment said, Because that setup before it is not empty. the "Valid" logged and executed at assembly stage. Even if you don't subscribed, it always execute it.