Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Java

Flow.Subscriber — receiving items with demand control

Flow.Subscriber<T> receives items and terminal signals from a publisher. Its subscription lets it express demand through request(n), allowing the receiver to control how many items are delivered.

Version: Java SE 21; repository runtime, production runtime not attested

Syntax

class Receiver<T> implements Flow.Subscriber<T> {
    public void onSubscribe(Flow.Subscription subscription) { }
    public void onNext(T item) { }
    public void onError(Throwable throwable) { }
    public void onComplete() { }
}

Flow.Subscriber<T> in java.util.concurrent is a receiver interface. T specifies the item type. Calls for one Flow.Subscription are strictly sequential; this does not imply attachment to a single thread.

Flow.Subscriber.onSubscribe(Flow.Subscription subscription) receives the subscription before the other methods. Store it and request items. Flow.Subscriber.onNext(T item) handles one item. request(n) adds demand rather than replacing previous demand. On an active subscription, calling request(n) with n <= 0 produces an onError signal with IllegalArgumentException. After cancellation or a terminal signal, calling request(n) does not trigger a new onError signal. Items are not delivered without demand. This is flow control, or backpressure.

Flow.Subscriber.onError(Throwable throwable) signals an unrecoverable error, while Flow.Subscriber.onComplete() signals normal completion. Both are terminal: after either, the subscription invokes no further receiver methods. Do not use exceptions thrown from receiver methods as an error protocol: the interface does not guarantee correct subsequent behavior. cancel() is best-effort: messages may still arrive, and a terminal signal is not guaranteed.

The example uses SubmissionPublisher and requests another item only after processing the current one. close() ends publishing but does not replace waiting for delivery; done.join() waits for the receiver to finish. The example demonstrates the normal path; an error completes done exceptionally.

Examples

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Flow;
import java.util.concurrent.SubmissionPublisher;

public class Main {
    static final class Receiver implements Flow.Subscriber<Integer> {
        private Flow.Subscription subscription;
        final CompletableFuture<Void> done = new CompletableFuture<>();

        @Override
        public void onSubscribe(Flow.Subscription subscription) {
            this.subscription = subscription;
            subscription.request(1);
        }

        @Override
        public void onNext(Integer item) {
            System.out.println("item=" + item);
            subscription.request(1);
        }

        @Override
        public void onError(Throwable throwable) {
            done.completeExceptionally(throwable);
        }

        @Override
        public void onComplete() {
            System.out.println("complete");
            done.complete(null);
        }
    }

    public static void main(String[] args) {
        Receiver receiver = new Receiver();
        try (SubmissionPublisher<Integer> publisher = new SubmissionPublisher<>()) {
            publisher.subscribe(receiver);
            publisher.submit(10);
            publisher.submit(20);
        }
        receiver.done.join();
    }
}

Result

item=10
item=20
complete

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources