How to do it...

To save some time and space, instead of creating our own implementation of the Flow.Publisher<T> interface, we can use the SubmissionPublisher<T> class from the java.util.concurrent package. But, we will create our own implementation of the Flow.Subscriber<T> interface:

class DemoSubscriber<T> implements Flow.Subscriber<T> {  private String name;  private Flow.Subscription subscription;  public DemoSubscriber(String name){ this.name = name; }  public void onSubscribe(Flow.Subscription subscription) {    this.subscription = subscription;    this.subscription.request(0);  }  public void onNext(T item) {    System.out.println(name + " received: " + item);    this.subscription.request(1);  }  public void onError(Throwable ex){ ex.printStackTrace() ...

Get Java 11 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.