How it works...

The subscriber is Observer<T> in the Reactive Stream model and its main objective is to listen to and observe the subject of the model which is either Mono<T> or Flux<T>. The full creation of a subscriber happens when org.reactiveStreams.Subscriber is used to override the four callback methods namely onSubscribe(), onNext(), onComplete(), and onError(). These methods can be enabled only if the publishers' counterparts of these are not invoked.

The onSubscribe() callback method manages the subscription mechanism of the subscriber by configuring the emission rate of Stream objects. This method is responsible for issuing a subscription to publishers and also managing the requests of data emission per delay per period. The Subscription<T> interface is part of this method which contains the following blueprint:

public interface Subscription { 
  public void request(long n); 
  public void cancel(); 
} 

To render each Stream object either OutputStream or Collection, onNext() is executed. Different implementations can be executed within this method as long as all requirements fit within the scope of an anonymous inner class.

When the subscriber receives all the needed Stream objects from the publisher without a problem, it triggers its onComplete() method. Since this method is the last one to be called, all the necessary finishing touches such as data transformation, database persistence, or buffered writing are included in this method. But in case Throwable is encountered along the way, subscriber will trigger its onError() function which becomes the last execution instead.

The other way of generating subscribers is to implement Java 1.8's Consumer<T> functional interface. Using Lambda expression, we can construct an implementation that can expose all the data Stream objects without the limitation of an anonymous inner class.

And since Subscriber<T> supports Lambda expressions, method references can also be accepted as one way of generating subscribers that will log all Stream objects in a console or add them in a data repository.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.103.210