Building RESTful Web Services with Spring 5(Second Edition)
上QQ阅读APP看书,第一时间看更新

Mono

Mono is another type of Reactor can emit only one item at the most. An asynchronous task that just wants to signal completion can use a Mono. Mono mainly deals with a stream of one element, as opposed to Flux's N elements.

Both Flux and Mono make use of this semantic by coercing to the relevant type when using some operations. For example, concatenating two Monos together will produce a Flux; on the other hand, calling single() on Flux<T> will return a Mono <T>.

Both Flux and Mono are Reactive Streams (RS) publisher implementations and conform to Reactive-pull back pressure.

Mono is used in specific scenarios like an HTTP request that produces only one response. In such cases, using Mono would be the right choice.

Returning a Mono<HttpResponse> for an HTTP request like the scenario mentioned earlier is better than returning a Flux<HttpResponse>, as it offers only operators that are relevant to a context of zero items or one item.

Mono can be used to represent no-value asynchronous processes that only have the concept of completion.