liveBook · Manning

Reactive programming

As explained in Spring in Action, Sixth Edition

Reactive programming is a programming approach based on asynchronous processing of data streams and the propagation of change over time.

Reactive programming builds a pipeline for data to flow through, rather than a list of instructions that must finish one after another. Each stage can work on data and pass it along. This can be useful when waiting for I/O would otherwise leave a thread blocked, but it is not the right fit for every task. section 11.1 section 11.2

Think of a newspaper subscription

A useful subscription delivers each issue when it is ready, while work on later issues continues. Waiting until an entire year’s issues are complete before delivering any resembles processing a whole batch before passing it to the next task. section 11.0

In Project Reactor, a Mono is a pipeline for at most one item; a Flux can carry zero, one, or many items. Both implement Reactive Streams’ Publisher. Operations such as map() transform data as it passes through a pipeline. Do not assume every operation runs on the same thread—or that it necessarily runs on a different one. section 11.2

String name = "Craig";
String capitalName = name.toUpperCase();
String greeting = "Hello, " + capitalName + "!";
System.out.println(greeting);

Follow the imperative greeting example: each statement completes before the next begins. section 11.2

Mono.just("Craig")
    .map(n -> n.toUpperCase())
    .map(cn -> "Hello, " + cn + "!")
    .subscribe(System.out::println);

Compare the Reactor version: just() supplies a name, map() stages transform it, and subscribe() prints the greeting. section 11.2

Spring supports reactive programming through Spring WebFlux, a reactive web framework that borrows from Spring MVC. section 1.4.1

How Spring in Action, Sixth Edition covers it

How deep it goes
Explains the imperative contrast and introduces pipelines, Mono, Flux, and basic operations without detailing execution internals.
Examples
Uses newspaper delivery and two Java greeting implementations to contrast batch-like steps with a Reactor pipeline.
What it assumes
Starts from familiar sequential Java-style code before introducing reactive pipelines.
What you can do after
Apply it via Project Reactor to express a simple transformation as a Mono pipeline.
How it connects
Connects reactive programming to imperative programming, I/O blocking, Reactive Streams, Project Reactor, and Spring WebFlux.

Also explained in

Compare how 6 books explain reactive programming →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Spring in Action, Sixth Edition. The section links point to the parts of the book it draws on.