Reactive Programming - Project Reactor - Mono

Mono

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html

A Mono is a Reactive Streams Publisher with basic rx operators that emits at most one item via the onNext signal then terminates with an onComplete signal (successful Mono, with or without value), or only emits a single onError signal (failed Mono).

  1. Can emit 0 or 1 item
  2. No stream
  3. Followed by an onComplete or onError
  4. No backpressure
  5. A lightweight publisher
  6. Request -> Response model
  7. Why do we need Mono at all? For convenience.

Working with a Mono

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/Mono.html

  1. As shown in the image, only one item will be published by the Publisher to the Subscriber. The last line represents the completion of the stream. The operator box represents the transform operation for this Mono stream.
  2. That transformation operation will be applied to the item of Mono stream using the onNext() method.
  3. The red X icon represents an error that happened when applying the transformation to the item and this is the onError().
  4. If no error happened, the transformation will be applied to the item successfully and using the onComplete() method.

Creating Mono publisher using the Factory methods

This can be used to support existing codebases.

Factory methods Usage
just when the value is in memory already
empty no item to emit
error emit error
fromSupplier defer execution by using Supplier<T>
fromCallable defer execution by using Callable<T>
fromFuture Publisher from CompletableFuture<T>