As per my understanding, in Spring WebFlux reactor
Mono<Void>
refers for a void Mono
Mono.empty()
refers to void, as calling anything over this gives a null pointer.
How do these stand different in their usage ?
Mono. empty() is a method invocation that returns a Mono that completes emitting no item. It represents an empty publisher that only calls onSubscribe and onComplete . This Publisher is effectively stateless, and only a single instance exists.
A Mono<T> is a Reactive Streams Publisher , also augmented with a lot of operators that can be used to generate, transform, orchestrate Mono sequences. It is a specialization of Flux that can emit at most 1 <T> element: a Mono is either valued (complete with element), empty (complete without element) or failed (error).
The flatMapMany is a generic operator on Mono that returns a Publisher. Let's apply flatMapManyto our solution: private <T> Flux<T> monoTofluxUsingFlatMapMany(Mono<List<T>> monoList) { return monoList .flatMapMany(Flux::fromIterable) .log(); }
What Is the Mono. defer Method? Here, defer takes in a Supplier of Mono publisher and returns that Mono lazily when subscribed downstream.
Mono<T>
is a generic type - in your specific situation it represents Void
type as Mono<Void>
Mono.empty()
- return a Mono that completes without emitting any item.
Let's assume that you got a method:
private Mono<Void> doNothing() {
return Mono.empty();
}
Whe you want to chain anything after the method call it won't work with flatMap
as it is a completed Mono
.
In case you want continue another job after that method you can use operator then
:
doNothing().then(doSomething())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With