- All Implemented Interfaces:
Multi<T>,Flow.Publisher<T>
- Direct Known Subclasses:
io.smallrye.mutiny.operators.multi.AbstractMultiOperator,BroadcastProcessor,MultiOperator,UnicastProcessor
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionMakes thisMultibe able to broadcast its events (items,failure, andcompletion) to multiple subscribers.cache()Creates a newMultithat subscribes to this upstream and caches all of its events and replays them, to all the downstream subscribers.capDemandsUsing(LongFunction<Long> function) Cap all downstream subscriber requests to a value computed by a function.collect()convert()Converts aMultito other typesgroup()ifNoItem()Produces aMultireacting when no item event is fired by the upstream multi for the specified length of time.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".Log events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.Configures actions when the subscriber cancels the subscription.Allows configuring the actions or continuation to execute when thisMultifires the completion event.LikeMulti.onFailure(Predicate)but applied to all failures fired by the upstream multi.Configures a type of failure filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.Configures a predicate filtering the failures on which the behavior (specified with the returnedMultiOnFailure) is applied.onItem()Configures the behavior when anitemevent is received from the thisMultiConfigures the back-pressure behavior when the consumer cannot keep up with the emissions from thisMulti.Configures actions when items are being requested.Configures the action to execute when the observedMultisends aFlow.Subscription.Configures actions when thisMultiterminates on completion, on failure or on subscriber cancellation.A demand-pacer allows controlling upstream demand using a request and a delay.Allows pausing and resuming demand propagation to upstream using aDemandPauser.runSubscriptionOn(Executor executor) select()Selects items from thisMulti.skip()Skips items from thisMulti.Configures the subscriber consuming thisMulti.voidsubscribe(MultiSubscriber<? super T> subscriber) voidsubscribe(Flow.Subscriber<? super T> subscriber) Produces a newMultitransforming thisMultiinto a hot stream.toUni()<R> Multi<R> withContext(BiFunction<Multi<T>, Context, Multi<R>> builder) Materialize the subscriberContextfor a sub-pipeline.
-
Constructor Details
-
AbstractMulti
public AbstractMulti()
-
-
Method Details
-
subscribe
-
subscribe
- Specified by:
subscribein interfaceFlow.Publisher<T>
-
onItem
Description copied from interface:MultiConfigures the behavior when anitemevent is received from the thisMulti -
subscribe
Description copied from interface:MultiConfigures the subscriber consuming thisMulti. -
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
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
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
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
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
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
Description copied from interface:MultiProduces a newMultiinvoking theonItem,onFailureandonCompletionmethods on the suppliedExecutor.This operator delegates to
Multi.emitOn(Executor, int)with a default buffer size ofInfrastructure.getBufferSizeS()items.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.
-
emitOn
Description copied from interface:MultiProduces a newMultiinvoking theonItem,onFailureandonCompletionmethods on the suppliedExecutor.This operator uses a queue of capacity
bufferSizeto emit items from a running executor thread, reducing the need for thread context switches when items are emitted fast from the upstream.This operator tracks
Flow.Subscription.request(long)demand, but it does not forward requests to the upstream. It instead requestsbufferSizeelements at subscription time and wheneverbufferSizeitems have been emitted, allowing for efficient batching.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
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, theFlow.Subscriber.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
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
Description copied from interface:MultiSelects items from thisMulti. -
skip
Description copied from interface:MultiSkips items from thisMulti. -
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
-
onSubscription
Description copied from interface:MultiConfigures the action to execute when the observedMultisends aFlow.Subscription. 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
Description copied from interface:MultiMakes thisMultibe able to broadcast its events (items,failure, andcompletion) to multiple subscribers. -
convert
Description copied from interface:MultiConverts aMultito other typesExamples:
multi.convert().with(multi -> x); // Convert with a custom lambda converter -
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
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
Description copied from interface:MultiConfigures actions when items are being requested. -
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
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
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
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. -
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. -
withContext
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
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
-
pauseDemand
Description copied from interface:MultiAllows pausing and resuming demand propagation to upstream using aDemandPauser.Unlike cancellation which terminates the subscription, temporarily pausing suspends the demand without unsubscribing. This is useful for implementing flow control patterns where demand needs to be temporarily suspended based on external conditions (e.g., downstream system availability, rate limiting, resource constraints).
Example:
DemandPauser pauser = new DemandPauser(); Multi.createFrom().range(0, 100) .pauseDemand().using(pauser) .onItem().call(i -> Uni.createFrom().nullItem() .onItem().delayIt().by(Duration.ofMillis(10))) .subscribe().with(System.out::println); // Later, from anywhere in the application: pauser.pause(); // Stop requesting new items pauser.resume(); // Continue requesting itemsReactive Streams Compliance: This operator is compliant with the Reactive Streams specification as long as the
DemandPauserused to manage demand is eventually resumed. When the upstream signals completion, if the stream is paused, the completion signal is deferred until the stream is resumed, at which point all buffered items are delivered to downstream before the completion signal is propagated. When the upstream signals failure, any buffered items are discarded and the error is immediately propagated downstream.- Specified by:
pauseDemandin interfaceMulti<T>- Returns:
- a
MultiDemandPausingto configure the pausing behavior - See Also:
-
capDemandsUsing
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
-