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

public final Class PublishProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Type Parameters
<T>
the value type multicasted to Subscribers.
Imports
java.util.concurrent.atomic.*, org.reactivestreams.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.exceptions.MissingBackpressureException, io.reactivex.rxjava3.internal.subscriptions.SubscriptionHelper, io.reactivex.rxjava3.internal.util.*, io.reactivex.rxjava3.plugins.RxJavaPlugins

Processor that multicasts all subsequently observed items to its current Subscribers.

This processor does not have a public constructor by design; a new empty instance of this PublishProcessor can be created via the create() method.

Since a PublishProcessor 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.

PublishProcessor is a io.reactivex.rxjava3.core.Flowable as well as a FlowableProcessor, however, it does not coordinate backpressure between different subscribers and between an upstream source and a subscriber. If an upstream item is received via onNext(Object), if a subscriber is not ready to receive an item, that subscriber is terminated via a MissingBackpressureException. To avoid this case, use offer(Object) and retry sometime later if it returned false. The PublishProcessor's Subscriber-side consumes items in an unbounded manner.

For a multicasting processor type that also coordinates between the downstream Subscribers and the upstream source as well, consider using MulticastProcessor.

When this PublishProcessor is terminated via onError(Throwable) or onComplete(), late Subscribers only receive the respective terminal event.

Unlike a BehaviorProcessor, a PublishProcessor doesn't retain/cache items, therefore, a new Subscriber won't receive any past items.

Even though PublishProcessor 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 PublishProcessor reached its terminal state will result in the given Subscription being canceled immediately.

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). Note that serializing over offer(Object) is not supported through toSerialized() because it is a method available on the PublishProcessor and BehaviorProcessor classes only.

This PublishProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasSubscribers().

Backpressure:
The processor does not coordinate backpressure for its subscribers and implements a weaker onSubscribe which calls requests Long#MAX_VALUE from the incoming Subscriptions. This makes it possible to subscribe the PublishProcessor to multiple sources (note on serialization though) unlike the standard Subscriber contract. Child subscribers, however, are not overflown but receive an IllegalStateException in case their requested amount is zero.
Scheduler:
PublishProcessor does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the Subscribers get notified on the thread the respective onXXX methods were invoked.
Error handling:
When the onError(Throwable) is called, the PublishProcessor 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 cancel 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 PublishProcessor when the onError() was called, the global error handler is not invoked.
Example usage:
 PublishProcessor<Object> processor = PublishProcessor.create();
 // subscriber1 will receive all onNext and onComplete events
 processor.subscribe(subscriber1);
 processor.onNext("one");
 processor.onNext("two");
 // subscriber2 will only receive "three" and onComplete
 processor.subscribe(subscriber2);
 processor.onNext("three");
 processor.onComplete();

  
See Also
MulticastProcessor

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
PublishProcessor.PublishSubscription<
the value type
T
>

Wraps the actual subscriber, tracks its requests and makes cancellation to remove itself from the current subscribers array.

Field Summary

Modifier and TypeField and Description
pack-priv static final PublishProcessor.PublishSubscription<T>[]
EMPTY

An empty subscribers array to avoid allocating it all the time.

pack-priv Throwable
error

The error, write before terminating and read after checking subscribers.

pack-priv final AtomicReference<PublishProcessor.PublishSubscription<T>[]>
subscribers

The array of currently subscribed subscribers.

pack-priv static final PublishProcessor.PublishSubscription<T>[]
TERMINATED

The terminated indicator for the subscribers array.

Constructor Summary

AccessConstructor and Description
pack-priv
PublishProcessor()

Constructs a PublishProcessor.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean

Returns:

true if successful, false if this processor has terminated
add
(PublishProcessor.PublishSubscription<T>
the subscriber to add
ps
)

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

public static <
the value type
T
>
@NonNull PublishProcessor<T>

Returns:

the new PublishProcessor
create
()

Constructs a PublishProcessor.

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 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 the item was emitted to all Subscribers
offer
(T
the item to emit, not null
t
)

Tries to emit the item to all currently subscribed Subscribers if all of them has requested some value, returns false otherwise.

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(PublishProcessor.PublishSubscription<T>
the subscription wrapping a subscriber 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 PublishProcessor.PublishSubscription<T>[] EMPTY

An empty subscribers array to avoid allocating it all the time.

Annotations
@SuppressWarnings:rawtypes
errorback to summary
pack-priv Throwable error

The error, write before terminating and read after checking subscribers.

subscribersback to summary
pack-priv final AtomicReference<PublishProcessor.PublishSubscription<T>[]> subscribers

The array of currently subscribed subscribers.

TERMINATEDback to summary
pack-priv static final PublishProcessor.PublishSubscription<T>[] TERMINATED

The terminated indicator for the subscribers array.

Annotations
@SuppressWarnings:rawtypes

Constructor Detail

PublishProcessorback to summary
pack-priv PublishProcessor()

Constructs a PublishProcessor.

Annotations
@SuppressWarnings:unchecked
Since
2.0

Method Detail

addback to summary
pack-priv boolean add(PublishProcessor.PublishSubscription<T> ps)

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

Parameters
ps:PublishProcessor.PublishSubscription<T>

the subscriber to add

Returns:boolean

true if successful, false if this processor has terminated

createback to summary
public static <T> @NonNull PublishProcessor<T> create()

Constructs a PublishProcessor.

Parameters
<T>
the value type
Returns:@NonNull PublishProcessor<T>

the new PublishProcessor

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
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 emit the item to all currently subscribed Subscribers if all of them has requested some value, returns false otherwise.

This method should be called in a sequential manner just like the onXXX methods of this PublishProcessor.

History: 2.0.8 - experimental

Parameters
t:T

the item to emit, not null

Returns:boolean

true if the item was emitted to all Subscribers

Annotations
@CheckReturnValue
Exceptions
NullPointerException:
if t is null
Since
2.2
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(PublishProcessor.PublishSubscription<T> ps)

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

Parameters
ps:PublishProcessor.PublishSubscription<T>

the subscription wrapping a subscriber to remove

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

pack-priv final Class PublishProcessor.PublishSubscription<T>

extends AtomicLong
implements Subscription
Class Inheritance
All Implemented Interfaces
org.reactivestreams.Subscription
Type Parameters
<T>
the value type

Wraps the actual subscriber, tracks its requests and makes cancellation to remove itself from the current subscribers array.

Field Summary

Modifier and TypeField and Description
pack-priv final Subscriber<? super T>
downstream

The actual subscriber.

pack-priv final PublishProcessor<T>
parent

The parent processor servicing this subscriber.

private static final long

Constructor Summary

AccessConstructor and Description
pack-priv
PublishSubscription(Subscriber<? super T>
the actual subscriber
actual
,
PublishProcessor<T>
the parent PublishProcessor
parent
)

Constructs a PublishSubscriber, wraps the actual subscriber and the state.

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.
public boolean
pack-priv boolean
public void
public void
public 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

The actual subscriber.

parentback to summary
pack-priv final PublishProcessor<T> parent

The parent processor servicing this subscriber.

serialVersionUIDback to summary
private static final long serialVersionUID

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

Constructor Detail

PublishSubscriptionback to summary
pack-priv PublishSubscription(Subscriber<? super T> actual, PublishProcessor<T> parent)

Constructs a PublishSubscriber, wraps the actual subscriber and the state.

Parameters
actual:Subscriber<? super T>

the actual subscriber

parent:PublishProcessor<T>

the parent PublishProcessor

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
isCancelledback to summary
public boolean isCancelled()
isFullback to summary
pack-priv boolean isFull()
onCompleteback to summary
public void onComplete()
onErrorback to summary
public void onError(Throwable t)
onNextback to summary
public 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