Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are Mono<Void> and Mono.empty() different

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 ?

like image 638
Aditya Rewari Avatar asked May 15 '20 15:05

Aditya Rewari


People also ask

What is mono empty ()?

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.

What is mono in Java?

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).

What is flatMapMany?

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 does mono defer do?

What Is the Mono. defer Method? Here, defer takes in a Supplier of Mono publisher and returns that Mono lazily when subscribed downstream.


Video Answer


1 Answers

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())
like image 184
mslowiak Avatar answered Sep 20 '22 00:09

mslowiak