These interfaces correspond to the Reactive-Streams specification

  • Publisher is responsible for producing data. The only method in it is subscribe, which starts a new subscription after receiving a Subscriber
  • Subscriber is responsible for subscribing to consumption data
  • Subscription defines the context of a Subscription, such as canceling or notifying of obtaining the next N data entries
  • Processor - both Subscriber and Publisher

Comparison to Java 8 Streams

Flow / Reactive Streams

Stream

List<Integer> elements = new ArrayList<>();
Flux.just(1, 2, 3, 4).subscribe(elements::add);
List<Integer> elements = Stream.of(1, 2, 3, 4).collect(toList());

The core difference is that Reactive is a push model, whereas the Java 8 Streams are a pull model. In a reactive approach, events are pushed to the subscribers as they come in.

The next thing to notice is a Streams terminal operator is just that, terminal, pulling all the data and returning a result. With Reactive we could have an infinite stream coming in from an external resource, with multiple subscribers attached and removed on an ad hoc basis. We can also do things like combine streams, throttle streams, and apply backpressure, which we will cover next.

Implementations