Class AbstractMulti<T>
- java.lang.Object
-
- io.smallrye.mutiny.operators.AbstractMulti<T>
-
- All Implemented Interfaces:
Multi<T>,org.reactivestreams.Publisher<T>
- Direct Known Subclasses:
io.smallrye.mutiny.operators.multi.AbstractMultiOperator,BroadcastProcessor,MultiOperator,UnicastProcessor
public abstract class AbstractMulti<T> extends java.lang.Object implements Multi<T>
-
-
Constructor Summary
Constructors Constructor Description AbstractMulti()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MultiBroadcast<T>broadcast()Makes thisMultibe able to broadcast its events (items,failure, andcompletion) to multiple subscribers.Multi<T>cache()Creates a newMultithat subscribes to this upstream and caches all of its events and replays them, to all the downstream subscribers.Multi<T>capDemandsUsing(java.util.function.LongFunction<java.lang.Long> function)Cap all downstream subscriber requests to a value computed by a function.MultiCollect<T>collect()MultiConvert<T>convert()Converts aMultito other typesMulti<T>emitOn(java.util.concurrent.Executor executor)Produces a newMultiinvoking theonItem,onFailureandonCompletionmethods on the suppliedExecutor.MultiGroup<T>group()MultiIfNoItem<T>ifNoItem()Produces aMultireacting when no item event is fired by the upstream multi for the specified length of time.Multi<T>log()Log events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber, and derives the identifier from the upstream operator class "simple name".Multi<T>log(java.lang.String identifier)Log events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.MultiOnCancel<T>onCancellation()Configures actions when the subscriber cancels the subscription.MultiOnCompletion<T>onCompletion()Allows configuring the actions or continuation to execute when thisMultifires the completion event.MultiOnFailure<T>onFailure()LikeMulti.onFailure(Predicate)but applied to all failures fired by the upstream multi.MultiOnFailure<T>onFailure(java.lang.Class<? extends java.lang.Throwable> typeOfFailure)Configures a type of failure filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.MultiOnFailure<T>onFailure(java.util.function.Predicate<? super java.lang.Throwable> predicate)Configures a predicate filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.MultiOnItem<T>onItem()Configures the behavior when anitemevent is received from the thisMultiMultiOverflow<T>onOverflow()Configures the back-pressure behavior when the consumer cannot keep up with the emissions from thisMulti.MultiOnRequest<T>onRequest()Configures actions when items are being requested.MultiOnSubscribe<T>onSubscribe()Configures the action to execute when the observedMultisends aSubscription.MultiOnSubscribe<T>onSubscription()Configures the action to execute when the observedMultisends aSubscription.MultiOnTerminate<T>onTermination()Configures actions when thisMultiterminates on completion, on failure or on subscriber cancellation.MultiDemandPacing<T>paceDemand()A demand-pacer allows controlling upstream demand using a request and a delay.Multi<T>runSubscriptionOn(java.util.concurrent.Executor executor)MultiSelect<T>select()Selects items from thisMulti.MultiSkip<T>skip()Skips items from thisMulti.MultiSubscribe<T>subscribe()Configures the subscriber consuming thisMulti.voidsubscribe(MultiSubscriber<? super T> subscriber)voidsubscribe(org.reactivestreams.Subscriber<? super T> subscriber)Multi<T>toHotStream()Produces a newMultitransforming thisMultiinto a hot stream.Uni<T>toUni()<R> Multi<R>withContext(java.util.function.BiFunction<Multi<T>,Context,Multi<R>> builder)Materialize the subscriberContextfor a sub-pipeline.
-
-
-
Method Detail
-
subscribe
public void subscribe(MultiSubscriber<? super T> subscriber)
-
subscribe
public void subscribe(org.reactivestreams.Subscriber<? super T> subscriber)
- Specified by:
subscribein interfaceorg.reactivestreams.Publisher<T>
-
onItem
public MultiOnItem<T> onItem()
Description copied from interface:MultiConfigures the behavior when anitemevent is received from the thisMulti
-
subscribe
public MultiSubscribe<T> subscribe()
Description copied from interface:MultiConfigures the subscriber consuming thisMulti.
-
toUni
public Uni<T> toUni()
Description copied from interface:MultiCreates aUnifrom thisMulti.When a subscriber subscribes to the returned
Uni, it subscribes to thisMultiand requests one item. The event emitted by thisMultiare then forwarded to theUni:- on item event, the item is fired by the produced
Uni - on failure event, the failure is fired by the produced
Uni - on completion event, a
nullitem is fired by the producesUni - any item or failure events received after the first event is dropped
If the subscription on the produced
Uniis cancelled, the subscription to the passedMultiis also cancelled. - on item event, the item is fired by the produced
-
onFailure
public MultiOnFailure<T> onFailure()
Description copied from interface:MultiLikeMulti.onFailure(Predicate)but applied to all failures fired by the upstream multi. It allows configuring the on failure behavior (recovery, retry...).
-
onFailure
public MultiOnFailure<T> onFailure(java.util.function.Predicate<? super java.lang.Throwable> predicate)
Description copied from interface:MultiConfigures a predicate filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.For instance, to only when an
IOExceptionis fired as failure you can use:multi.onFailure(IOException.class).recoverWithItem("hello")The fallback value (
hello) will only be used if the upstream multi fires a failure of typeIOException.
-
onFailure
public MultiOnFailure<T> onFailure(java.lang.Class<? extends java.lang.Throwable> typeOfFailure)
Description copied from interface:MultiConfigures a type of failure filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.For instance, to only when an
IOExceptionis fired as failure you can use:multi.onFailure(IOException.class).recoverWithItem("hello")The fallback value (
hello) will only be used if the upstream multi fire a failure of typeIOException.*
-
ifNoItem
public MultiIfNoItem<T> ifNoItem()
Description copied from interface:MultiProduces aMultireacting when no item event is fired by the upstream multi for the specified length of time.This
Multidetects if thisMultidoes not emit an item for the configured length of time.Examples:
multi.ifNoItem().after(Duration.ofMillis(1000)).fail() // Propagate a TimeoutException multi.ifNoItem().after(Duration.ofMillis(1000)).recoverWithCompletion() // Complete the event on timeout multi.ifNoItem().after(Duration.ofMillis(1000)).on(myExecutor)... // Configure the executor calling on timeout actions
-
cache
public Multi<T> cache()
Description copied from interface:MultiCreates a newMultithat subscribes to this upstream and caches all of its events and replays them, to all the downstream subscribers.
-
emitOn
public Multi<T> emitOn(java.util.concurrent.Executor executor)
Description copied from interface:MultiProduces a newMultiinvoking theonItem,onFailureandonCompletionmethods on the suppliedExecutor.Instead of receiving the
itemevent on the thread firing the event, this method influences the threading context to switch to a thread from the given executor. Same behavior for failure and completion.Note that the subscriber is guaranteed to never be called concurrently.
Be careful as this operator can lead to concurrency problems with non thread-safe objects such as CDI request-scoped beans. It might also break reactive-streams semantics with items being emitted concurrently.
-
runSubscriptionOn
public Multi<T> runSubscriptionOn(java.util.concurrent.Executor executor)
Description copied from interface:MultiWhen a subscriber subscribes to thisMulti, execute the subscription to the upstreamMultion a thread from the given executor. As a result, theSubscriber.onSubscribe(Subscription)method will be called on this thread (except mentioned otherwise)- Specified by:
runSubscriptionOnin interfaceMulti<T>- Parameters:
executor- the executor to use, must not benull- Returns:
- a new
Multi
-
onCompletion
public MultiOnCompletion<T> onCompletion()
Description copied from interface:MultiAllows configuring the actions or continuation to execute when thisMultifires the completion event.- Specified by:
onCompletionin interfaceMulti<T>- Returns:
- the object to configure the action.
-
select
public MultiSelect<T> select()
Description copied from interface:MultiSelects items from thisMulti.
-
skip
public MultiSkip<T> skip()
Description copied from interface:MultiSkips items from thisMulti.
-
onOverflow
public MultiOverflow<T> onOverflow()
Description copied from interface:MultiConfigures the back-pressure behavior when the consumer cannot keep up with the emissions from thisMulti.- Specified by:
onOverflowin interfaceMulti<T>- Returns:
- the object to configure the overflow strategy
-
onSubscribe
public MultiOnSubscribe<T> onSubscribe()
Description copied from interface:MultiConfigures the action to execute when the observedMultisends aSubscription. The downstream don't have a subscription yet. It will be passed once the configured action completes.For example:
multi.onSubscribe().invoke(sub -> System.out.println("subscribed")); // Delay the subscription by 1 second (or until an asynchronous action completes) multi.onSubscribe().call(sub -> Uni.createFrom(1).onItem().delayIt().by(Duration.ofSecond(1)));- Specified by:
onSubscribein interfaceMulti<T>- Returns:
- the object to configure the action to execution on subscription.
-
onSubscription
public MultiOnSubscribe<T> onSubscription()
Description copied from interface:MultiConfigures the action to execute when the observedMultisends aSubscription. The downstream does not have a subscription yet. It will be passed once the configured action completes.For example:
multi.onSubscription().invoke(sub -> System.out.println("subscribed")); // Delay the subscription by 1 second (or until an asynchronous action completes) multi.onSubscription().call(sub -> Uni.createFrom(1).onItem().delayIt().by(Duration.ofSecond(1)));- Specified by:
onSubscriptionin interfaceMulti<T>- Returns:
- the object to configure the action to execution on subscription.
-
broadcast
public MultiBroadcast<T> broadcast()
Description copied from interface:MultiMakes thisMultibe able to broadcast its events (items,failure, andcompletion) to multiple subscribers.
-
convert
public MultiConvert<T> convert()
Description copied from interface:MultiConverts aMultito other typesExamples:
multi.convert().with(multi -> x); // Convert with a custom lambda converter- Specified by:
convertin interfaceMulti<T>- Returns:
- the object to convert an
Multiinstance - See Also:
MultiConvert
-
onTermination
public MultiOnTerminate<T> onTermination()
Description copied from interface:MultiConfigures actions when thisMultiterminates on completion, on failure or on subscriber cancellation.- Specified by:
onTerminationin interfaceMulti<T>- Returns:
- the object to configure the termination actions
-
onCancellation
public MultiOnCancel<T> onCancellation()
Description copied from interface:MultiConfigures actions when the subscriber cancels the subscription.- Specified by:
onCancellationin interfaceMulti<T>- Returns:
- the object to configure the cancellation actions
-
onRequest
public MultiOnRequest<T> onRequest()
Description copied from interface:MultiConfigures actions when items are being requested.
-
collect
public MultiCollect<T> collect()
Description copied from interface:MultiProducesUnicollecting/aggregating items from thisMulti. It allows accumulating the items emitted by thismultiinto a structure such as a into aList(MultiCollect.asList()), aMap(MultiCollect.asMap(Function), or a custom collector. When thismultisends the completion signal, the structure is emitted by the returnedUni.If this
Multiemits a failure, the producedUniproduces the same failure and the aggregated items are discarded.You can also retrieve the first and last items using
MultiCollect.first()andMultiCollect.last(). Be aware to not used method collecting items on unbounded / infiniteMulti.
-
group
public MultiGroup<T> group()
Description copied from interface:MultiProducesMultigrouping items from thisMultiinto various "form of chunks" (list,Multi). The grouping can be done linearly (MultiGroup.intoLists()andMultiGroup.intoMultis(), or based on a grouping function (MultiGroup.by(Function))
-
toHotStream
public Multi<T> toHotStream()
Description copied from interface:MultiProduces a newMultitransforming thisMultiinto a hot stream.With a hot stream, when no subscribers are present, emitted items are dropped. Late subscribers would only receive items emitted after their subscription. If the upstream has already been terminated, the termination event (failure or completion) is forwarded to the subscribers.
Note that this operator consumes the upstream stream without back-pressure. It still enforces downstream back-pressure. If the subscriber is not ready to receive an item when the upstream emits an item, the subscriber gets a
BackPressureFailurefailure.- Specified by:
toHotStreamin interfaceMulti<T>- Returns:
- the new multi.
-
log
public Multi<T> log(java.lang.String identifier)
Description copied from interface:MultiLog events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.Events will be logged as long as the
Multihasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructureclass.- Specified by:
login interfaceMulti<T>- Parameters:
identifier- an identifier of this operator to be used in log events- Returns:
- a new
Multi - See Also:
Infrastructure.setOperatorLogger(Infrastructure.OperatorLogger)
-
log
public Multi<T> log()
Description copied from interface:MultiLog events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber, and derives the identifier from the upstream operator class "simple name".Events will be logged as long as the
Multihasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructureclass.- Specified by:
login interfaceMulti<T>- Returns:
- a new
Multi - See Also:
Multi.log(String),Infrastructure.setOperatorLogger(Infrastructure.OperatorLogger)
-
withContext
public <R> Multi<R> withContext(java.util.function.BiFunction<Multi<T>,Context,Multi<R>> builder)
Description copied from interface:MultiMaterialize the subscriberContextfor a sub-pipeline.The provided function takes this
Multiand theContextas parameters, and returns aMultito build the sub-pipeline, as in:someMulti.withContext((multi, ctx) -> multi.onItem().transform(n -> n + "::" + ctx.getOrElse("foo", () -> "yolo")));Note that the
builderfunction is called at subscription time, so it cannot see context updates from upstream operators yet.
-
paceDemand
public MultiDemandPacing<T> paceDemand()
Description copied from interface:MultiA demand-pacer allows controlling upstream demand using a request and a delay.Each time the delay expires the pacer can evaluate a new demand based on the previous request and the number of emitted items that have been observed since the previous request.
In the following example a demand of 25 is issued every 100ms, using the default worker pool to perform requests:
var pacer = new FixedDemandPacer(25L, Duration.ofMillis(100L)); var multi = Multi.createFrom().range(0, 100) .paceDemand().on(Infrastructure.getDefaultWorkerPool()).using(pacer);Important: this operator is NOT compliant with the reactive streams specification. Downstream demand requests are being ignored, so it is possible that this operator requests more than what the downstream subscriber would want, depending on theDemandPacerobject in use.- Specified by:
paceDemandin interfaceMulti<T>- Returns:
- a group to configure the demand pacing
-
capDemandsUsing
public Multi<T> capDemandsUsing(java.util.function.LongFunction<java.lang.Long> function)
Description copied from interface:MultiCap all downstream subscriber requests to a value computed by a function.The function must return a valid demand which is strictly positive and below or equal to that of the current outstanding demand. The function argument is the current outstanding demand.
- Specified by:
capDemandsUsingin interfaceMulti<T>- Parameters:
function- the function, must not benull, must not returnnull, must return alongsuch that(0 < n <= outstanding)whereoutstandingis the current outstanding demand- Returns:
- the new
Multi
-
-