Introduction to Reactive Programming

Reactor 3 is a library built around Reactive streams specification.

Why

  • To make asynchronous code more readable and maintainable.
  • Declarative paradigm.
  • Build asynchronous processing pipeline.
  • It is event-based model where data is pushed to the consumer, as it become available.
  • Deal with asynchronous sequences of events.

Reactive Stream

Reactive Stream

Idea

sequenceDiagram Publisher ->>+ Subscriber : push event Subscriber -->>- Publisher : feedback

Operator

Applying an operator returns a new intermediate Publisher

sequenceDiagram Publisher ->>+ Operator_Publiser : push event opt Operator Operator_Publiser ->>+ Subscriber : push event Subscriber -->>- Operator_Publiser : void end Operator_Publiser -->>- Publisher : feedback

Flux

Flux is a stream which can emit 0..N elements: - implements Publisher - Flux

Flux<String> fl = Flux.just("a", "b", "c");

Mono

  • Mono is a stream of 0..1 elements:
  • implements Publisher
  • Mono
Mono<String> mn = Mono.just("hello");