Reactive Programming - Project Reactor

What is it?

  1. The Reactive library, Project Reactor, is a non-blocking application library for the Java Virtual Machine (JVM) that is derived from the Reactive Streams Specification. The Project Reactor library serves as the base for the reactive stack within the Spring ecosystem, and its development is in close cooperation with Spring.
  2. One of the main popular reactive libraries in Java.
  3. Because this is a reactive library, this is a fully non-blocking reactive stream with backpressure supported.
  4. This integrates directly with the Java 8 functional APIs.
  5. The main artifact of that project reactor is the reactor-core.
  6. Spring WebFlux
  7. Spring R2DBC
  8. Redis
  9. Elasticsearch/Opensearch
  10. Mongo
  11. Kafka
  12. RabbitMQ

and so on.

Reading material

  1. https://projectreactor.io/learn
  2. https://github.com/reactor/reactor-core
  3. https://projectreactor.io/docs/core/release/api/reactor/core/publisher/package-summary.html

Implementations for the Publisher interface

https://projectreactor.io/docs/core/release/api/reactor/core/publisher/package-summary.html

  1. The Reactor library provides two implementations for the Publisher interface from the reactive specification.

  2. All of these implementations support non-blocking, asynchronous requirements from the reactive specification.

    1. Reactive Programming - Project Reactor - Mono
    2. Reactive Programming - Project Reactor - Flux
    3. Reactive Programming - Project Reactor - Sinks.Many
  3. public interface UserRepository extends JpaRepository<User, String> {
      Optional<User> findById(String id);
      List<Customer> findByFirstname(String firstname);
    }
    
    public interface UserRepository extends ReactiveCrudRepository<User, String> {
      Mono<User> findById(String id);
      Flux<Customer> findByFirstname(String firstname);
    }
    

How to handle Data from Remote Service/DB

If the library that we are working with already supports reactive-streams, we can just use them without any issues.

Libraries that already support Reactive Streams:

  1. HTTP
    1. Spring Webflux
  2. Relational - R2DBC
    1. Postgres/H2/MySql
  3. Redis - Reactive Redis
  4. Mongo
  5. Elasticsearch
  6. kafka …

If the library does not support reactive-streams, we cannot try to wrap those calls using Reactive interfaces.

This is not valid.

Mono.fromSupplier(() -> httpCall(...)).subscribe(...);

See https://github.com/explorer436/programming-playground/blob/main/java-playground/reactive-programming-examples/reactive-streams-using-project-reactor/src/main/java/reactive/programming/examples/mono_examples/NonBlockingIO.java

Are Mono and Flux Data Structures?

  1. No.
  2. List, Set, etc. - represent data in memory. They represent storage.
  3. Mono and Flux - represent a tunnel/pipe through which data can be transferred from one place to another.
  4. Data Structures and Publisher interfaces are analogous to a water bottles and a water tubes respectively.

Tags

  1. Reactive Programming - Operators