Class AsyncProcessor<T>

Type Parameters:
T - the value type
All Implemented Interfaces:
FlowableSubscriber<T>, org.reactivestreams.Processor<T,T>, org.reactivestreams.Publisher<T>, org.reactivestreams.Subscriber<T>

public final class AsyncProcessor<@NonNull T> extends FlowableProcessor<T>
Processor that emits the very last value followed by a completion event or the received error to Subscribers.

This processor does not have a public constructor by design; a new empty instance of this AsyncProcessor can be created via the create() method.

Since an AsyncProcessor is a Reactive Streams Processor type, nulls are not allowed (Rule 2.13) as parameters to onNext(Object) and onError(Throwable). Such calls will result in a NullPointerException being thrown and the processor's state is not changed.

AsyncProcessor is a Flowable as well as a FlowableProcessor and supports backpressure from the downstream but its Subscriber-side consumes items in an unbounded manner.

When this AsyncProcessor is terminated via onError(Throwable), the last observed item (if any) is cleared and late Subscribers only receive the onError event.

The AsyncProcessor caches the latest item internally and it emits this item only when onComplete is called. Therefore, it is not recommended to use this Processor with infinite or never-completing sources.

Even though AsyncProcessor implements the Subscriber interface, calling onSubscribe is not required (Rule 2.12) if the processor is used as a standalone source. However, calling onSubscribe after the AsyncProcessor reached its terminal state will result in the given Subscription being canceled immediately.

Calling onNext(Object), onError(Throwable) and onComplete() is required to be serialized (called from the same thread or called non-overlappingly from different threads through external means of serialization). The FlowableProcessor.toSerialized() method available to all FlowableProcessors provides such serialization and also protects against reentrance (i.e., when a downstream Subscriber consuming this processor also wants to call onNext(Object) on this processor recursively). The implementation of onXXX methods are technically thread-safe but non-serialized calls to them may lead to undefined state in the currently subscribed Subscribers.

This AsyncProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasSubscribers() as well as means to read the very last observed value - after this AsyncProcessor has been completed - in a non-blocking and thread-safe manner via hasValue() or getValue().

Backpressure:
The AsyncProcessor honors the backpressure of the downstream Subscribers and won't emit its single value to a particular Subscriber until that Subscriber has requested an item. When the AsyncProcessor is subscribed to a Flowable, the processor consumes this Flowable in an unbounded manner (requesting Long.MAX_VALUE) as only the very last upstream item is retained by it.
Scheduler:
AsyncProcessor does not operate by default on a particular Scheduler and the Subscribers get notified on the thread where the terminating onError or onComplete methods were invoked.
Error handling:
When the onError(Throwable) is called, the AsyncProcessor enters into a terminal state and emits the same Throwable instance to the last set of Subscribers. During this emission, if one or more Subscribers dispose their respective Subscriptions, the Throwable is delivered to the global error handler via RxJavaPlugins.onError(Throwable) (multiple times if multiple Subscribers cancel at once). If there were no Subscribers subscribed to this AsyncProcessor when the onError() was called, the global error handler is not invoked.

Example usage:


 AsyncProcessor<Object> processor = AsyncProcessor.create();
 
 TestSubscriber<Object> ts1 = processor.test();

 ts1.assertEmpty();

 processor.onNext(1);

 // AsyncProcessor only emits when onComplete was called.
 ts1.assertEmpty();

 processor.onNext(2);
 processor.onComplete();

 // onComplete triggers the emission of the last cached item and the onComplete event.
 ts1.assertResult(2);

 TestSubscriber<Object> ts2 = processor.test();

 // late Subscribers receive the last cached item too
 ts2.assertResult(2);
 
  • Field Details

  • Constructor Details

    • AsyncProcessor

      AsyncProcessor()
      Constructs an AsyncProcessor.
      Since:
      2.0
  • Method Details

    • create

      @CheckReturnValue @NonNull public static <T> @NonNull AsyncProcessor<T> create()
      Creates a new AsyncProcessor.
      Type Parameters:
      T - the value type to be received and emitted
      Returns:
      the new AsyncProcessor instance
    • onSubscribe

      public void onSubscribe(@NonNull @NonNull org.reactivestreams.Subscription s)
      Description copied from interface: FlowableSubscriber
      Implementors of this method should make sure everything that needs to be visible in Subscriber.onNext(Object) is established before calling Subscription.request(long). In practice this means no initialization should happen after the request() call and additional behavior is thread safe in respect to onNext.
    • onNext

      public void onNext(@NonNull @NonNull T t)
    • onError

      public void onError(@NonNull @NonNull Throwable t)
    • onComplete

      public void onComplete()
    • hasSubscribers

      @CheckReturnValue public boolean hasSubscribers()
      Description copied from class: FlowableProcessor
      Returns true if the FlowableProcessor has subscribers.

      The method is thread-safe.

      Specified by:
      hasSubscribers in class FlowableProcessor<T>
      Returns:
      true if the FlowableProcessor has subscribers
    • hasThrowable

      @CheckReturnValue public boolean hasThrowable()
      Description copied from class: FlowableProcessor
      Returns true if the FlowableProcessor has reached a terminal state through an error event.

      The method is thread-safe.

      Specified by:
      hasThrowable in class FlowableProcessor<T>
      Returns:
      true if the FlowableProcessor has reached a terminal state through an error event
      See Also:
    • hasComplete

      @CheckReturnValue public boolean hasComplete()
      Description copied from class: FlowableProcessor
      Returns true if the FlowableProcessor has reached a terminal state through a complete event.

      The method is thread-safe.

      Specified by:
      hasComplete in class FlowableProcessor<T>
      Returns:
      true if the FlowableProcessor has reached a terminal state through a complete event
      See Also:
    • getThrowable

      Description copied from class: FlowableProcessor
      Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet.

      The method is thread-safe.

      Specified by:
      getThrowable in class FlowableProcessor<T>
      Returns:
      the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet
    • subscribeActual

      protected void subscribeActual(@NonNull @NonNull org.reactivestreams.Subscriber<? super @NonNull T> s)
      Description copied from class: Flowable
      Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.

      There is no need to call any of the plugin hooks on the current Flowable instance or the Subscriber; all hooks and basic safeguards have been applied by Flowable.subscribe(Subscriber) before this method gets called.

      Specified by:
      subscribeActual in class Flowable<T>
      Parameters:
      s - the incoming Subscriber, never null
    • add

      Tries to add the given subscriber to the subscribers array atomically or returns false if the processor has terminated.
      Parameters:
      ps - the subscriber to add
      Returns:
      true if successful, false if the processor has terminated
    • remove

      Atomically removes the given subscriber if it is subscribed to this processor.
      Parameters:
      ps - the subscriber's subscription wrapper to remove
    • hasValue

      @CheckReturnValue public boolean hasValue()
      Returns true if this processor has any value.

      The method is thread-safe.

      Returns:
      true if this processor has any value
    • getValue

      @Nullable @CheckReturnValue public T getValue()
      Returns a single value this processor currently has or null if no such value exists.

      The method is thread-safe.

      Returns:
      a single value this processor currently has or null if no such value exists