Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is then, thenEmpty, thenMany and flatMapMany in spring webflux?

I don't understand the use and the difference between then, thenEmpty, thenMany and flatMapMany on Flux or Mono in spring webflux.

like image 955
Amr Khaled Avatar asked Jan 14 '18 22:01

Amr Khaled


People also ask

What is flatMapMany?

flatMap. flatMapMany. Java Streams. Accepts Function SE -> SE which takes a synchronous element and returns a single synchronous element. Returns a Java Stream.

What is mono and flux in reactive programming Spring?

Project Reactor is the implementation of Reactive Streams specification. Reactor provides two types: Mono: implements Publisher and returns 0 or 1 elements. Flux: implements Publisher and returns N elements.

What is backpressure in Spring WebFlux?

3. Handling Backpressure in Spring WebFlux. Spring WebFlux provides an asynchronous non-blocking flow of reactive streams. The responsible for backpressure within Spring WebFlux is the Project Reactor. It internally uses Flux functionalities to apply the mechanisms to control the events produced by the emitter.

What is the difference between flux and Mono?

A Flux object represents a reactive sequence of 0.. N items, while a Mono object represents a single-value-or-empty (0..1) result. This distinction carries a bit of semantic information into the type, indicating the rough cardinality of the asynchronous processing.


1 Answers

  • flatMap vs flatMapMany

In functional programming, flatMap returns the same type than the type that bear the method, so for Mono<T>, flatMap returns a Mono. Which means that only one element can be emitted by the inner Publisher (or that it is truncated). We enforced that by having Mono#flatMap take a Function<T, Mono<R>>.

As a consequence, we needed an alternative for more arbitrary Publisher that could emit more than one element. Hence Mono#flatMapMany(Function<T, Publisher<R>>) which returns a Flux<R>.

TL;DR: Mono#flatMap is for asynchronous but 1-to-1 transformation of the element in the source Mono, Mono#flatMapMany is for 1-to-N asynchronous transformation (like Flux#flatMap).

  • then, thenEmpty and thenMany

All the thenXXX methods on Mono have one semantic in common: they ignore the source onNext signals and react on completion signals (onComplete and onError), continuing the sequence at this point with various options. As a consequence, this can change the generic type of the returned Mono:

  1. then will just replay the source terminal signal, resulting in a Mono<Void> to indicate that this never signals any onNext.
  2. thenEmpty not only returns a Mono<Void>, but it takes a Mono<Void> as a parameter. It represents a concatenation of the source completion signal then the second, empty Mono completion signal. In other words, it completes when A then B have both completed sequentially, and doesn't emit data.
  3. thenMany waits for the source to complete then plays all the signals from its Publisher<R> parameter, resulting in a Flux<R> that will "pause" until the source completes, then emit the many elements from the provided publisher before replaying its completion signal as well.
like image 111
Simon Baslé Avatar answered Sep 28 '22 05:09

Simon Baslé