Communication Models
- Synchronous + Blocking – Calling Customer Service and waiting for response online
- Asynchronous- Asking My Friend to Call Customer Service and I carry forward with my work
- Non-Blocking- Asking Call back from Customer Service and I carry forward with my work
- Asynchronous + Non Blocking – I am Calling customer Service and asking to call back My Friend and I carry forward with my work
request -> response
request -> streaming response (Stock Price in Stock Market App, Heart Beat for Health Check in Spring Boot Appp)
streaming request -> response (Using Google Docs and updating in drive in regular time intervals)
streaming request -> streaming response (Playing Game Online)
Reactive Stream Specification
Process Stream of Messages in a Non Blocking Asynchronous manner with Observer Design Pattern(Observe and React incase of change)
- Publisher: Emits a sequence of elements to its subscribers.
void subscribe(Subscriber<? super T> s)
- Subscriber: Consumes elements provided by a Publisher.
void onSubscribe(Subscription s) void onNext(T t) void onError(Throwable t) void onComplete()
- Subscription: Represents a one-to-one lifecycle of a Subscriber subscribing to a Publisher.
void request(long n) void cancel()
- Processor: Represents a processing stage, which is both a Subscriber and a Publisher.Inherits both Subscriber and Publisher interfaces.
There would be one Publisher at Top, similar to root of tree and there would be 0 to N intermediate processors(subscriber + publisher) and there would be leaf Subscriber
Publisher, Subscriber and Subscription
public interface Publisher<T> { public void subscribe(Subscriber<? super T> s); }
public interface Subscription { public void request(long n); public void cancel(); }
public interface Subscriber<T> { public void onSubscribe(Subscription s); public void onNext(T t); public void onError(Throwable t); public void onComplete(); }
- Publisher will have subscribe method through which we would pass the subscriber instance. The Subscription object would be returned by Publisher
- The Publisher hands over subscription object to Subscriber. Subscriber uses onSubscribe method to accept subscription.
- Subscriber could use subscription object using request method and could cancel subscription. Communication between Publisher and Subscriber happens using subscription object
- Subscriber can request N items using subscription object. The Publisher can iterate to N object using onNext method. Publisher only give 3 items if 3 items is requested by subscriber.
- If the Publisher has completed transferring all Items, then Publisher can call onComplete() method in Subscriber to notify the subscriber that its work is done
- Publisher calls onError() method to notify error.