liveBook · Manning

Producer

As explained in Spring in Action, Sixth Edition

A producer is a messaging client that creates and sends messages to a destination.

A producer creates data for someone else to receive. In Reactive Streams, that role is called a Publisher: it sends data to a Subscriber through a Subscription. This is a stream of data, not an example of a messaging client sending messages to a destination. section 11.1.1

The Subscriber first receives a Subscription. It then calls request(n) to say how many items it can accept, or cancel() to stop. This request limit provides backpressure: the Publisher should not overwhelm the Subscriber with more data than it can handle. section 11.1.1

public interface Subscription {
  void request(long n);
  void cancel();
}

The two Subscription methods show how a consumer controls how much data the producer sends. section 11.1.1

Reactor supplies two Publisher implementations. Flux represents zero, one, or many items; Mono is for at most one. A producer can therefore be part of a pipeline rather than a method that immediately returns a finished value. section 11.2

Producing a value through a pipeline

Mono.just("Craig") starts with a value. Two map() operations turn it into a greeting, and subscribe() receives and prints the result. The operations form a pipeline; they need not all run on the same thread. section 11.2

Passing a producer to a repository

A reactive POST handler can accept a Mono<Taco> request body and pass it to saveAll(), which accepts any Reactive Streams Publisher. The repository returns a Flux<Taco>; next() converts its first result to the Mono<Taco> returned by the handler. Because the handler does not wait for the request body to become a Taco before being invoked, this avoids one source of blocking in the nonreactive version. section 12.1.2

@PostMapping(consumes = "application/json")
@ResponseStatus(HttpStatus.CREATED)
public Mono<Taco> postTaco(@RequestBody Mono<Taco> tacoMono) {
  return tacoRepo.saveAll(tacoMono).next();
}

The controller listing shows a Mono passed directly to a reactive repository and a Mono returned to the caller. section 12.1.2

How Spring in Action, Sixth Edition covers it

How deep it goes
Defines Publisher and Subscription behavior, then works through Reactor and controller usage; does not implement a messaging client.
Examples
Uses a Mono greeting pipeline and a taco POST handler that passes a Mono to a reactive repository.
What it assumes
Introduces Reactive Streams interfaces directly while using Java expressions and Spring controller syntax in its examples.
What you can do after
Apply it via Reactor Mono or Flux and a reactive Spring repository to pass data through a nonblocking controller.
How it connects
Connects Publishers to Subscribers, Subscriptions, backpressure, Mono, Flux, and reactive repositories.

Also explained in

Compare how 8 books explain producer →

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.