What you need to understand here is the difference between assembly time and subscription time.
Assembly time is when you create your pipeline by building the operator chain. At this point your publisher is not subscribed yet and you need to think kind of imperatively.
Subscription time is when you trigger the execution by subscribing and the data starts flow through your pipeline. This is when you need to think reactively in terms of callbacks, lambdas, lazy execution, etc..
More on this in the great article by Simon Baslé.
As @akarnokd mentioned in his answer, the getFallback()
method is called imperatively at assembly time since it is not defined as a lambda, just a regular method call.
You can achieve true laziness by one of the below methods:
1, You can use Mono.fromCallable
and put your log inside the lambda:
public static void main(String[] args) {
Mono<Integer> m = Mono.just(1);
m.flatMap(a -> Mono.delay(Duration.ofMillis(5000)).flatMap(p -> Mono.empty()))
.switchIfEmpty(getFallback())
.doOnNext(a -> System.out.println(a))
.block();
}
private static Mono<Integer> getFallback() {
System.out.println("Assembly time, here we are just in the process of creating the mono but not triggering it. This is always called regardless of the emptiness of the parent Mono.");
return Mono.fromCallable(() -> {
System.out.println("Subscription time, this is the moment when the publisher got subscribed. It is got called only when the Mono was empty and fallback needed.");
return 5;
});
}
2, You can use Mono.defer
and delay the execution and the assembling of your inner Mono until subscription:
public static void main(String[] args) {
Mono<Integer> m = Mono.just(1);
m.flatMap(a -> Mono.delay(Duration.ofMillis(5000)).flatMap(p -> Mono.empty()))
.switchIfEmpty(Mono.defer(() -> getFallback()))
.doOnNext(a -> System.out.println(a))
.block();
}
private static Mono<Integer> getFallback() {
System.out.println("Since we are using Mono.defer in the above pipeline, this message gets logged at subscription time.");
return Mono.just(5);
}
Note that your original solution is also perfectly fine. You just need to aware of that the code before returning the Mono is executed at assembly time.
switchIfEmpty
. Please provide the code you are having problems with. – OctuplegetFallback();
inmain
withoutswitchIfEmpty
and the other constructs? Why? – Octuple