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 9 Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.