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

public final Class MulticastProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Annotations
@BackpressureSupport:FULL
@SchedulerSupport:none
Type Parameters
<T>
the input and output value type
Imports
java.util.concurrent.atomic.*, org.reactivestreams.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.exceptions.*, io.reactivex.rxjava3.internal.functions.ObjectHelper, io.reactivex.rxjava3.internal.subscriptions.*, io.reactivex.rxjava3.internal.util.*, io.reactivex.rxjava3.operators.*, io.reactivex.rxjava3.plugins.RxJavaPlugins

A FlowableProcessor implementation that coordinates downstream requests through a front-buffer and stable-prefetching, optionally canceling the upstream if all subscribers have cancelled.

This processor does not have a public constructor by design; a new empty instance of this MulticastProcessor can be created via the following create methods that allow configuring it:

When the reference counting behavior is enabled, the MulticastProcessor cancels its upstream when all Subscribers have cancelled. Late Subscribers will then be immediately completed.

Because MulticastProcessor implements the Subscriber interface, calling onSubscribe is mandatory (Rule 2.12). If MulticastProcessor should run standalone, i.e., without subscribing the MulticastProcessor to another Publisher, use start() or startUnbounded() methods to initialize the internal buffer. Failing to do so will lead to a NullPointerException at runtime.

Use offer(Object) to try and offer/emit items but don't fail if the internal buffer is full.

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

Since a MulticastProcessor is a io.reactivex.rxjava3.core.Flowable, it supports backpressure. The backpressure from the currently subscribed Subscribers are coordinated by emitting upstream items only if all of those Subscribers have requested at least one item. This behavior is also called lockstep-mode because even if some Subscribers can take any number of items, other Subscribers requesting less or infrequently will slow down the overall throughput of the flow.

Calling onNext(Object), offer(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 MulticastProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasSubscribers(). This processor doesn't allow peeking into its buffer.

When this MulticastProcessor is terminated via onError(Throwable) or onComplete(), all previously signaled but not yet consumed items will be still available to Subscribers and the respective terminal even is only emitted when all previous items have been successfully delivered to Subscribers. If there are no Subscribers, the remaining items will be buffered indefinitely.

The MulticastProcessor does not support clearing its cached events (to appear empty again).

Backpressure:
The backpressure from the currently subscribed Subscribers are coordinated by emitting upstream items only if all of those Subscribers have requested at least one item. This behavior is also called lockstep-mode because even if some Subscribers can take any number of items, other Subscribers requesting less or infrequently will slow down the overall throughput of the flow.
Scheduler:
MulticastProcessor does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the Subscribers get notified on an arbitrary thread in a serialized fashion.

Example:


   MulticastProcessor<Integer> mp = Flowable.range(1, 10)
   .subscribeWith(MulticastProcessor.create());

   mp.test().assertResult(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

   // --------------------

   MulticastProcessor<Integer> mp2 = MulticastProcessor.create(4);
   mp2.start();

   assertTrue(mp2.offer(1));
   assertTrue(mp2.offer(2));
   assertTrue(mp2.offer(3));
   assertTrue(mp2.offer(4));

   assertFalse(mp2.offer(5));

   mp2.onComplete();

   mp2.test().assertResult(1, 2, 3, 4);

History: 2.1.14 - experimental

Since
2.2

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv final int
pack-priv int
pack-priv volatile boolean
pack-priv static final MulticastProcessor.MulticastSubscription<T>[]
pack-priv volatile Throwable
pack-priv int
pack-priv final int
pack-priv volatile SimpleQueue<T>
pack-priv final boolean
pack-priv final AtomicReference<MulticastProcessor.MulticastSubscription<T>[]>
pack-priv static final MulticastProcessor.MulticastSubscription<T>[]
pack-priv final AtomicReference<Subscription>
pack-priv final AtomicInteger

Constructor Summary

AccessConstructor and Description
pack-priv
MulticastProcessor(int
the prefetch amount
bufferSize
,
boolean
if true and if all Subscribers have canceled, the upstream is cancelled
refCount
)

Constructs a fresh instance with the given prefetch amount and the optional refCount-behavior.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
public static <
the input and output value type
T
>
@NonNull MulticastProcessor<T>

Returns:

the new MulticastProcessor instance
create
()

Constructs a fresh instance with the default Flowable.bufferSize() prefetch amount and no refCount-behavior.

public static <
the input and output value type
T
>
@NonNull MulticastProcessor<T>

Returns:

the new MulticastProcessor instance
create
(boolean
if true and if all Subscribers have canceled, the upstream is cancelled
refCount
)

Constructs a fresh instance with the default Flowable.bufferSize() prefetch amount and the optional refCount-behavior.

public static <
the input and output value type
T
>
@NonNull MulticastProcessor<T>

Returns:

the new MulticastProcessor instance
create
(int
the prefetch amount
bufferSize
)

Constructs a fresh instance with the given prefetch amount and no refCount behavior.

public static <
the input and output value type
T
>
@NonNull MulticastProcessor<T>

Returns:

the new MulticastProcessor instance
create
(int
the prefetch amount
bufferSize
,
boolean
if true and if all Subscribers have canceled, the upstream is cancelled
refCount
)

Constructs a fresh instance with the given prefetch amount and the optional refCount-behavior.

pack-priv void
public 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 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 successful, false if the queue is full
offer
(T
the item to offer, not null
t
)

Tries to offer an item into the internal queue and returns false if the queue is full.

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
public void
start()

Initializes this Processor by setting an upstream Subscription that ignores request amounts, uses a fixed buffer and allows using the onXXX and offer methods afterwards.

public void
startUnbounded()

Initializes this Processor by setting an upstream Subscription that ignores request amounts, uses an unbounded buffer and allows using the onXXX and offer methods afterwards.

protected void
Inherited from io.reactivex.rxjava3.processors.FlowableProcessor:
toSerialized

Field Detail

bufferSizeback to summary
pack-priv final int bufferSize
consumedback to summary
pack-priv int consumed
doneback to summary
pack-priv volatile boolean done
EMPTYback to summary
pack-priv static final MulticastProcessor.MulticastSubscription<T>[] EMPTY
Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv volatile Throwable error
fusionModeback to summary
pack-priv int fusionMode
limitback to summary
pack-priv final int limit
queueback to summary
pack-priv volatile SimpleQueue<T> queue
refcountback to summary
pack-priv final boolean refcount
subscribersback to summary
pack-priv final AtomicReference<MulticastProcessor.MulticastSubscription<T>[]> subscribers
TERMINATEDback to summary
pack-priv static final MulticastProcessor.MulticastSubscription<T>[] TERMINATED
Annotations
@SuppressWarnings:rawtypes
upstreamback to summary
pack-priv final AtomicReference<Subscription> upstream
wipback to summary
pack-priv final AtomicInteger wip

Constructor Detail

MulticastProcessorback to summary
pack-priv MulticastProcessor(int bufferSize, boolean refCount)

Constructs a fresh instance with the given prefetch amount and the optional refCount-behavior.

Parameters
bufferSize:int

the prefetch amount

refCount:boolean

if true and if all Subscribers have canceled, the upstream is cancelled

Annotations
@SuppressWarnings:unchecked

Method Detail

addback to summary
pack-priv boolean add(MulticastProcessor.MulticastSubscription<T> inner)
createback to summary
public static <T> @NonNull MulticastProcessor<T> create()

Constructs a fresh instance with the default Flowable.bufferSize() prefetch amount and no refCount-behavior.

Parameters
<T>
the input and output value type
Returns:@NonNull MulticastProcessor<T>

the new MulticastProcessor instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> @NonNull MulticastProcessor<T> create(boolean refCount)

Constructs a fresh instance with the default Flowable.bufferSize() prefetch amount and the optional refCount-behavior.

Parameters
<T>
the input and output value type
refCount:boolean

if true and if all Subscribers have canceled, the upstream is cancelled

Returns:@NonNull MulticastProcessor<T>

the new MulticastProcessor instance

Annotations
@CheckReturnValue
@NonNull
createback to summary
public static <T> @NonNull MulticastProcessor<T> create(int bufferSize)

Constructs a fresh instance with the given prefetch amount and no refCount behavior.

Parameters
<T>
the input and output value type
bufferSize:int

the prefetch amount

Returns:@NonNull MulticastProcessor<T>

the new MulticastProcessor instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
IllegalArgumentException:
if bufferSize is non-positive
createback to summary
public static <T> @NonNull MulticastProcessor<T> create(int bufferSize, boolean refCount)

Constructs a fresh instance with the given prefetch amount and the optional refCount-behavior.

Parameters
<T>
the input and output value type
bufferSize:int

the prefetch amount

refCount:boolean

if true and if all Subscribers have canceled, the upstream is cancelled

Returns:@NonNull MulticastProcessor<T>

the new MulticastProcessor instance

Annotations
@CheckReturnValue
@NonNull
Exceptions
IllegalArgumentException:
if bufferSize is non-positive
drainback to summary
pack-priv void drain()
Annotations
@SuppressWarnings:unchecked
getThrowableback to summary
public 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:Throwable

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

Annotations
@Override
@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
offerback to summary
public boolean offer(T t)

Tries to offer an item into the internal queue and returns false if the queue is full.

Parameters
t:T

the item to offer, not null

Returns:boolean

true if successful, false if the queue is full

Annotations
@CheckReturnValue
Exceptions
NullPointerException:
if t is null
IllegalStateException:
if the processor is in fusion mode
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
@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
@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(MulticastProcessor.MulticastSubscription<T> inner)
Annotations
@SuppressWarnings:unchecked
startback to summary
public void start()

Initializes this Processor by setting an upstream Subscription that ignores request amounts, uses a fixed buffer and allows using the onXXX and offer methods afterwards.

startUnboundedback to summary
public void startUnbounded()

Initializes this Processor by setting an upstream Subscription that ignores request amounts, uses an unbounded buffer and allows using the onXXX and offer methods afterwards.

subscribeActualback to summary
protected void subscribeActual(@NonNull Subscriber<? super T> s)
Annotations
@Override
io.reactivex.rxjava3.processors back to summary

pack-priv final Class MulticastProcessor.MulticastSubscription<T>

extends AtomicLong
implements Subscription
Class Inheritance
All Implemented Interfaces
org.reactivestreams.Subscription

Field Summary

Modifier and TypeField and Description
pack-priv final Subscriber<? super T>
pack-priv long
pack-priv final MulticastProcessor<T>
private static final long

Constructor Summary

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

Method Summary

Modifier and TypeMethod and Description
public void
cancel()

Implements org.reactivestreams.Subscription.cancel.

Request the Publisher to stop sending data and clean up resources.
pack-priv void
pack-priv void
pack-priv void
onNext(T t)

public void
request(long
the strictly positive number of elements to requests to the upstream Publisher
n
)

Implements org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.
Inherited from java.util.concurrent.atomic.AtomicLong:
accumulateAndGetaddAndGetcompareAndExchangecompareAndExchangeAcquirecompareAndExchangeReleasecompareAndSetdecrementAndGetdoubleValuefloatValuegetgetAcquiregetAndAccumulategetAndAddgetAndDecrementgetAndIncrementgetAndSetgetAndUpdategetOpaquegetPlainincrementAndGetintValuelazySetlongValuesetsetOpaquesetPlainsetReleasetoStringupdateAndGetweakCompareAndSetweakCompareAndSetAcquireweakCompareAndSetPlainweakCompareAndSetReleaseweakCompareAndSetVolatile

Field Detail

downstreamback to summary
pack-priv final Subscriber<? super T> downstream
emittedback to summary
pack-priv long emitted
parentback to summary
pack-priv final MulticastProcessor<T> parent
serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.util.concurrent.atomic.AtomicLong.serialVersionUID.

Constructor Detail

MulticastSubscriptionback to summary
pack-priv MulticastSubscription(Subscriber<? super T> actual, MulticastProcessor<T> parent)

Method Detail

cancelback to summary
public void 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)
onNextback to summary
pack-priv void onNext(T t)
requestback to summary
public void request(long n)

Implements org.reactivestreams.Subscription.request.

Doc from org.reactivestreams.Subscription.request.

No events will be sent by a Publisher until demand is signaled via this method.

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 Publisher as "effectively unbounded".

Whatever has been requested can be sent by the Publisher so only signal demand for what can be safely handled.

A Publisher can send less than is requested if the stream ends but then must emit either Subscriber#onError(Throwable) or Subscriber#onComplete().

Parameters
n:long

the strictly positive number of elements to requests to the upstream Publisher

Annotations
@Override