Summary - Reactive
- Java course
- Expert
- Lesson 24/46
- 5 min
Combine Flow API and SubmissionPublisher to write a temperature monitor.
Summary - Reactive
In this group we saw the foundations of Reactive Streams: the three interfaces of the Flow API, the interaction of Publisher and Subscriber, a ready Publisher with SubmissionPublisher, demand control with backpressure, and declarative flow with Project Reactor Mono. These organize asynchronous data flow.
The Flow API contract is standard; Reactor wraps it in a declarative style. Backpressure prevents memory bloat with demand-based delivery. Now we review flow and subscription with two small tasks.
Contract and library
The Flow API only defines the contract; how items are delivered is up to the implementation. SubmissionPublisher offers an asynchronous implementation, while you can also write your own synchronous Publisher. Reactor adds declarative operators on top of this contract.
The key is that, whichever path you pick, the flow goes from Publisher to Subscriber and demand comes from the Subscriber. These two principles are the foundation of all reactive applications. When the flow ends, onComplete comes; on error, onError.
Practice Filter and transform with Mono
Create a Mono with Mono.just(3). Filter with .filter(x -> x > 1). Increase by one with .map(x -> x + 1). Print the result with .subscribe(System.out::println).
Starter code
// Create a Mono with Mono.just
// Add filter and map
// Print with subscribeSample solution
import reactor.core.publisher.Mono;
public class MonoFilterDemo {
public static void main(String[] args) {
Mono.just(3)
.filter(x -> x > 1)
.map(x -> x + 1)
.subscribe(System.out::println);
}
}Expected output
4
Practice Count two subscribers
Define an EmptySubscriber class that implements Subscriber<Integer> (onSubscribe calls request(1), the rest are empty). In main, create new SubmissionPublisher<Integer>(), subscribe two EmptySubscribers, and print the getNumberOfSubscribers result with System.out.println. Then close with pub.close().
Starter code
// Create a SubmissionPublisher
// Add two subscribers
// Print the subscriber countSample solution
import java.util.concurrent.SubmissionPublisher;
import java.util.concurrent.Flow.*;
class EmptySubscriber implements Subscriber<Integer> {
public void onSubscribe(Subscription s) { s.request(1); }
public void onNext(Integer v) { }
public void onError(Throwable t) { }
public void onComplete() { }
}
public class SubCountDemo {
public static void main(String[] args) {
SubmissionPublisher<Integer> pub = new SubmissionPublisher<>();
pub.subscribe(new EmptySubscriber());
pub.subscribe(new EmptySubscriber());
System.out.println(pub.getNumberOfSubscribers());
pub.close();
}
}Expected output
2
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which side sets backpressure?
- Subscriber Correct answer
- Channel
- Publisher
- Buffer
Explanation: The Subscriber sets the demand with request.
2 Complete the subscribe call
pub.____(new EmptySubscriber());
pub.subscribe(new EmptySubscriber());
Explanation: subscribe adds a subscriber.