Class MultiCreate
- java.lang.Object
-
- io.smallrye.mutiny.groups.MultiCreate
-
public class MultiCreate extends java.lang.Object
Group methods allowing to createMulti
instances from various sources.
-
-
Field Summary
Fields Modifier and Type Field Description static MultiCreate
INSTANCE
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <T> Multi<T>
completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
<T> Multi<T>
completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)
<T> Multi<T>
context(java.util.function.Function<Context,Multi<? extends T>> mapper)
<I,T>
Multi<T>converter(io.smallrye.mutiny.converters.MultiConverter<I,T> converter, I instance)
Creates a newMulti
from the passed instance with the passed converter.<T> Multi<T>
deferred(java.util.function.Supplier<Multi<? extends T>> supplier)
<T> Multi<T>
emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer)
Likeemitter(Consumer, BackPressureStrategy)
with theBackPressureStrategy.BUFFER
strategy.<T> Multi<T>
emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, int bufferSize)
Likeemitter(Consumer)
with theBackPressureStrategy.BUFFER
strategy and the given buffer size.<T> Multi<T>
emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, BackPressureStrategy strategy)
Creates aMulti
deferring the logic to the given consumer.<T> Multi<T>
empty()
Creates aMulti
that fires the completion event without having emitted any items.<T> Multi<T>
failure(java.lang.Throwable failure)
Creates aMulti
that emits afailure
event immediately after being subscribed to.<T> Multi<T>
failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aMulti
that emits afailure
event produced using the passed supplier immediately after being subscribed to.<S,T>
Multi<T>generator(java.util.function.Supplier<S> initialStateSupplier, java.util.function.BiFunction<S,GeneratorEmitter<? super T>,S> generator)
Creates aMulti
from on some initial state and a generator function.<T> Multi<T>
item(java.util.function.Supplier<? extends T> supplier)
Creates a newMulti
that emits an item immediately after being subscribed to with the specified single (potentiallynull
) value.<T> Multi<T>
item(T item)
Creates a newMulti
that emits an item immediately after being subscribed to with the specified single item.<T> Multi<T>
items(java.util.function.Supplier<? extends java.util.stream.Stream<? extends T>> supplier)
Creates a newMulti
that emits the items immediately after being subscribed to.<T> Multi<T>
items(java.util.stream.Stream<T> items)
Creates a newMulti
that emits the items from the passedStream
individually after being subscribed to (according to the subscriber's request).<T> Multi<T>
items(T... items)
Creates a newMulti
that emits the items individually after being subscribed to (according to the subscriber's request).<T> Multi<T>
iterable(java.lang.Iterable<T> iterable)
Creates a newMulti
that emits the items individually after being subscribed to (according to the subscriber's request).<T> Multi<T>
nothing()
Creates aMulti
that will never fire any events.<T> Multi<T>
optional(java.util.function.Supplier<java.util.Optional<T>> supplier)
Creates a newMulti
that emits an item immediately after being subscribed to with the value contained in the optional supplied bysupplier
.<T> Multi<T>
optional(java.util.Optional<T> optional)
Creates a newMulti
that emits an item immediately after being subscribed to with the value contained in the given optional ifOptional.isPresent()
or empty otherwise.<T> Multi<T>
publisher(org.reactivestreams.Publisher<T> publisher)
Creates aMulti
from the passedPublisher
.Multi<java.lang.Integer>
range(int startInclusive, int endExclusive)
<R,I>
MultiResource<R,I>resource(java.util.function.Supplier<? extends R> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)
Creates aMulti
from a resource, generated by a supplier function called for each individualSubscriber
, while streaming the items from aPublisher/Multi
created from the resource.<R,I>
MultiResourceUni<R,I>resourceFromUni(java.util.function.Supplier<Uni<R>> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)
Creates aMulti
from a resource, generated by a supplier function called for each individualSubscriber
, while streaming the items from aPublisher/Multi
created from the resource.<T> Multi<T>
safePublisher(org.reactivestreams.Publisher<T> publisher)
Creates aMulti
from the passedPublisher
.MultiTimePeriod
ticks()
Creates aMulti
that emitslong
items (ticks) starting with 0 and incrementing at specified time intervals.<T> Multi<T>
uni(Uni<T> uni)
-
-
-
Field Detail
-
INSTANCE
public static final MultiCreate INSTANCE
-
-
Method Detail
-
converter
@CheckReturnValue public <I,T> Multi<T> converter(io.smallrye.mutiny.converters.MultiConverter<I,T> converter, I instance)
Creates a newMulti
from the passed instance with the passed converter.
-
completionStage
@CheckReturnValue public <T> Multi<T> completionStage(java.util.concurrent.CompletionStage<? extends T> stage)
Creates aMulti
from the givenCompletionStage
orCompletableFuture
. The producedMulti
emits the item of the passedCompletionStage
and then fires the completion event. If theCompletionStage
never completes (or fails), the producedMulti
would not emit anyitem
orfailure
events.Cancelling the subscription on the produced
Multi
cancels the passedCompletionStage
(callingCompletableFuture.cancel(boolean)
on the future retrieved usingCompletionStage.toCompletableFuture()
.If the stage has already been completed (or failed), the produced
Multi
sends the item or failure immediately after subscription. If it's not the case, the subscriber's callbacks are called on the thread used by the passedCompletionStage
. If the completion stage redeemsnull
, it fires the completion event without any item.- Type Parameters:
T
- the type of item- Parameters:
stage
- the stage, must not benull
- Returns:
- the produced
Multi
-
completionStage
@CheckReturnValue public <T> Multi<T> completionStage(java.util.function.Supplier<? extends java.util.concurrent.CompletionStage<? extends T>> supplier)
Creates aMulti
from the givenCompletionStage
orCompletableFuture
. The future is created by invoking the passedSupplier
lazily at subscription time.The produced
Multi
emits the item of the passedCompletionStage
followed by the completion event. If theCompletionStage
never completes (or failed), the producedMulti
would not emit an item or a failure.Cancelling the subscription on the produced
Multi
cancels the passedCompletionStage
(callingCompletableFuture.cancel(boolean)
on the future retrieved usingCompletionStage.toCompletableFuture()
.If the produced stage has already been completed (or failed), the produced
Multi
sends the item or failure immediately after subscription. In the case of item, the event is followed by the completion event. If the produced stage is not yet completed, the subscriber's callbacks are called on the thread used by the passedCompletionStage
.If the produced completion stage redeems
null
, it fires the completion event without any item.If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
- Returns:
- the produced
Multi
-
safePublisher
@CheckReturnValue public <T> Multi<T> safePublisher(org.reactivestreams.Publisher<T> publisher)
Creates aMulti
from the passedPublisher
.It is assumed that the
Publisher
is fully compliant with the Reactive Streams protocol and passes the TCK. If this is not the case usepublisher(Publisher)
instead.When a subscriber subscribes to the produced
Multi
, it subscribes to thePublisher
and delegate the requests. Note that each Multi's subscriber would produce a new subscription.If the Multi's observer cancels its subscription, the subscription to the
Publisher
is also cancelled.- Type Parameters:
T
- the type of item- Parameters:
publisher
- the publisher, must not benull
- Returns:
- the produced
Multi
- See Also:
publisher(Publisher)
-
publisher
@CheckReturnValue public <T> Multi<T> publisher(org.reactivestreams.Publisher<T> publisher)
Creates aMulti
from the passedPublisher
.The
Publisher
is not assumed to be fully compliant with the Reactive Streams TCK, hence it is wrapped around a subscriber that enforces the Reactive Streams protocol. If you know thePublisher
is safe then you should usesafePublisher(Publisher)
instead.When a subscriber subscribes to the produced
Multi
, it subscribes to thePublisher
and delegate the requests. Note that each Multi's subscriber would produce a new subscription.If the Multi's observer cancels its subscription, the subscription to the
Publisher
is also cancelled.- Type Parameters:
T
- the type of item- Parameters:
publisher
- the publisher, must not benull
- Returns:
- the produced
Multi
- See Also:
safePublisher(Publisher)
-
uni
@CheckReturnValue public <T> Multi<T> uni(Uni<T> uni)
Creates an never ofMulti
from the givenUni
.When a subscriber subscribes to the returned
Multi
and request an item, it subscribes to the givenUni
and the events from thisUni
are propagated to theMulti
:- if the
Uni
emits a non-null
item - this item is propagated to theMulti
and followed with the completion event - if the
Uni
emits anull
item - theMulti
fires the completion event - if the
Uni
emits a failure, this failure event is propagated by theMulti
It's important to note that the subscription to the
Uni
happens when the subscriber to the producedMulti
requests values, and not at subscription time. - if the
-
item
@CheckReturnValue public <T> Multi<T> item(java.util.function.Supplier<? extends T> supplier)
Creates a newMulti
that emits an item immediately after being subscribed to with the specified single (potentiallynull
) value. The value is retrieved lazily at subscription time, using the passedSupplier
. Unlikedeferred(Supplier)
, the supplier produces an item and not aMulti
.If the supplier produces
null
, the producedMulti
fires the completion event. If the supplier produces a non-null
item, the producedMulti
fires an item event followed with the completion event. If the supplier throws an exception, a failure event with the exception is fired.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
supplier
- the item supplier, must not benull
, can producenull
- Returns:
- the new
Multi
-
items
@CheckReturnValue public <T> Multi<T> items(java.util.function.Supplier<? extends java.util.stream.Stream<? extends T>> supplier)
Creates a newMulti
that emits the items immediately after being subscribed to. The individual items come from theStream
supplied by the givenSupplier
. This supplier is called at subscription time.If the supplier produces
null
, the producedMulti
fires a failure event. If the supplier produces an empty stream, the producedMulti
fires a completion event. For each item from the supplied stream, an item event is fired. When all the items have been emitted, the completion event is fired. If the supplier throws an exception, a failure event with the exception is fired. The stream is consumed sequentially.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
supplier
- the item supplier, must not benull
, must not producenull
- Returns:
- the new
Multi
-
item
@CheckReturnValue public <T> Multi<T> item(T item)
-
items
@SafeVarargs @CheckReturnValue public final <T> Multi<T> items(T... items)
Creates a newMulti
that emits the items individually after being subscribed to (according to the subscriber's request).If
items
isnull
, anIllegalArgumentException
is thrown at call time. If one of the item fromitems
isnull
, a failure event is fired (with anIllegalArgumentException
). When all the items have been emitted, the completion event is fired.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
items
- the items, must not benull
, must not containnull
- Returns:
- the new
Multi
-
iterable
@CheckReturnValue public <T> Multi<T> iterable(java.lang.Iterable<T> iterable)
Creates a newMulti
that emits the items individually after being subscribed to (according to the subscriber's request).If
iterable
isnull
, anIllegalArgumentException
is thrown at call time. If one of the item fromiterable
isnull
, a failure event is fired (with anIllegalArgumentException
). When all the items have been emitted, the completion event is fired.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
iterable
- the iterable of items, must not benull
, must not containnull
- Returns:
- the new
Multi
-
items
@CheckReturnValue public <T> Multi<T> items(java.util.stream.Stream<T> items)
Creates a newMulti
that emits the items from the passedStream
individually after being subscribed to (according to the subscriber's request).If
items
isnull
, anIllegalArgumentException
is thrown at call time. If one of the item from the stream isnull
, a failure event is fired (with anIllegalArgumentException
). When all the items have been emitted, the completion event is fired. The stream is consumed sequentially.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
items
- the items, must not benull
, must not containnull
- Returns:
- the new
Multi
-
optional
@CheckReturnValue public <T> Multi<T> optional(java.util.Optional<T> optional)
Creates a newMulti
that emits an item immediately after being subscribed to with the value contained in the given optional ifOptional.isPresent()
or empty otherwise.
-
optional
@CheckReturnValue public <T> Multi<T> optional(java.util.function.Supplier<java.util.Optional<T>> supplier)
Creates a newMulti
that emits an item immediately after being subscribed to with the value contained in the optional supplied bysupplier
.If the optional is empty, an empty
Multi
is produced. Otherwise the contained value is emitted as item, followed with the completion event. Unlikeoptional(Optional)
, the passedSupplier
is called lazily at subscription time.If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of the produced item- Parameters:
supplier
- the supplier, must not benull
, must not returnnull
- Returns:
- the new
Multi
-
emitter
@CheckReturnValue public <T> Multi<T> emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer)
Likeemitter(Consumer, BackPressureStrategy)
with theBackPressureStrategy.BUFFER
strategy.Note that to create hot streams, you should use a
BroadcastProcessor
.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
consumer
- the consumer receiving the emitter, must not benull
- Returns:
- the produced
Multi
-
emitter
@CheckReturnValue public <T> Multi<T> emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, int bufferSize)
Likeemitter(Consumer)
with theBackPressureStrategy.BUFFER
strategy and the given buffer size.Note that to create hot streams, you should use a
BroadcastProcessor
.If the buffer is full, a
BufferOverflowException
in propagated downstream.- Type Parameters:
T
- the type of item emitted by the produced Multi- Parameters:
consumer
- the consumer receiving the emitter, must not benull
bufferSize
- the buffer size, must be strictly positive- Returns:
- the produced
Multi
-
emitter
@CheckReturnValue public <T> Multi<T> emitter(java.util.function.Consumer<MultiEmitter<? super T>> consumer, BackPressureStrategy strategy)
Creates aMulti
deferring the logic to the given consumer. The consumer can be used with callback-based APIs to fire items (non-null
), failure or completion events.Emitting
null
value is not supported. Emitting values after having fired a failure or the completion event is a no-op. So subsequent item events are dropped.Using this method, you can produce a
Multi
based on listener or callbacks APIs. You register the listener in the consumer and emits the items / failure / completion events when the listener is invoked. Don't forget to unregister the listener on cancellation.If the consumer throws an exception, a failure event with the exception is fired.
Note that to create hot streams, you should use a
BroadcastProcessor
.- Type Parameters:
T
- the type of items emitted by the emitter. Must not benull
- Parameters:
consumer
- callback receiving theMultiEmitter
and events downstream. The callback is called for each subscriber (at subscription time). Must not benull
strategy
- the back pressure strategy to apply when the downstream subscriber cannot keep up with the items emitted by the emitter.- Returns:
- the produced
Multi
-
deferred
@CheckReturnValue public <T> Multi<T> deferred(java.util.function.Supplier<Multi<? extends T>> supplier)
Creates aMulti
thatsupplies
anMulti
to subscribe to for eachSubscriber
. The supplier is called at subscription time.In practice, it defers the
Multi
creation at subscription time and allows each subscriber to get differentMulti
. So, it does not create theMulti
until ansubscriber
subscribes, and creates a freshMulti
for each subscriber.Unlike
item(Supplier)
, the supplier produces anMulti
(and not an item).If the supplier throws an exception, a failure event with the exception is fired. If the supplier produces
null
, a failure event containing aNullPointerException
is fired.- Type Parameters:
T
- the type of item- Parameters:
supplier
- the supplier, must not benull
, must not producenull
- Returns:
- the produced
Multi
-
context
@Experimental("Context support is a new experimental API introduced in Mutiny 1.3.0") @CheckReturnValue public <T> Multi<T> context(java.util.function.Function<Context,Multi<? extends T>> mapper)
Creates aMulti
usingFunction.apply(Object)
on the subscription-boundContext
(the mapper is called at subscription time).This method is semantically equivalent to
deferred(Supplier)
, except that it passes a context.- Type Parameters:
T
- the type of the item- Parameters:
mapper
- the mapper, must not benull
, must not producenull
- Returns:
- the produced
Multi
-
failure
@CheckReturnValue public <T> Multi<T> failure(java.lang.Throwable failure)
Creates aMulti
that emits afailure
event immediately after being subscribed to.
-
failure
@CheckReturnValue public <T> Multi<T> failure(java.util.function.Supplier<java.lang.Throwable> supplier)
Creates aMulti
that emits afailure
event produced using the passed supplier immediately after being subscribed to. The supplier is called at subscription time, and produces an never ofThrowable
. If the supplier throws an exception, afailure
event is fired with this exception. If the supplier producesnull
, afailure
event is fired with aNullPointerException
.
-
nothing
@CheckReturnValue public <T> Multi<T> nothing()
Creates aMulti
that will never fire any events.- Type Parameters:
T
- the virtual type of item- Returns:
- a never emitting
Multi
-
empty
@CheckReturnValue public <T> Multi<T> empty()
Creates aMulti
that fires the completion event without having emitted any items. An emptyMulti
does not fires a failure event either.- Type Parameters:
T
- the virtual type of item- Returns:
- an empty
Multi
-
ticks
@CheckReturnValue public MultiTimePeriod ticks()
Creates aMulti
that emitslong
items (ticks) starting with 0 and incrementing at specified time intervals.The timer starts at the first request. Once this request is received, the produced stream is a hot stream.
Be aware that if the subscriber does not request enough items in time, a back pressure failure is fired. The produced
Multi
never completes until cancellation by the subscriber.The callbacks are invoked on the executor passed in
MultiTimePeriod.onExecutor(ScheduledExecutorService)
.- Returns:
- the object to configure the time period (initial delay, executor, interval)
-
range
@CheckReturnValue public Multi<java.lang.Integer> range(int startInclusive, int endExclusive)
Creates aMulti
emitting the sequence of integer fromstartInclusive
toendExclusive
. Once all the integers have been emitted, the completion event is fired.- Parameters:
startInclusive
- the start integer (inclusive)endExclusive
- the end integer (exclusive)- Returns:
- the
Multi
emitting the items
-
resource
@CheckReturnValue public <R,I> MultiResource<R,I> resource(java.util.function.Supplier<? extends R> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)
Creates aMulti
from a resource, generated by a supplier function called for each individualSubscriber
, while streaming the items from aPublisher/Multi
created from the resource.This method gets a resource and creates a
Publisher
from this resource (by calling thestreamSupplier
function). The subscriber receives the items from thisPublisher
. When the stream completes, fails or when the subscriber cancels the subscription, a finalizer is called to close the resource. This cleanup process can be either synchronous and asynchronous, as well as distinct for each type of event.This method can be seen as a reactive version of the "try/finally" construct.
- Type Parameters:
R
- the type of the resource.I
- the type of items emitted by the stream produced by thestreamSupplier
.- Parameters:
resourceSupplier
- a supplier called for each subscriber to generate the resource, must not benull
.streamSupplier
- a function returning the stream for the given resource instance, must not benull
.- Returns:
- an object to configure the finalizers.
-
resourceFromUni
@CheckReturnValue public <R,I> MultiResourceUni<R,I> resourceFromUni(java.util.function.Supplier<Uni<R>> resourceSupplier, java.util.function.Function<? super R,? extends org.reactivestreams.Publisher<I>> streamSupplier)
Creates aMulti
from a resource, generated by a supplier function called for each individualSubscriber
, while streaming the items from aPublisher/Multi
created from the resource.Unlike
This method gets a resource and creates aresource(Supplier, Function)
, theSupplier
produces aUni
. So, the actual resource can be resolved asynchronously.Publisher
from this resource (by calling thestreamSupplier
function once theUni
emits the resource instance). The subscriber receives the items from thisPublisher
. When the stream completes, fails or when the subscriber cancels the subscription, a finalizer is called to close the resource. This cleanup process can be either synchronous and asynchronous, as well as distinct for each type of event.If the Uni produced by the
resourceSupplier
emits a failure, the failure is propagated downstream. If the Uni produced by theresourceSupplier
does not emit an item before downstream cancellation, the resource creation is cancelled.This method can be seen as a reactive version of the "try/finally" construct.
- Type Parameters:
R
- the type of the resource.I
- the type of items emitted by the stream produced by thestreamSupplier
.- Parameters:
resourceSupplier
- a supplier called for each subscriber to generate the resource, must not benull
. The supplier produces aUni
emitting the resource.streamSupplier
- a function returning the stream for the given resource instance, must not benull
.- Returns:
- an object to configure the finalizers.
-
generator
@CheckReturnValue public <S,T> Multi<T> generator(java.util.function.Supplier<S> initialStateSupplier, java.util.function.BiFunction<S,GeneratorEmitter<? super T>,S> generator)
Creates aMulti
from on some initial state and a generator function.The generator function accepts the current state and a
GeneratorEmitter
to emit items, failures and completion events. The function shall return the new state which will be used for the next item generation, if any. The state can benull
, but emitted items cannot benull
. A failure is propagated downstream if the function throws an exception.Items are being generated based on subscription requests. Requesting
Long.MAX_VALUE
items can possibly make for an infinite stream unless the generator function callsGeneratorEmitter.complete()
at some point.- Type Parameters:
S
- the state typeT
- the items type- Parameters:
initialStateSupplier
- a supplier for the initial state, must not benull
but can supplynull
generator
- the generator function, returns the new state for the next item generation- Returns:
- a new
Multi
-
-