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







