FlowableProcessor variant that queues up events until a single Subscriber subscribes to it, replays
those events to it until the Subscriber catches up and then switches to relaying events live to
this single Subscriber until this UnicastProcessor terminates or the Subscriber cancels
its subscription.
This processor does not have a public constructor by design; a new empty instance of this
UnicastProcessor can be created via the following create methods that
allow specifying the retention policy for items:
create() - creates an empty, unbounded UnicastProcessor that
caches all items and the terminal event it receives.create(int) - creates an empty, unbounded UnicastProcessor
with a hint about how many total items one expects to retain.create(boolean) - creates an empty, unbounded UnicastProcessor that
optionally delays an error it receives and replays it after the regular items have been emitted.create(int, Runnable) - creates an empty, unbounded UnicastProcessor
with a hint about how many total items one expects to retain and a callback that will be
called exactly once when the UnicastProcessor gets terminated or the single Subscriber cancels.create(int, Runnable, boolean) - creates an empty, unbounded UnicastProcessor
with a hint about how many total items one expects to retain and a callback that will be
called exactly once when the UnicastProcessor gets terminated or the single Subscriber cancels
and optionally delays an error it receives and replays it after the regular items have been emitted.
If more than one Subscriber attempts to subscribe to this Processor, they
will receive an IllegalStateException if this UnicastProcessor hasn't terminated yet,
or the Subscribers receive the terminal event (error or completion) if this
Processor has terminated.
The UnicastProcessor buffers notifications and replays them to the single Subscriber as requested,
for which it holds upstream items an unbounded internal buffer until they can be emitted.
Since a UnicastProcessor is a Reactive Streams Processor,
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.
Since a UnicastProcessor is a io. as well as a FlowableProcessor, it
honors the downstream backpressure but consumes an upstream source in an unbounded manner (requesting Long#MAX_VALUE).
When this UnicastProcessor is terminated via onError(Throwable) the current or late single Subscriber
may receive the Throwable before any available items could be emitted. To make sure an onError event is delivered
to the Subscriber after the normal items, create a UnicastProcessor with the create(boolean) or
create(int, Runnable, boolean) factory methods.
Even though UnicastProcessor 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 UnicastProcessor 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).
This UnicastProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(),
getThrowable() and hasSubscribers().
UnicastProcessor honors the downstream backpressure but consumes an upstream source
(if any) in an unbounded manner (requesting Long#MAX_VALUE).UnicastProcessor does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and
the single Subscriber gets notified on the thread the respective onXXX methods were invoked.onError(Throwable) is called, the UnicastProcessor enters into a terminal state
and emits the same Throwable instance to the current single Subscriber. During this emission,
if the single Subscribers cancels its respective Subscriptions, the
Throwable is delivered to the global error handler via
io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) .
If there were no Subscribers subscribed to this UnicastProcessor when the onError()
was called, the global error handler is not invoked.
Example usage:
UnicastProcessor<Integer> processor = UnicastProcessor.create();
TestSubscriber<Integer> ts1 = processor.test();
// fresh UnicastProcessors are empty
ts1.assertEmpty();
TestSubscriber<Integer> ts2 = processor.test();
// A UnicastProcessor only allows one Subscriber during its lifetime
ts2.assertFailure(IllegalStateException.class);
processor.onNext(1);
ts1.assertValue(1);
processor.onNext(2);
ts1.assertValues(1, 2);
processor.onComplete();
ts1.assertResult(1, 2);
// ----------------------------------------------------
UnicastProcessor<Integer> processor2 = UnicastProcessor.create();
// a UnicastProcessor caches events until its single Subscriber subscribes
processor2.onNext(1);
processor2.onNext(2);
processor2.onComplete();
TestSubscriber<Integer> ts3 = processor2.test();
// the cached events are emitted in order
ts3.assertResult(1, 2);
| Modifier and Type | Class and Description |
|---|---|
| pack-priv class |
| Modifier and Type | Field and Description |
|---|---|
| pack-priv volatile boolean | |
| pack-priv final boolean | |
| pack-priv volatile boolean | |
| pack-priv final AtomicReference | |
| pack-priv boolean | |
| pack-priv Throwable | |
| pack-priv final AtomicBoolean | |
| pack-priv final AtomicReference | |
| pack-priv final SpscLinkedArrayQueue | |
| pack-priv final AtomicLong | |
| pack-priv final BasicIntQueueSubscription |
| Access | Constructor and Description |
|---|---|
| pack-priv | UnicastProcessor(int
the capacity hint for the internal, unbounded queue capacityHint, Runnable the callback to run when the Processor is terminated or cancelled, null not allowed onTerminate, boolean deliver pending onNext events before onError delayError)Creates an UnicastProcessor with the given capacity hint and callback for when the Processor is terminated normally or its single Subscriber cancels. |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv boolean | checkTerminated(boolean failFast, boolean d, boolean empty, Subscriber<? super T> a, SpscLinkedArrayQueue<T> q)
|
| public static < the value type T> @NonNull UnicastProcessor | Returns: an UnicastSubject instanceCreates an UnicastSubject with an internal buffer capacity hint 16. |
| public static < the value type T> @NonNull UnicastProcessor | Returns: an UnicastProcessor instancethe hint to size the internal unbounded buffer capacityHint)Creates an UnicastProcessor with the given internal buffer capacity hint. |
| public static < the value type T> @NonNull UnicastProcessor | Returns: an UnicastProcessor instancedeliver pending onNext events before onError delayError)Creates an UnicastProcessor with default internal buffer capacity hint and delay error flag. |
| public static < the value type T> @NonNull UnicastProcessor | Returns: an UnicastProcessor instancethe hint to size the internal unbounded buffer capacityHint, @NonNull Runnable the non null callback onTerminate)Creates an UnicastProcessor with the given internal buffer capacity hint and a callback for the case when the single Subscriber cancels its subscription or the processor is terminated. |
| public static < the value type T> @NonNull UnicastProcessor | Returns: an UnicastProcessor instancethe hint to size the internal unbounded buffer capacityHint, @NonNull Runnable the non null callback onTerminate, boolean deliver pending onNext events before onError delayError)Creates an UnicastProcessor with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Subscriber cancels its subscription or the processor is terminated. |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| pack-priv void | |
| public @Nullable Throwable | getThrowable()
Implements abstract io. |
| public boolean | hasComplete()
Implements abstract io. |
| public boolean | hasSubscribers()
Implements abstract io. |
| public boolean | hasThrowable()
Implements abstract io. |
| public void | |
| public void | onError(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(Subscription
the s)Subscription that allows requesting data via Subscription#request(long)Implements org. Publisher#subscribe(Subscriber).
|
| protected void |
| cancelled | back to summary |
|---|---|
| pack-priv volatile boolean cancelled | |
| delayError | back to summary |
|---|---|
| pack-priv final boolean delayError | |
| done | back to summary |
|---|---|
| pack-priv volatile boolean done | |
| downstream | back to summary |
|---|---|
| pack-priv final AtomicReference<Subscriber<? super T>> downstream | |
| enableOperatorFusion | back to summary |
|---|---|
| pack-priv boolean enableOperatorFusion | |
| error | back to summary |
|---|---|
| pack-priv Throwable error | |
| once | back to summary |
|---|---|
| pack-priv final AtomicBoolean once | |
| onTerminate | back to summary |
|---|---|
| pack-priv final AtomicReference<Runnable> onTerminate | |
| queue | back to summary |
|---|---|
| pack-priv final SpscLinkedArrayQueue<T> queue | |
| requested | back to summary |
|---|---|
| pack-priv final AtomicLong requested | |
| wip | back to summary |
|---|---|
| pack-priv final BasicIntQueueSubscription<T> wip | |
| UnicastProcessor | back to summary |
|---|---|
| pack-priv UnicastProcessor(int capacityHint, Runnable onTerminate, boolean delayError) Creates an UnicastProcessor with the given capacity hint and callback for when the Processor is terminated normally or its single Subscriber cancels. History: 2.0.8 - experimental
| |
| checkTerminated | back to summary |
|---|---|
| pack-priv boolean checkTerminated(boolean failFast, boolean d, boolean empty, Subscriber<? super T> a, SpscLinkedArrayQueue<T> q) | |
| create | back to summary |
|---|---|
| public static <T> @NonNull UnicastProcessor Creates an UnicastSubject with an internal buffer capacity hint 16.
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull UnicastProcessor Creates an UnicastProcessor with the given internal buffer capacity hint.
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull UnicastProcessor Creates an UnicastProcessor with default internal buffer capacity hint and delay error flag. History: 2.0.8 - experimental
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull UnicastProcessor Creates an UnicastProcessor with the given internal buffer capacity hint and a callback for the case when the single Subscriber cancels its subscription or the processor is terminated. The callback, if not null, is called exactly once and non-overlapped with any active replay.
| |
| create | back to summary |
|---|---|
| public static <T> @NonNull UnicastProcessor Creates an UnicastProcessor with the given internal buffer capacity hint, delay error flag and a callback for the case when the single Subscriber cancels its subscription or the processor is terminated. The callback, if not null, is called exactly once and non-overlapped with any active replay. History: 2.0.8 - experimental
| |
| doTerminate | back to summary |
|---|---|
| pack-priv void doTerminate() | |
| drain | back to summary |
|---|---|
| pack-priv void drain() | |
| drainFused | back to summary |
|---|---|
| pack-priv void drainFused(Subscriber<? super T> a) | |
| drainRegular | back to summary |
|---|---|
| pack-priv void drainRegular(Subscriber<? super T> a) | |
| 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. | |
| 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.
| |
| 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(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(Subscription s) Implements org. Doc from org. Invoked after calling
No data will start flowing until
It is the responsibility of this
The
| |
| subscribeActual | back to summary |
|---|---|
protected void subscribeActual(Subscriber<? super T> s)
| |
| Modifier and Type | Field and Description |
|---|---|
| private static final long |
| Access | Constructor and Description |
|---|---|
| pack-priv |
| Modifier and Type | Method and Description |
|---|---|
| public void | cancel()
Implements org. Publisher to stop sending data and clean up resources.
|
| public void | clear()
Implements io. |
| public boolean | isEmpty()
Implements io. |
| public T | poll()
Implements io. |
| public void | request(long
the strictly positive number of elements to requests to the upstream n)PublisherImplements org. Publisher until demand is signaled via this method.
|
| public int | requestFusion(int requestedMode)
Implements io. |
| serialVersionUID | back to summary |
|---|---|
| private static final long serialVersionUID Hides io. | |
| UnicastQueueSubscription | back to summary |
|---|---|
| pack-priv UnicastQueueSubscription() | |
| cancel | back to summary |
|---|---|
| public void cancel() Implements org. Doc from org. Request the Data may still be sent to meet previously signalled demand after calling cancel.
| |
| clear | back to summary |
|---|---|
| public void clear() Implements io. Doc from io. Removes all enqueued items from this queue.
| |
| isEmpty | back to summary |
|---|---|
| public boolean isEmpty() Implements io. Doc from io. Returns true if the queue is empty.
Note however that due to potential fused functions in
| |
| poll | back to summary |
|---|---|
| public T poll() Implements io. Doc from io. Tries to dequeue a value (non-null) or returns null if the queue is empty.
If the producer uses | |
| request | back to summary |
|---|---|
| public void request(long n) Implements org. Doc from org. No events will be sent by a
It can be called however often and whenever needed—but if the outstanding cumulative demand ever becomes Long.MAX_VALUE or more,
it may be treated by the
Whatever has been requested can be sent by the
A | |
| requestFusion | back to summary |
|---|---|
| public int requestFusion(int requestedMode) Implements io. Doc from io. Request a fusion mode from the upstream.
This should be called before
Calling this method multiple times or after
| |