Top Description Inners Fields Constructors Methods
io.reactivex.processors

public final Class ReplayProcessor<T>

extends FlowableProcessor<T>
Class Inheritance
Type Parameters
<T>
the value type
Imports
java.lang.reflect.Array, java.util.*, java.util.concurrent.TimeUnit, java.util.concurrent.atomic.*, org.reactivestreams.*, io.reactivex.Scheduler, io.reactivex.annotations.*, io.reactivex.internal.functions.ObjectHelper, io.reactivex.internal.subscriptions.SubscriptionHelper, io.reactivex.internal.util.*, io.reactivex.plugins.RxJavaPlugins

Replays events to Subscribers.

The ReplayProcessor supports the following item retainment strategies:

The ReplayProcessor can be created in bounded and unbounded mode. It can be bounded by size (maximum number of elements retained at most) and/or time (maximum age of elements replayed).

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

This ReplayProcessor respects the individual backpressure behavior of its Subscribers but does not coordinate their request amounts towards the upstream (because there might not be any) and consumes the upstream in an unbounded manner (requesting Long.MAX_VALUE). Note that Subscribers receive a continuous sequence of values after they subscribed even if an individual item gets delayed due to backpressure. Due to concurrency requirements, a size-bounded ReplayProcessor may hold strong references to more source emissions than specified.

When this ReplayProcessor is terminated via onError(Throwable) or onComplete(), late Subscribers will receive the retained/cached items first (if any) followed by the respective terminal event. If the ReplayProcessor has a time-bound, the age of the retained/cached items are still considered when replaying and thus it may result in no items being emitted before the terminal event.

Once an Subscriber has subscribed, it will receive items continuously from that point on. Bounds only affect how many past items a new Subscriber will receive before it catches up with the live event feed.

Even though ReplayProcessor 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 ReplayProcessor 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 ReplayProcessor supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasSubscribers() as well as means to read the retained/cached items in a non-blocking and thread-safe manner via hasValue(), getValue(), getValues() or getValues(Object[]).

Note that due to concurrency requirements, a size- and time-bounded ReplayProcessor may hold strong references to more source emissions than specified while it isn't terminated yet. Use the cleanupBuffer() to allow such inaccessible items to be cleaned up by GC once no consumer references them anymore.

Backpressure:
This ReplayProcessor respects the individual backpressure behavior of its Subscribers but does not coordinate their request amounts towards the upstream (because there might not be any) and consumes the upstream in an unbounded manner (requesting Long.MAX_VALUE). Note that Subscribers receive a continuous sequence of values after they subscribed even if an individual item gets delayed due to backpressure.
Scheduler:
ReplayProcessor does not operate by default on a particular io.reactivex.Scheduler and the Subscribers get notified on the thread the respective onXXX methods were invoked. Time-bound ReplayProcessors use the given Scheduler in their create methods as time source to timestamp of items received for the age checks.
Error handling:
When the onError(Throwable) is called, the ReplayProcessor 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.plugins.RxJavaPlugins#onError(Throwable) (multiple times if multiple Subscribers cancel at once). If there were no Subscribers subscribed to this ReplayProcessor when the onError() was called, the global error handler is not invoked.

Example usage:

 ReplayProcessor<Object> processor = new ReplayProcessor<T>();
 processor.onNext("one");
 processor.onNext("two");
 processor.onNext("three");
 processor.onComplete();

 // both of the following will get the onNext/onComplete calls from above
 processor.subscribe(subscriber1);
 processor.subscribe(subscriber2);

  

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static class
pack-priv static interface
ReplayProcessor.ReplayBuffer<
the value type
T
>

Abstraction over a buffer that receives events and replays them to individual Subscribers.

pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class
pack-priv static class

Field Summary

Modifier and TypeField and Description
pack-priv final ReplayProcessor.ReplayBuffer<T>
pack-priv boolean
pack-priv static final ReplayProcessor.ReplaySubscription<T>[]
private static final Object[]
EMPTY_ARRAY

An empty array to avoid allocation in getValues().

pack-priv final AtomicReference<ReplayProcessor.ReplaySubscription<T>[]>
pack-priv static final ReplayProcessor.ReplaySubscription<T>[]

Constructor Summary

AccessConstructor and Description
pack-priv
ReplayProcessor(ReplayProcessor.ReplayBuffer<T>
the ReplayBuffer instance, not null (not verified)
buffer
)

Constructs a ReplayProcessor with the given custom ReplayBuffer instance.

Method Summary

Modifier and TypeMethod and Description
pack-priv boolean
public void
cleanupBuffer()

Makes sure the item cached by the head node in a bounded ReplayProcessor is released (as it is never part of a replay).

public static <
the type of items observed and emitted by the ReplayProcessor
T
>
ReplayProcessor<T>

Returns:

the created ReplayProcessor
create
()

Creates an unbounded ReplayProcessor.

public static <
the type of items observed and emitted by this type of processor
T
>
ReplayProcessor<T>

Returns:

the created processor
create
(int
the initial buffer capacity
capacityHint
)

Creates an unbounded ReplayProcessor with the specified initial buffer capacity.

pack-priv static <
the type of items observed and emitted by this type of processor
T
>
ReplayProcessor<T>

Returns:

the created processor
createUnbounded
()

Creates an unbounded ReplayProcessor with the bounded-implementation for testing purposes.

public static <
the type of items observed and emitted by this type of processor
T
>
ReplayProcessor<T>

Returns:

the created processor
createWithSize
(int
the maximum number of buffered items
maxSize
)

Creates a size-bounded ReplayProcessor.

public static <
the type of items observed and emitted by this type of processor
T
>
ReplayProcessor<T>

Returns:

the created processor
createWithTime
(long
the maximum age of the contained items
maxAge
,
TimeUnit
the time unit of time
unit
,
Scheduler
the Scheduler that provides the current time
scheduler
)

Creates a time-bounded ReplayProcessor.

public static <
the type of items observed and emitted by this type of processor
T
>
ReplayProcessor<T>

Returns:

the created processor
createWithTimeAndSize
(long
the maximum age of the contained items
maxAge
,
TimeUnit
the time unit of time
unit
,
Scheduler
the Scheduler that provides the current time
scheduler
,
int
the maximum number of buffered items
maxSize
)

Creates a time- and size-bounded ReplayProcessor.

public Throwable
getThrowable()

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

Returns the error that caused the FlowableProcessor to terminate or null if the FlowableProcessor hasn't terminated yet.
public T

Returns:

the latest value this processor currently has or null if no such value exists
getValue
()

Returns the latest value this processor has or null if no such value exists.

public Object[]

Returns:

the array containing the snapshot of all values of this processor
getValues
()

Returns an Object array containing snapshot all values of this processor.

public T[]

Returns:

the given array if the values fit into it or a new array containing all values
getValues
(T[]
the target array to copy values into if it fits
array
)

Returns a typed array containing a snapshot of all values of this processor.

public boolean
hasComplete()

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

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

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

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

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

Returns true if the FlowableProcessor has reached a terminal state through an error event.
public boolean

Returns:

true if the 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(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
onSubscribe(Subscription
the Subscription that allows requesting data via Subscription#request(long)
s
)

Implements org.reactivestreams.Subscriber.onSubscribe, io.reactivex.FlowableSubscriber.onSubscribe.

Implementors of this method should make sure everything that needs to be visible in onNext(Object) is established before calling Subscription#request(long).
pack-priv void
pack-priv int
size()

protected void
subscribeActual(Subscriber<? super T>
the incoming Subscriber, never null
s
)

Implements abstract io.reactivex.Flowable.subscribeActual.

Operator implementations (both source and intermediate) should implement this method that performs the necessary business logic and handles the incoming Subscribers.
pack-priv int
Inherited from io.reactivex.processors.FlowableProcessor:
toSerialized