What are the Mono and Flux types?

Technology CommunityCategory: SpringWhat are the Mono and Flux types?
VietMX Staff asked 3 years ago

The WebFlux framework in Spring Framework 5 uses Reactor as its async foundation.

This project provides two core types: Mono to represent a single async value, and Flux to represent a stream of async values. They both implement the Publisher interface defined in the Reactive Streams specification.

Mono implements Publisher and returns 0 or 1 elements:

public abstract class Mono<T> implements Publisher<T> {...}

Also, Flux implements Publisher and returns N elements:

public abstract class Mono<T> implements Publisher<T> {...}

By definition, the two types represent streams, hence they’re both lazy, which means nothing is executed until we consume the stream using the subscribe() method. Both types are immutable, therefore calling any method will return a new instance of Flux or Mono.