Class ConnectableFlowable<T>
- java.lang.Object
-
- io.reactivex.rxjava3.core.Flowable<T>
-
- io.reactivex.rxjava3.flowables.ConnectableFlowable<T>
-
- Type Parameters:
T- the type of items emitted by theConnectableFlowable
- All Implemented Interfaces:
org.reactivestreams.Publisher<T>
- Direct Known Subclasses:
FlowablePublish,FlowableReplay
public abstract class ConnectableFlowable<T> extends Flowable<T>
AConnectableFlowableresembles an ordinaryFlowable, except that it does not begin emitting items when it is subscribed to, but only when itsconnect(io.reactivex.rxjava3.functions.Consumer<? super io.reactivex.rxjava3.disposables.Disposable>)method is called. In this way you can wait for all intendedSubscribers toFlowable.subscribe()to theFlowablebefore theFlowablebegins emitting items.
When the upstream terminates, the
ConnectableFlowableremains in this terminated state and, depending on the actual underlying implementation, relays cached events to lateSubscribers. In order to reuse and restart thisConnectableFlowable, thereset()method has to be called. When called, thisConnectableFlowablewill appear as fresh, unconnected source to newSubscribers. Disposing the connection will reset theConnectableFlowableto its fresh state and there is no need to callreset()in this case.Note that although
connect()andreset()are safe to call from multiple threads, it is recommended a dedicated thread or business logic manages the connection or resetting of aConnectableFlowableso that there is no unwanted signal loss due to earlyconnect()orreset()calls whileSubscribers are still being subscribed to to thisConnectableFlowableto receive signals from the get go.- Since:
- 2.0.0
- See Also:
- RxJava Wiki: Connectable Observable Operators
-
-
Constructor Summary
Constructors Constructor Description ConnectableFlowable()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description @NonNull Flowable<T>autoConnect()Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the firstSubscribersubscribes.@NonNull Flowable<T>autoConnect(int numberOfSubscribers)Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it.@NonNull Flowable<T>autoConnect(int numberOfSubscribers, @NonNull Consumer<? super Disposable> connection)Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it and calls the specified callback with theDisposableassociated with the established connection.@NonNull Disposableconnect()Instructs theConnectableFlowableto begin emitting the items from its underlyingFlowableto itsSubscribers.abstract voidconnect(@NonNull Consumer<? super Disposable> connection)Instructs theConnectableFlowableto begin emitting the items from its underlyingFlowableto itsSubscribers.@NonNull Flowable<T>refCount()Returns aFlowablethat stays connected to thisConnectableFlowableas long as there is at least one subscription to thisConnectableFlowable.@NonNull Flowable<T>refCount(int subscriberCount)Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect if all subscribers have unsubscribed.@NonNull Flowable<T>refCount(int subscriberCount, long timeout, @NonNull java.util.concurrent.TimeUnit unit)Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.@NonNull Flowable<T>refCount(int subscriberCount, long timeout, @NonNull java.util.concurrent.TimeUnit unit, @NonNull Scheduler scheduler)Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.@NonNull Flowable<T>refCount(long timeout, @NonNull java.util.concurrent.TimeUnit unit)Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.@NonNull Flowable<T>refCount(long timeout, @NonNull java.util.concurrent.TimeUnit unit, @NonNull Scheduler scheduler)Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.abstract voidreset()Resets thisConnectableFlowableinto its fresh state if it has terminated.-
Methods inherited from class io.reactivex.rxjava3.core.Flowable
all, amb, ambArray, ambWith, any, blockingFirst, blockingFirst, blockingForEach, blockingForEach, blockingIterable, blockingIterable, blockingLast, blockingLast, blockingLatest, blockingMostRecent, blockingNext, blockingSingle, blockingSingle, blockingStream, blockingStream, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, blockingSubscribe, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, buffer, bufferSize, cache, cacheWithInitialCapacity, cast, collect, collect, collectInto, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatest, combineLatestArray, combineLatestArray, combineLatestArrayDelayError, combineLatestArrayDelayError, combineLatestDelayError, combineLatestDelayError, compose, concat, concat, concat, concat, concat, concat, concatArray, concatArrayDelayError, concatArrayEager, concatArrayEager, concatArrayEagerDelayError, concatArrayEagerDelayError, concatDelayError, concatDelayError, concatDelayError, concatEager, concatEager, concatEager, concatEager, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatEagerDelayError, concatMap, concatMap, concatMap, concatMapCompletable, concatMapCompletable, concatMapCompletableDelayError, concatMapCompletableDelayError, concatMapCompletableDelayError, concatMapDelayError, concatMapDelayError, concatMapDelayError, concatMapEager, concatMapEager, concatMapEagerDelayError, concatMapEagerDelayError, concatMapIterable, concatMapIterable, concatMapMaybe, concatMapMaybe, concatMapMaybeDelayError, concatMapMaybeDelayError, concatMapMaybeDelayError, concatMapSingle, concatMapSingle, concatMapSingleDelayError, concatMapSingleDelayError, concatMapSingleDelayError, concatMapStream, concatMapStream, concatWith, concatWith, concatWith, concatWith, contains, count, create, debounce, debounce, debounce, debounce, defaultIfEmpty, defer, delay, delay, delay, delay, delay, delay, delaySubscription, delaySubscription, delaySubscription, dematerialize, distinct, distinct, distinct, distinctUntilChanged, distinctUntilChanged, distinctUntilChanged, doAfterNext, doAfterTerminate, doFinally, doOnCancel, doOnComplete, doOnEach, doOnEach, doOnError, doOnLifecycle, doOnNext, doOnRequest, doOnSubscribe, doOnTerminate, elementAt, elementAt, elementAtOrError, empty, error, error, filter, first, firstElement, firstOrError, firstOrErrorStage, firstStage, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMap, flatMapCompletable, flatMapCompletable, flatMapIterable, flatMapIterable, flatMapIterable, flatMapIterable, flatMapMaybe, flatMapMaybe, flatMapSingle, flatMapSingle, flatMapStream, flatMapStream, forEach, forEachWhile, forEachWhile, forEachWhile, fromAction, fromArray, fromCallable, fromCompletable, fromCompletionStage, fromFuture, fromFuture, fromIterable, fromMaybe, fromObservable, fromOptional, fromPublisher, fromRunnable, fromSingle, fromStream, fromSupplier, generate, generate, generate, generate, generate, groupBy, groupBy, groupBy, groupBy, groupBy, groupBy, groupJoin, hide, ignoreElements, interval, interval, interval, interval, intervalRange, intervalRange, isEmpty, join, just, just, just, just, just, just, just, just, just, just, last, lastElement, lastOrError, lastOrErrorStage, lastStage, lift, map, mapOptional, materialize, merge, merge, merge, merge, merge, merge, merge, merge, mergeArray, mergeArray, mergeArrayDelayError, mergeArrayDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeDelayError, mergeWith, mergeWith, mergeWith, mergeWith, never, observeOn, observeOn, observeOn, ofType, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureBuffer, onBackpressureDrop, onBackpressureDrop, onBackpressureLatest, onBackpressureLatest, onBackpressureReduce, onBackpressureReduce, onErrorComplete, onErrorComplete, onErrorResumeNext, onErrorResumeWith, onErrorReturn, onErrorReturnItem, onTerminateDetach, parallel, parallel, parallel, publish, publish, publish, publish, range, rangeLong, rebatchRequests, reduce, reduce, reduceWith, repeat, repeat, repeatUntil, repeatWhen, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, replay, retry, retry, retry, retry, retry, retryUntil, retryWhen, safeSubscribe, sample, sample, sample, sample, sample, sample, sample, scan, scan, scanWith, sequenceEqual, sequenceEqual, sequenceEqual, sequenceEqual, serialize, share, single, singleElement, singleOrError, singleOrErrorStage, singleStage, skip, skip, skip, skipLast, skipLast, skipLast, skipLast, skipLast, skipLast, skipUntil, skipWhile, sorted, sorted, startWith, startWith, startWith, startWith, startWithArray, startWithItem, startWithIterable, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, subscribe, subscribeActual, subscribeOn, subscribeOn, subscribeWith, switchIfEmpty, switchMap, switchMap, switchMapCompletable, switchMapCompletableDelayError, switchMapDelayError, switchMapDelayError, switchMapMaybe, switchMapMaybeDelayError, switchMapSingle, switchMapSingleDelayError, switchOnNext, switchOnNext, switchOnNextDelayError, switchOnNextDelayError, take, take, take, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeLast, takeUntil, takeUntil, takeWhile, test, test, test, throttleFirst, throttleFirst, throttleFirst, throttleLast, throttleLast, throttleLast, throttleLatest, throttleLatest, throttleLatest, throttleLatest, throttleLatest, throttleWithTimeout, throttleWithTimeout, throttleWithTimeout, timeInterval, timeInterval, timeInterval, timeInterval, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timeout, timer, timer, timestamp, timestamp, timestamp, timestamp, to, toFuture, toList, toList, toList, toMap, toMap, toMap, toMultimap, toMultimap, toMultimap, toMultimap, toObservable, toSortedList, toSortedList, toSortedList, toSortedList, unsafeCreate, unsubscribeOn, using, using, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, window, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, withLatestFrom, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zip, zipArray, zipWith, zipWith, zipWith, zipWith
-
-
-
-
Method Detail
-
connect
@SchedulerSupport("none") public abstract void connect(@NonNull @NonNull Consumer<? super Disposable> connection)
Instructs theConnectableFlowableto begin emitting the items from its underlyingFlowableto itsSubscribers.- Scheduler:
- The behavior is determined by the implementor of this abstract class.
- Parameters:
connection- the action that receives the connection subscription before the subscription to source happens allowing the caller to synchronously disconnect a synchronous source- Throws:
java.lang.NullPointerException- ifconnectionisnull- See Also:
- ReactiveX documentation: Connect
-
reset
@SchedulerSupport("none") public abstract void reset()
Resets thisConnectableFlowableinto its fresh state if it has terminated.Calling this method on a fresh or active
ConnectableFlowablehas no effect.- Scheduler:
- The behavior is determined by the implementor of this abstract class.
- Since:
- 3.0.0
-
connect
@NonNull @SchedulerSupport("none") public final @NonNull Disposable connect()
Instructs theConnectableFlowableto begin emitting the items from its underlyingFlowableto itsSubscribers.To disconnect from a synchronous source, use the
connect(io.reactivex.rxjava3.functions.Consumer)method.- Scheduler:
- The behavior is determined by the implementor of this abstract class.
- Returns:
- the subscription representing the connection
- See Also:
- ReactiveX documentation: Connect
-
refCount
@NonNull @CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(PASS_THROUGH) public @NonNull Flowable<T> refCount()
Returns aFlowablethat stays connected to thisConnectableFlowableas long as there is at least one subscription to thisConnectableFlowable.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload does not operate on any particularScheduler.
- Returns:
- the new
Flowableinstance - See Also:
- ReactiveX documentation: RefCount,
refCount(int),refCount(long, TimeUnit),refCount(int, long, TimeUnit)
-
refCount
@CheckReturnValue @SchedulerSupport("none") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount)
Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect if all subscribers have unsubscribed.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload does not operate on any particularScheduler.
History: 2.1.14 - experimental
- Parameters:
subscriberCount- the number of subscribers required to connect to the upstream- Returns:
- the new
Flowableinstance - Throws:
java.lang.IllegalArgumentException- ifsubscriberCountis non-positive- Since:
- 2.2
-
refCount
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(long timeout, @NonNull @NonNull java.util.concurrent.TimeUnit unit)
Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload operates on thecomputationScheduler.
History: 2.1.14 - experimental
- Parameters:
timeout- the time to wait before disconnecting after all subscribers unsubscribedunit- the time unit of the timeout- Returns:
- the new
Flowableinstance - Throws:
java.lang.NullPointerException- ifunitisnull- Since:
- 2.2
- See Also:
refCount(long, TimeUnit, Scheduler)
-
refCount
@CheckReturnValue @SchedulerSupport("custom") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(long timeout, @NonNull @NonNull java.util.concurrent.TimeUnit unit, @NonNull @NonNull Scheduler scheduler)
Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches 1 and disconnect after the specified timeout if all subscribers have unsubscribed.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload operates on the specifiedScheduler.
History: 2.1.14 - experimental
- Parameters:
timeout- the time to wait before disconnecting after all subscribers unsubscribedunit- the time unit of the timeoutscheduler- the target scheduler to wait on before disconnecting- Returns:
- the new
Flowableinstance - Throws:
java.lang.NullPointerException- ifunitorschedulerisnull- Since:
- 2.2
-
refCount
@CheckReturnValue @SchedulerSupport("io.reactivex:computation") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull @NonNull java.util.concurrent.TimeUnit unit)
Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload operates on thecomputationScheduler.
History: 2.1.14 - experimental
- Parameters:
subscriberCount- the number of subscribers required to connect to the upstreamtimeout- the time to wait before disconnecting after all subscribers unsubscribedunit- the time unit of the timeout- Returns:
- the new
Flowableinstance - Throws:
java.lang.NullPointerException- ifunitisnulljava.lang.IllegalArgumentException- ifsubscriberCountis non-positive- Since:
- 2.2
- See Also:
refCount(int, long, TimeUnit, Scheduler)
-
refCount
@CheckReturnValue @SchedulerSupport("custom") @BackpressureSupport(PASS_THROUGH) @NonNull public final @NonNull Flowable<T> refCount(int subscriberCount, long timeout, @NonNull @NonNull java.util.concurrent.TimeUnit unit, @NonNull @NonNull Scheduler scheduler)
Connects to the upstreamConnectableFlowableif the number of subscribed subscriber reaches the specified count and disconnect after the specified timeout if all subscribers have unsubscribed.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by the upstream
ConnectableFlowable's backpressure behavior. - Scheduler:
- This
refCountoverload operates on the specifiedScheduler.
History: 2.1.14 - experimental
- Parameters:
subscriberCount- the number of subscribers required to connect to the upstreamtimeout- the time to wait before disconnecting after all subscribers unsubscribedunit- the time unit of the timeoutscheduler- the target scheduler to wait on before disconnecting- Returns:
- the new
Flowableinstance - Throws:
java.lang.NullPointerException- ifunitorschedulerisnulljava.lang.IllegalArgumentException- ifsubscriberCountis non-positive- Since:
- 2.2
-
autoConnect
@NonNull @CheckReturnValue @BackpressureSupport(PASS_THROUGH) @SchedulerSupport("none") public @NonNull Flowable<T> autoConnect()
Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the firstSubscribersubscribes.
The connection happens after the first subscription and happens at most once during the lifetime of the returned
Flowable. If thisConnectableFlowableterminates, the connection is never renewed, no matter howSubscribers come and go. UserefCount()to renew a connection or dispose an active connection when allSubscribers have cancelled theirSubscriptions.This overload does not allow disconnecting the connection established via
connect(Consumer). Use theautoConnect(int, Consumer)overload to gain access to theDisposablerepresenting the only connection.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by
the upstream
ConnectableFlowable's behavior. - Scheduler:
autoConnectdoes not operate by default on a particularScheduler.
- Returns:
- a new
Flowableinstance that automatically connects to thisConnectableFlowablewhen the firstSubscribersubscribes - See Also:
refCount(),autoConnect(int, Consumer)
-
autoConnect
@NonNull @CheckReturnValue @BackpressureSupport(PASS_THROUGH) @SchedulerSupport("none") public @NonNull Flowable<T> autoConnect(int numberOfSubscribers)
Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it.
The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned
Flowable. If thisConnectableFlowableterminates, the connection is never renewed, no matter howSubscribers come and go. UserefCount()to renew a connection or dispose an active connection when allSubscribers have cancelled theirSubscriptions.This overload does not allow disconnecting the connection established via
connect(Consumer). Use theautoConnect(int, Consumer)overload to gain access to theDisposablerepresenting the only connection.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by
the upstream
ConnectableFlowable's behavior. - Scheduler:
autoConnectdoes not operate by default on a particularScheduler.
- Parameters:
numberOfSubscribers- the number of subscribers to await before calling connect on theConnectableFlowable. A non-positive value indicates an immediate connection.- Returns:
- a new
Flowableinstance that automatically connects to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it
-
autoConnect
@NonNull @CheckReturnValue @BackpressureSupport(PASS_THROUGH) @SchedulerSupport("none") public @NonNull Flowable<T> autoConnect(int numberOfSubscribers, @NonNull @NonNull Consumer<? super Disposable> connection)
Returns aFlowablethat automatically connects (at most once) to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it and calls the specified callback with theDisposableassociated with the established connection.
The connection happens after the given number of subscriptions and happens at most once during the lifetime of the returned
Flowable. If thisConnectableFlowableterminates, the connection is never renewed, no matter howSubscribers come and go. UserefCount()to renew a connection or dispose an active connection when allSubscribers have cancelled theirSubscriptions.- Backpressure:
- The operator itself doesn't interfere with backpressure which is determined by
the upstream
ConnectableFlowable's behavior. - Scheduler:
autoConnectdoes not operate by default on a particularScheduler.
- Parameters:
numberOfSubscribers- the number of subscribers to await before calling connect on theConnectableFlowable. A non-positive value indicates an immediate connection.connection- the callbackConsumerthat will receive theDisposablerepresenting the established connection- Returns:
- a new
Flowableinstance that automatically connects to thisConnectableFlowablewhen the specified number ofSubscribers subscribe to it and calls the specified callback with theDisposableassociated with the established connection - Throws:
java.lang.NullPointerException- ifconnectionisnull
-
-