O'Reilly logo

Java 9 Cookbook by Nick Samoylov, Mohamed Sanaulla

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required