This is the second part of my blog series on reactive programming, providing an overview of Project Reactor, a reactive library based on the Reactive Streams specification. Part 1 covered an introduction to reactive programming.

1. An Introduction to Project Reactor

Reactive programming is supported by Spring Framework since version 5. That support is built on top of Project Reactor.

Project Reactor (or just Reactor) is a Reactive library for building non-blocking applications on the JVM and is based on the Reactive Streams Specification. The reactor is the foundation of the reactive stack in the Spring ecosystem and it is being developed in close collaboration with Spring. WebFlux, Spring’s reactive-stack web framework, requires Reactor as a core dependency.

1.1 Reactor Modules

Project Reactor consists of a set of modules as listed in the Reactor documentation. The modules are embeddable and interoperable. The main artifact is Reactor Core which holds the reactive types Flux and Mono, that implement the Reactive Stream’s Publisher interface (for details see the first blog post of this series) and a set of operators that can be applied on these.

Some other modules are:

  • Reactor Test — which provides some utilities for testing reactive streams
  • Reactor Extra — that provides some additional Flux operators
  • Reactor Netty — non-blocking and backpressure-ready TCP, HTTP, and UDP clients and servers — based on the Netty framework
  • Reactor Adapter — for adapting to/from other reactive libraries such as RxJava2 and Akka Streams
  • Reactor Kafka — a reactive API for Kafka which enables messages to be published to and consumed from Kafka

1.2 Set up a Project

Before we continue, if you want to set up a project and run some of the code samples below, generate a new Spring Boot application using Spring Initializr. As dependency select Spring Reactive Web. After importing the project in your IDE have a look at the POM file and you will see that the spring-boot-starter-webflux dependency is added which will also bring in the reactor-core dependency. Also, the reactor-test has been added as a dependency. Now you are ready to run the coming code examples.

XML

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

#java #tutorial #reactive programming #reactor #webflux

Project Reactor — Reactive Programming With Spring, Part 2
2.65 GEEK