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

public final Class ReplaySubject<T>

extends Subject<T>
Class Inheritance
Type Parameters
<T>
the value type
Imports
java.lang.reflect.Array, java.util.*, java.util.concurrent.TimeUnit, java.util.concurrent.atomic.*, io.reactivex.rxjava3.annotations.*, io.reactivex.rxjava3.core.Observer, .Scheduler, io.reactivex.rxjava3.disposables.Disposable, io.reactivex.rxjava3.internal.functions.ObjectHelper, io.reactivex.rxjava3.internal.util.*, io.reactivex.rxjava3.plugins.RxJavaPlugins

Replays events (in a configurable bounded or unbounded manner) to current and late Observers.

This subject does not have a public constructor by design; a new empty instance of this ReplaySubject can be created via the following create methods that allow specifying the retention policy for items:

Since a Subject is conceptionally derived from the Processor type in the Reactive Streams specification, 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 subject's state is not changed.

Since a ReplaySubject is an io.reactivex.rxjava3.core.Observable, it does not support backpressure.

When this ReplaySubject is terminated via onError(Throwable) or onComplete(), late io.reactivex.rxjava3.core.Observers will receive the retained/cached items first (if any) followed by the respective terminal event. If the ReplaySubject 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 Observer has subscribed, it will receive items continuously from that point on. Bounds only affect how many past items a new Observer will receive before it catches up with the live event feed.

Even though ReplaySubject implements the Observer interface, calling onSubscribe is not required (Rule 2.12) if the subject is used as a standalone source. However, calling onSubscribe after the ReplaySubject reached its terminal state will result in the given Disposable being disposed 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 Subjects provides such serialization and also protects against reentrance (i.e., when a downstream Observer consuming this subject also wants to call onNext(Object) on this subject recursively).

This ReplaySubject supports the standard state-peeking methods hasComplete(), hasThrowable(), getThrowable() and hasObservers() 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 ReplaySubject 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 it anymore.

Scheduler:
ReplaySubject does not operate by default on a particular io.reactivex.rxjava3.core.Scheduler and the Observers get notified on the thread the respective onXXX methods were invoked. Time-bound ReplaySubjects 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 ReplaySubject enters into a terminal state and emits the same Throwable instance to the last set of Observers. During this emission, if one or more Observers dispose their respective Disposables, the Throwable is delivered to the global error handler via io.reactivex.rxjava3.plugins.RxJavaPlugins#onError(Throwable) (multiple times if multiple Observers cancel at once). If there were no Observers subscribed to this ReplaySubject when the onError() was called, the global error handler is not invoked.

Example usage:

 ReplaySubject<Object> subject = ReplaySubject.create();
 subject.onNext("one");
 subject.onNext("two");
 subject.onNext("three");
 subject.onComplete();

 // both of the following will get the onNext/onComplete calls from above
 subject.subscribe(observer1);
 subject.subscribe(observer2);

  

Nested and Inner Type Summary

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

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

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 ReplaySubject.ReplayBuffer<T>
pack-priv boolean
pack-priv static final ReplaySubject.ReplayDisposable<T>[]
private static final Object[]
EMPTY_ARRAY

An empty array to avoid allocation in getValues().

pack-priv final AtomicReference<ReplaySubject.ReplayDisposable<T>[]>
pack-priv static final ReplaySubject.ReplayDisposable<T>[]

Constructor Summary

AccessConstructor and Description
pack-priv
ReplaySubject(ReplaySubject.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 ReplaySubject is released (as it is never part of a replay).

public static <
the type of items observed and emitted by the Subject
T
>
@NonNull ReplaySubject<T>

Returns:

the created subject
create
()

Creates an unbounded replay subject.

public static <
the type of items observed and emitted by the Subject
T
>
@NonNull ReplaySubject<T>

Returns:

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

Creates an unbounded replay subject with the specified initial buffer capacity.

pack-priv static <
the type of items observed and emitted by the Subject
T
>
ReplaySubject<T>

Returns:

the created subject
createUnbounded
()

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

public static <
the type of items observed and emitted by the Subject
T
>
@NonNull ReplaySubject<T>

Returns:

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

Creates a size-bounded replay subject.

public static <
the type of items observed and emitted by the Subject
T
>
@NonNull ReplaySubject<T>

Returns:

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

Creates a time-bounded replay subject.

public static <
the type of items observed and emitted by the Subject
T
>
@NonNull ReplaySubject<T>

Returns:

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

Creates a time- and size-bounded replay subject.

public @Nullable Throwable
getThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.getThrowable.

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

Returns:

a single value the Subject currently has or null if no such value exists
getValue
()

Returns a single value the Subject currently has or null if no such value exists.

public Object[]

Returns:

the array containing the snapshot of all values of the Subject
getValues
()

Returns an Object array containing snapshot all values of the Subject.

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 the Subject.

public boolean
hasComplete()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasComplete.

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

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasObservers.

Returns true if the subject has any Observers.
public boolean
hasThrowable()

Implements abstract io.reactivex.rxjava3.subjects.Subject.hasThrowable.

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

Returns:

true if the subject has any value
hasValue
()

Returns true if the subject has any value.

pack-priv int
public void
onComplete()

Implements io.reactivex.rxjava3.core.Observer.onComplete.

Notifies the Observer that the Observable has finished sending push-based notifications.
public void
public void
onNext(T
the item emitted by the Observable
t
)

Implements io.reactivex.rxjava3.core.Observer.onNext.

Provides the Observer with a new item to observe.
public void
pack-priv void
pack-priv int
size()

protected void
subscribeActual(Observer<? super T> observer)

pack-priv ReplaySubject.ReplayDisposable<T>[]
terminate(Object terminalValue)

Inherited from io.reactivex.rxjava3.subjects.Subject:
toSerialized