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. 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().
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.
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.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);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv static class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv static final AsyncProcessor. | |
| pack-priv Throwable | error
Write before updating subscribers, read after reading subscribers as TERMINATED. |
| pack-priv final AtomicReference | |
| pack-priv static final AsyncProcessor. | |
| pack-priv T | value
Write before updating subscribers, read after reading subscribers as TERMINATED. |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | Returns: true if successful, false if the processor has terminatedthe subscriber to add psTries 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 | |
| public @Nullable Throwable | getThrowable()
Implements abstract io. |
| public T | Returns: a single value this processor currently has or null if no such value existsReturns a single value this processor currently has or null if no such value exists. |
| public boolean | hasComplete()
Implements abstract io. |
| public boolean | hasSubscribers()
Implements abstract io. |
| public boolean | hasThrowable()
Implements abstract io. |
| public boolean | Returns: true if this processor has any valueReturns true if this processor has any value. |
| public void | |
| public void | onError(@NonNull Throwable
the throwable signaled t)Implements org. |
| public void | onNext(T
the element signaled t)Implements org. Publisher in response to requests to Subscription#request(long).
|
| public void | onSubscribe(@NonNull Subscription
the s)Subscription that allows requesting data via Subscription#request(long)Implements org. Publisher#subscribe(Subscriber).
|
| pack-priv void | remove(AsyncProcessor.
the subscriber's subscription wrapper to remove psAtomically removes the given subscriber if it is subscribed to this processor. |
| protected void |
| EMPTY | back to summary |
|---|---|
pack-priv static final AsyncProcessor.
| |
| error | back to summary |
|---|---|
| pack-priv Throwable error Write before updating subscribers, read after reading subscribers as TERMINATED. | |
| subscribers | back to summary |
|---|---|
| pack-priv final AtomicReference<AsyncProcessor. | |
| TERMINATED | back to summary |
|---|---|
pack-priv static final AsyncProcessor.
| |
| value | back to summary |
|---|---|
| pack-priv T value Write before updating subscribers, read after reading subscribers as TERMINATED. | |
| AsyncProcessor | back to summary |
|---|---|
| pack-priv AsyncProcessor() Constructs an AsyncProcessor.
| |
| add | back to summary |
|---|---|
| pack-priv boolean add(AsyncProcessor. Tries to add the given subscriber to the subscribers array atomically or returns false if the processor has terminated.
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull AsyncProcessor Creates a new AsyncProcessor.
| |
| getThrowable | back to summary |
|---|---|
| public @Nullable Throwable getThrowable() Implements abstract io. Doc from io. Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet. The method is thread-safe. | |
| getValue | back 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.
| |
| hasComplete | back to summary |
|---|---|
| public boolean hasComplete() Implements abstract io. Doc from io. Returns true if the FlowableProcessor has reached a terminal state through a complete event. The method is thread-safe.
| |
| hasSubscribers | back to summary |
|---|---|
| public boolean hasSubscribers() Implements abstract io. Doc from io. Returns true if the FlowableProcessor has subscribers. The method is thread-safe.
| |
| hasThrowable | back to summary |
|---|---|
| public boolean hasThrowable() Implements abstract io. Doc from io. Returns true if the FlowableProcessor has reached a terminal state through an error event. The method is thread-safe.
| |
| hasValue | back to summary |
|---|---|
| public boolean hasValue() Returns true if this processor has any value. The method is thread-safe.
| |
| onComplete | back to summary |
|---|---|
| public void onComplete() Implements org. Doc from org. Successful terminal state.
No further events will be sent even if
| |
| onError | back to summary |
|---|---|
| public void onError(@NonNull Throwable t) Implements org. Doc from org. Failed terminal state.
No further events will be sent even if
| |
| onNext | back to summary |
|---|---|
| public void onNext(T t) Implements org. Doc from org. Data notification sent by the
| |
| onSubscribe | back to summary |
|---|---|
| public void onSubscribe(@NonNull Subscription s) Implements org. Doc from org. Invoked after calling
No data will start flowing until
It is the responsibility of this
The
| |
| remove | back to summary |
|---|---|
| pack-priv void remove(AsyncProcessor. Atomically removes the given subscriber if it is subscribed to this processor.
| |
| subscribeActual | back to summary |
|---|---|
protected void subscribeActual(@NonNull Subscriber<? super T> s)
| |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv final AsyncProcessor | |
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | cancel()
Overrides io. Implements org. Publisher to stop sending data and clean up resources.
|
| pack-priv void | |
| pack-priv void |
| parent | back to summary |
|---|---|
| pack-priv final AsyncProcessor<T> parent | |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides io. | |
| AsyncSubscription | back to summary |
|---|---|
| pack-priv AsyncSubscription(Subscriber<? super T> actual, AsyncProcessor<T> parent) | |
| cancel | back to summary |
|---|---|
| public void cancel() Overrides io. Implements org. Doc from org. Request the Data may still be sent to meet previously signalled demand after calling cancel.
| |
| onComplete | back to summary |
|---|---|
| pack-priv void onComplete() | |
| onError | back to summary |
|---|---|
| pack-priv void onError(Throwable t) | |