Top Description Inners Fields Constructors Methods
io.reactivex.processors

public final Class AsyncProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Type Parameters
<T>
the value type
Imports
java.util.Arrays, java.util.concurrent.atomic.AtomicReference, org.reactivestreams.*, io.reactivex.annotations.*, io.reactivex.internal.functions.ObjectHelper, io.reactivex.internal.subscriptions.DeferredScalarSubscription, io.reactivex.plugins.RxJavaPlugins

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 io.reactivex.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 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(), getValue(), getValues() or getValues(Object[]).

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 io.reactivex.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 io.reactivex.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 io.reactivex.plugins.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);

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv static final AsyncProcessor.AsyncSubscription<T>[]
pack-priv Throwable
error

Write before updating subscribers, read after reading subscribers as TERMINATED.

pack-priv final AtomicReference<AsyncProcessor.AsyncSubscription<T>[]>
pack-priv static final AsyncProcessor.AsyncSubscription<T>[]
pack-priv T
value

Write before updating subscribers, read after reading subscribers as TERMINATED.

Constructor Summary

AccessConstructor and Description
pack-priv
AsyncProcessor()

Constructs an AsyncProcessor.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if successful, false if the processor has terminated
add
(AsyncProcessor.AsyncSubscription<T>
the subscriber to add
ps
)

Tries to add the given subscriber to the subscribers array atomically or returns false if the processor has terminated.

public static <
the value type to be received and emitted
T
>
AsyncProcessor<T>

Returns:

the new AsyncProcessor instance
create
()

Creates a new AsyncProcessor.

public Throwable
getThrowable()

Implements abstract io.reactivex.processors.FlowableProcessor.getThrowable.

Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet.
public T

Returns:

a single value this processor currently has or null if no such value exists
getValue
()

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

public Object[]

Returns:

the array containing the snapshot of all values of this processor
getValues
()

Deprecated in 2.1.14; put the result of getValue() into an array manually, will be removed in 3.x
Returns an Object array containing snapshot all values of this processor.
public T[]

Returns:

the given array if the values fit into it or a new array containing all values
getValues
(T[]
the target array to copy values into if it fits
array
)

Deprecated in 2.1.14; put the result of getValue() into an array manually, will be removed in 3.x
Returns a typed array containing a snapshot of all values of this processor.
public boolean
hasComplete()

Implements abstract io.reactivex.processors.FlowableProcessor.hasComplete.

Returns true if the FlowableProcessor has reached a terminal state through a complete event.
public boolean
hasSubscribers()

Implements abstract io.reactivex.processors.FlowableProcessor.hasSubscribers.

Returns true if the FlowableProcessor has subscribers.
public boolean
hasThrowable()

Implements abstract io.reactivex.processors.FlowableProcessor.hasThrowable.

Returns true if the FlowableProcessor has reached a terminal state through an error event.
public boolean

Returns:

true if this processor has any value
hasValue
()

Returns true if this processor has any value.

public void
onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Successful terminal state.
public void
onError(Throwable
the throwable signaled
t
)

Implements org.reactivestreams.Subscriber.onError.

Failed terminal state.
public void
onNext(T
the element signaled
t
)

Implements org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).
public void
onSubscribe(Subscription
the Subscription that allows requesting data via Subscription#request(long)
s
)

Implements org.reactivestreams.Subscriber.onSubscribe, io.reactivex.FlowableSubscriber.onSubscribe.

Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling Subscription#request(long).
pack-priv void
remove(AsyncProcessor.AsyncSubscription<T>
the subscriber's subscription wrapper to remove
ps
)

Atomically removes the given subscriber if it is subscribed to this processor.

protected void
subscribeActual(Subscriber<? super T>
the incoming Subscriber, never null
s
)

Implements abstract io.reactivex.Flowable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.
Inherited from io.reactivex.processors.FlowableProcessor:
toSerialized

Field Detail

EMPTYback to summary
pack-priv static final AsyncProcessor.AsyncSubscription<T>[] EMPTY
Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error

Write before updating subscribers, read after reading subscribers as TERMINATED.

subscribersback to summary
pack-priv final AtomicReference<AsyncProcessor.AsyncSubscription<T>[]> subscribers
TERMINATEDback to summary
pack-priv static final AsyncProcessor.AsyncSubscription<T>[] TERMINATED
Annotations
@SuppressWarnings:rawtypes
valueback to summary
pack-priv T value

Write before updating subscribers, read after reading subscribers as TERMINATED.

Constructor Detail

AsyncProcessorback to summary
pack-priv AsyncProcessor()

Constructs an AsyncProcessor.

Annotations
@SuppressWarnings:unchecked
Since
2.0

Method Detail

addback to summary
pack-priv boolean add(AsyncProcessor.AsyncSubscription<T> ps)

Tries to add the given subscriber to the subscribers array atomically or returns false if the processor has terminated.

Parameters
ps:AsyncProcessor.AsyncSubscription<T>

the subscriber to add

Returns:boolean

true if successful, false if the processor has terminated

createback to summary
public static <T> AsyncProcessor<T> create()

Creates a new AsyncProcessor.

Parameters
<T>
the value type to be received and emitted
Returns:AsyncProcessor<T>

the new AsyncProcessor instance

Annotations
@CheckReturnValue
@NonNull
getThrowableback to summary
public Throwable getThrowable()

Implements abstract io.reactivex.processors.FlowableProcessor.getThrowable.

Doc from io.reactivex.processors.FlowableProcessor.getThrowable.

Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet.

The method is thread-safe.

Returns:Throwable

the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet

Annotations
@Override
@Nullable
getValueback to summary
public T getValue()

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

The method is thread-safe.

Returns:T

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

Annotations
@Nullable
getValuesback to summary
public Object[] getValues()

Deprecated

in 2.1.14; put the result of getValue() into an array manually, will be removed in 3.x

Returns an Object array containing snapshot all values of this processor.

The method is thread-safe.

Returns:Object[]

the array containing the snapshot of all values of this processor

Annotations
@Deprecated
getValuesback to summary
public T[] getValues(T[] array)

Deprecated

in 2.1.14; put the result of getValue() into an array manually, will be removed in 3.x

Returns a typed array containing a snapshot of all values of this processor.

The method follows the conventions of Collection.toArray by setting the array element after the last value to null (if the capacity permits).

The method is thread-safe.

Parameters
array:T[]

the target array to copy values into if it fits

Returns:T[]

the given array if the values fit into it or a new array containing all values

Annotations
@Deprecated
hasCompleteback to summary
public boolean hasComplete()

Implements abstract io.reactivex.processors.FlowableProcessor.hasComplete.

Doc from io.reactivex.processors.FlowableProcessor.hasComplete.

Returns true if the FlowableProcessor has reached a terminal state through a complete event.

The method is thread-safe.

Returns:boolean

true if the FlowableProcessor has reached a terminal state through a complete event

Annotations
@Override
hasSubscribersback to summary
public boolean hasSubscribers()

Implements abstract io.reactivex.processors.FlowableProcessor.hasSubscribers.

Doc from io.reactivex.processors.FlowableProcessor.hasSubscribers.

Returns true if the FlowableProcessor has subscribers.

The method is thread-safe.

Returns:boolean

true if the FlowableProcessor has subscribers

Annotations
@Override
hasThrowableback to summary
public boolean hasThrowable()

Implements abstract io.reactivex.processors.FlowableProcessor.hasThrowable.

Doc from io.reactivex.processors.FlowableProcessor.hasThrowable.

Returns true if the FlowableProcessor has reached a terminal state through an error event.

The method is thread-safe.

Returns:boolean

true if the FlowableProcessor has reached a terminal state through an error event

Annotations
@Override
hasValueback to summary
public boolean hasValue()

Returns true if this processor has any value.

The method is thread-safe.

Returns:boolean

true if this processor has any value

onCompleteback to summary
public void onComplete()

Implements org.reactivestreams.Subscriber.onComplete.

Doc from org.reactivestreams.Subscriber.onComplete.

Successful terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Annotations
@SuppressWarnings:unchecked
@Override
onErrorback to summary
public void onError(Throwable t)

Implements org.reactivestreams.Subscriber.onError.

Doc from org.reactivestreams.Subscriber.onError.

Failed terminal state.

No further events will be sent even if Subscription#request(long) is invoked again.

Parameters
t:Throwable

the throwable signaled

Annotations
@SuppressWarnings:unchecked
@Override
onNextback to summary
public void onNext(T t)

Implements org.reactivestreams.Subscriber.onNext.

Doc from org.reactivestreams.Subscriber.onNext.

Data notification sent by the Publisher in response to requests to Subscription#request(long).

Parameters
t:T

the element signaled

Annotations
@Override
onSubscribeback to summary
public void onSubscribe(Subscription s)

Implements org.reactivestreams.Subscriber.onSubscribe, io.reactivex.FlowableSubscriber.onSubscribe.

Doc from io.reactivex.FlowableSubscriber.onSubscribe.

Implementors of this method should make sure everything that needs to be visible in 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. Invoked after calling Publisher#subscribe(Subscriber).

No data will start flowing until Subscription#request(long) is invoked.

It is the responsibility of this Subscriber instance to call Subscription#request(long) whenever more data is wanted.

The Publisher will send notifications only in response to Subscription#request(long).

Parameters
s:Subscription

Doc from org.reactivestreams.Subscriber.onSubscribe.

the Subscription that allows requesting data via Subscription#request(long)

Annotations
@Override
removeback to summary
pack-priv void remove(AsyncProcessor.AsyncSubscription<T> ps)

Atomically removes the given subscriber if it is subscribed to this processor.

Parameters
ps:AsyncProcessor.AsyncSubscription<T>

the subscriber's subscription wrapper to remove

Annotations
@SuppressWarnings:unchecked
subscribeActualback to summary
protected void subscribeActual(Subscriber<? super T> s)

Implements abstract io.reactivex.Flowable.subscribeActual.

Doc from io.reactivex.Flowable.subscribeActual.

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 subscribe(Subscriber) before this method gets called.

Parameters
s:Subscriber<? super T>

the incoming Subscriber, never null

Annotations
@Override
io.reactivex.processors back to summary

pack-priv final Class AsyncProcessor.AsyncSubscription<T>

extends DeferredScalarSubscription<T>
Class Inheritance

Field Summary

Modifier and TypeField and Description
pack-priv final AsyncProcessor<T>
private static final long
Inherited from io.reactivex.internal.subscriptions.DeferredScalarSubscription:
downstreamvalue

Constructor Summary

AccessConstructor and Description
pack-priv
AsyncSubscription(Subscriber<? super T> actual, AsyncProcessor<T> parent)

Method Summary

Modifier and TypeMethod and Description
public void
cancel()

Overrides io.reactivex.internal.subscriptions.DeferredScalarSubscription.cancel.

Implements org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.
pack-priv void
pack-priv void
Inherited from io.reactivex.internal.subscriptions.DeferredScalarSubscription:
clearcompleteisCancelledisEmptypollrequestrequestFusiontryCancel

Field Detail

parentback to summary
pack-priv final AsyncProcessor<T> parent
serialVersionUIDback to summary
private static final long serialVersionUID

Hides io.reactivex.internal.subscriptions.DeferredScalarSubscription.serialVersionUID.

Constructor Detail

AsyncSubscriptionback to summary
pack-priv AsyncSubscription(Subscriber<? super T> actual, AsyncProcessor<T> parent)

Method Detail

cancelback to summary
public void cancel()

Overrides io.reactivex.internal.subscriptions.DeferredScalarSubscription.cancel.

Implements org.reactivestreams.Subscription.cancel.

Doc from org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.

Data may still be sent to meet previously signalled demand after calling cancel.

Annotations
@Override
onCompleteback to summary
pack-priv void onComplete()
onErrorback to summary
pack-priv void onError(Throwable t)