What are the rules for the Subscription interface in Java 9?


A Subscription can be shared by exactly one Publisher and one Subscriber for the purpose of mediating data exchange. That is the reason subscribe() method doesn't return created Subscription, instead returns void. The Subscription is only passed to Subscriber through the onSubscribe() method callback. The Subscription interface contains two methods: request() and cancel().

Syntax

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

Rules for Subscription interface:

  • Subscription.request() and Subscription.cancel() methods must be called only inside of its Subscriber context.
  • Subscription must allow Subscriber to call the Subscription.request() method synchronously from within onNext() or onSubscribe() methods.
  • Subscription.request() method must place an upper bound on possible synchronous recursion between Publisher and Subscriber.
  • Subscription.request() method should respect the responsivity of its caller by returning in a timely manner.
  • Subscription.cancel() method must respect the responsivity of its caller by returning in a timely manner, and must be thread-safe.
  • While Subscription is not canceled, Subscription.request(long n) method must register a given number of additional elements to be produced to a respective subscriber.
  • While the Subscription is not canceled, Subscription.request(long n) method must call onError() method with an IllegalArgumentException if the argument is <= 0.
  • While the Subscription is not canceled, Subscription.request(long n) method may synchronously call onNext() method on this or other subscribers.
  • While the Subscription is not canceled, Subscription.request(long n) method may synchronously call onComplete() or onError() methods on this or other subscribers.
  • While the Subscription is not canceled, Subscription.cancel() method must request Publisher to stop signaling its Subscriber. The operation can't be required to affect the Subscription immediately.
  • While the Subscription is not canceled, Subscription.cancel() method must request Publisher to drop any references to the corresponding subscriber.
  • Calling Subscription.cancel() and Subscription.request () methods must return normally.
  • The Subscription must support an unbounded number of calls to request and support a demand up to 2^63-1. If the demand equal to or greater than 2^63-1 may be considered by the Publisher as effectively unbounded.

Updated on: 22-Apr-2020

220 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements