CodeBlanks Logo Icon CodeBlanks

Flow API

Learn reactive streams with the Java 9 Flow API.

The Flow API

Since Java 9, Reactive Streams has been part of the platform through the java.util.concurrent.Flow class. This API defines asynchronous data flow with three standard interfaces: a Publisher produces data, a Subscriber consumes it, and a Subscription controls the flow.

The flow direction is from Publisher to Subscriber; but the Subscriber states how much data it wants with request. This is called backpressure and stops the producer from outrunning the consumer. The Flow API lets frameworks agree on a common base.

The roles of the three interfaces

A Publisher has a single method: subscribe(Subscriber). It registers a subscriber. A Subscriber has four methods: onSubscribe receives the Subscription, onNext receives an item, onError an error, and onComplete the end of the flow.

A Subscription requests n items with request(n) and cancels the flow with cancel. This structure allows both synchronous and asynchronous delivery. What matters is the contract; timing is implementation-dependent.

Practice Send data with a synchronous Publisher

Define an OnePublisher class that implements Publisher<String>. Its subscribe method calls onSubscribe; the Subscription's request method calls s.onNext("hello") and s.onComplete(). In main, create an anonymous Subscriber with new OnePublisher().subscribe(...); onSubscribe calls request(1), and onNext prints the value with System.out.println.

Starter code

Main.java
// Define the OnePublisher class
// Add subscribe and onNext
// Subscribe in main and print

Sample solution

Main.java
import java.util.concurrent.Flow.*;

class OnePublisher implements Publisher<String> {
    public void subscribe(Subscriber<? super String> s) {
        s.onSubscribe(new Subscription() {
            public void request(long n) { s.onNext("hello"); s.onComplete(); }
            public void cancel() { }
        });
    }
}

public class FlowDemo {
    public static void main(String[] args) {
        new OnePublisher().subscribe(new Subscriber<String>() {
            public void onSubscribe(Subscription s) { s.request(1); }
            public void onNext(String v) { System.out.println(v); }
            public void onError(Throwable t) { }
            public void onComplete() { }
        });
    }
}

Expected output

hello

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 In which package is Java's built-in Reactive Streams API?

  • java.util.stream
  • java.io
  • java.nio
  • java.util.concurrent.Flow Correct answer

Explanation: The Flow class holds the Reactive Streams interfaces.

2 Complete the Subscriber import

Question
import java.util.concurrent.____.Subscriber;
Answer
import java.util.concurrent.Flow.Subscriber;

Explanation: The Flow class holds the Subscriber interface.

3 Match the Flow interfaces

Term Meaning
Publisher Produces data
Subscriber Consumes data
Subscription Controls the flow
onNext Item notification

All lessons in Expert