Top Description Inners Fields Constructors Methods
io.reactivex.rxjava3.processors

public final Class AsyncProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Type Parameters
<T>
the value type
Imports
java.util.concurrent.atomic.AtomicReference, org.reactivestreams.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.internal.subscriptions.DeferredScalarSubscription, io.reactivex.rxjava3.internal.util.ExceptionHelper, io.reactivex.rxjava3.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.rxjava3.core.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() 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 io.reactivex.rxjava3.core.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.rxjava3.core.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.rxjava3.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
>
@NonNull AsyncProcessor<T>

Returns:

the new AsyncProcessor instance
create
()

Creates a new AsyncProcessor.

public @Nullable Throwable
getThrowable()

Implements abstract io.reactivex.rxjava3.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 boolean
hasComplete()

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

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

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

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

Implements abstract io.reactivex.rxjava3.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(@NonNull 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
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
Inherited from io.reactivex.rxjava3.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> @NonNull AsyncProcessor<T> create()

Creates a new AsyncProcessor.

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

the new AsyncProcessor instance

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

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

Doc from io.reactivex.rxjava3.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:@Nullable Throwable

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

Annotations
@Override
@Nullable
@CheckReturnValue
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
@CheckReturnValue
hasCompleteback to summary
public boolean hasComplete()

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

Doc from io.reactivex.rxjava3.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
@CheckReturnValue
hasSubscribersback to summary
public boolean hasSubscribers()

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

Doc from io.reactivex.rxjava3.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
@CheckReturnValue
hasThrowableback to summary
public boolean hasThrowable()

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

Doc from io.reactivex.rxjava3.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
@CheckReturnValue
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

Annotations
@CheckReturnValue
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(@NonNull 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:@NonNull 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(@NonNull Subscription s)

Implements org.reactivestreams.Subscriber.onSubscribe.

Doc from org.reactivestreams.Subscriber.onSubscribe.

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:@NonNull Subscription

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(@NonNull Subscriber<? super T> s)
Annotations
@Override
io.reactivex.rxjava3.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.rxjava3.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
pack-priv void
pack-priv void
Inherited from io.reactivex.rxjava3.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.rxjava3.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.rxjava3.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)