T - the type of item produced by the Unipublic interface Uni<T>
Uni represents a lazy asynchronous action. It follows the subscription pattern, meaning that the action
is only triggered once a UniSubscriber subscribes to the Uni.
A Uni can have two outcomes:
item event, forwarding the completion of the action (potentially null if the item
does not represent a value, but the action was completed successfully)failure event, forwarding an exception
To trigger the computation, a UniSubscriber must subscribe to the Uni. It will be notified of the outcome
once there is an item or failure event fired by the observed Uni. A subscriber receives
(asynchronously) a UniSubscription and can cancel the demand at any time. Note that cancelling after
having received the outcome is a no-op.
| Modifier and Type | Method and Description |
|---|---|
UniAndGroup<T> |
and()
Deprecated.
Use
combine() |
<T2> Uni<Tuple2<T,T2>> |
and(Uni<T2> other)
Deprecated.
Use
combine() |
UniAwait<T> |
await()
Awaits (blocking the caller thread) until the item or a failure is emitted by the observed
Uni. |
Uni<T> |
cache()
Caches the events (item or failure) of this
Uni and replays it for all further UniSubscriber. |
default <O> Uni<O> |
chain(Function<? super T,Uni<? extends O>> mapper)
One the observed
Uni emits an item, execute the given mapper. |
static UniCombine |
combine()
|
UniConvert<T> |
convert()
Converts an
Uni to other types such as CompletionStage |
static UniCreate |
createFrom()
Creates a new
Uni from various sources such as CompletionStage,
UniEmitter, direct values, Exception... |
Uni<T> |
emitOn(Executor executor)
Produces a new
Uni invoking the UniSubscriber.onItem(Object) and
UniSubscriber.onFailure(Throwable) on the supplied Executor. |
default Uni<T> |
eventually(Runnable action)
Execute an action after an item or a failure has been emitted.
|
default <O> Uni<T> |
eventually(Supplier<Uni<? extends O>> supplier)
|
default <O> Uni<O> |
flatMap(Function<? super T,Uni<? extends O>> mapper)
Transforms the received item asynchronously, forwarding the events emitted by another
Uni produced by
the given mapper. |
UniIfNoItem<T> |
ifNoItem()
Produces a
Uni reacting when a no item event is fired by the upstream uni during the specified time
period. |
default Uni<T> |
invoke(Consumer<? super T> callback)
Produces a new
Uni invoking the given callback when the item event is fired. |
default Uni<T> |
invokeUni(Function<? super T,? extends Uni<?>> action)
Produces a new
Uni invoking the given @{code action} when the item event is received. |
default <O> Uni<O> |
map(Function<? super T,? extends O> mapper)
Transforms the item (potentially null) emitted by this
Uni by applying a (synchronous) function to it. |
UniOnEvent<T> |
on()
Allows adding behavior when various type of events are emitted by the current
Uni (item, failure) or
by the subscriber (cancellation, subscription) |
UniOnFailure<T> |
onFailure()
Like
onFailure(Predicate) but applied to all failures fired by the upstream uni. |
UniOnFailure<T> |
onFailure(Class<? extends Throwable> typeOfFailure)
Configures a type of failure filtering the failures on which the behavior (specified with the returned
UniOnFailure) is applied. |
UniOnFailure<T> |
onFailure(Predicate<? super Throwable> predicate)
Configures a predicate filtering the failures on which the behavior (specified with the returned
UniOnFailure) is applied. |
UniOnItem<T> |
onItem()
Configures the action to execute when the observed
Uni emits the item (potentially null). |
UniOnItemOrFailure<T> |
onItemOrFailure()
Configures the action to execute when the observed
Uni emits either an item (potentially null))
or a failure. |
UniOnSubscribe<T> |
onSubscribe()
Configures the action to execute when the observed
Uni sends a UniSubscription. |
UniOnTerminate<T> |
onTermination()
Configures actions to be performed on termination, that is, on item, on failure, or when the subscriber cancels
the subscription.
|
UniOr<T> |
or()
Deprecated.
Use
combine() |
UniRepeat<T> |
repeat()
Allows configuring repeating behavior.
|
Uni<T> |
runSubscriptionOn(Executor executor)
|
default <O> O |
stage(Function<Uni<T>,O> stage)
Allows structuring the pipeline by creating a logic separation:
|
UniSubscribe<T> |
subscribe()
Requests the
Uni to start resolving the item and allows configuring how the signals are propagated
(using a UniSubscriber, callbacks, or a CompletionStage. |
default CompletableFuture<T> |
subscribeAsCompletionStage()
Shortcut for
UniSubscribe.asCompletionStage(). |
default Uni<T> |
subscribeOn(Executor executor)
Deprecated.
Use
runSubscriptionOn(Executor) instead |
default <O> O |
then(Function<Uni<T>,O> stage)
Deprecated.
use
stage(Function) |
default <O> Uni<O> |
then(Supplier<Uni<? extends O>> supplier)
One the observed
Uni emits an item, execute the given supplier. |
Multi<T> |
toMulti()
|
static UniCreate createFrom()
Uni from various sources such as CompletionStage,
UniEmitter, direct values, Exception...
Examples:
Uni.createFrom().item(1); // Emit 1 at subscription time
Uni.createFrom().item(() -> x); // Emit x at subscription time, the supplier is invoked for each subscription
Uni.createFrom().completionStage(cs); // Emit the item from this completion stage
Uni.createFrom().completionStage(() -> cs); // Emit the item from this completion stage, the stage is not created before subscription
Uni.createFrom().failure(exception); // Emit the failure at subscription time
Uni.createFrom().deferred(() -> Uni.from().value(x)); // Defer the uni creation until subscription. Each subscription can produce a different uni
Uni.createFrom().item(null); // Emit null at subscription time
Uni.createFrom().nothing(); // Create a Uni not emitting any signal
Uni.createFrom().publisher(publisher); // Create a Uni from a Reactive Streams Publisher
@Deprecated default <O> O then(Function<Uni<T>,O> stage)
stage(Function)
Uni uni = upstream
.then(u -> { ...})
.then(u -> { ...})
.then(u -> { ...})
With `then` you can structure and chain groups of processing.
default <O> O stage(Function<Uni<T>,O> stage)
Uni uni = upstream
.stage(u -> { ...})
.stage(u -> { ...})
.stage(u -> { ...})
With `stage` you can structure and chain groups of processing.
static UniCombine combine()
UniSubscribe<T> subscribe()
Uni to start resolving the item and allows configuring how the signals are propagated
(using a UniSubscriber, callbacks, or a CompletionStage. Unlike await(), this method
configures non-blocking retrieval of the item and failure.
Examples:
Uni<String> uni = ...;
Subscription sub = uni.subscribe().with( // The return subscription can be used to cancel the operation
item -> {}, // Callback calls on item
failure -> {} // Callback calls on failure
);
UniSubscriber<String> myUniSubscriber = ...
uni.subscribe().withSubscriber(myUniSubscriber); // Subscribes to the Uni with the passed subscriber
CompletableFuture future = uni.subscribe().asCompletableFuture(); // Get a CompletionStage receiving the item or failure
// Cancelling the returned future cancels the subscription.
uni.await() for waiting (blocking the caller thread) until the resolution of the observed Uni.default CompletableFuture<T> subscribeAsCompletionStage()
UniSubscribe.asCompletionStage().UniUniAwait<T> await()
Uni.
If the observed uni fails, the failure is thrown. In the case of a checked exception, the exception is wrapped
into a CompletionException.
Examples:
Uni<T> uni = ...;
T res = uni.await().indefinitely(); // Await indefinitely until it get the item.
T res = uni.await().atMost(Duration.ofMillis(1000)); // Awaits at most 1s. After that, a TimeoutException is thrown
Optional<T> res = uni.await().asOptional().indefinitely(); // Retrieves the item as an Optional, empty if the item is null
UniOnItem<T> onItem()
Uni emits the item (potentially null).
Examples:
Uni<T> uni = ...;
uni.onItem().transform(x -> ...); // Transform the item into another item (~ map)
uni.onItem().transformToUni(x -> ...); // Transform the item into a Uni (~ flatMap)
UniOnSubscribe<T> onSubscribe()
Uni sends a UniSubscription.
The downstream don't have a subscription yet. It will be passed once the configured action completes.
Example:
uni.onSubscribe().invoke(sub -> System.out.println("subscribed"));
// Delay the subscription by 1 second (or until an asynchronous action completes)
uni.onSubscribe().invokeUni(sub -> Uni.createFrom(1).onItem().delayIt().by(Duration.ofSecond(1)));
UniOnItemOrFailure<T> onItemOrFailure()
Uni emits either an item (potentially null))
or a failure. Unlike onItem() and onFailure() the action would handle both cases in on "go".@Deprecated UniAndGroup<T> and()
combine()unis into a joined item. This item can be a Tuple or the item of a
combinator function.
If one of the combine Uni fire a failure, the other unis are cancelled, and the resulting
Uni fires the failure. If collectFailures() is called,
it waits for the completion of all the unis before propagating the failure event. If more than one
Uni failed, a CompositeException is fired, wrapping the different collected failures.
Depending on the number of participants, the produced Tuple is
different from Tuple2 to Tuple9. For more participants,
use UniAndGroup.unis(Uni[]) or
UniAndGroup.unis(Iterable).
Uni.all() for the equivalent static operator@Deprecated <T2> Uni<Tuple2<T,T2>> and(Uni<T2> other)
combine()Uni with the item of other into a Tuple2.
If this or other fails, the other resolution is cancelled.T2 - the type to pairother - the other Uni, must not be nulland for more options on the combination of items,
Uni.all() for the equivalent static operator@Deprecated UniOr<T> or()
combine()Uni with a set of Uni passed to
UniOr.unis(Uni[]) to produce a new Uni forwarding the first event
(item or failure). It behaves like the fastest of these competing unis.
The process subscribes to the set of Uni. When one of the Uni fires an item or a failure,
the event is propagated downstream. Also the other subscriptions are cancelled.
Note that the callback from the subscriber are called on the thread used to fire the winning Uni.
Use emitOn(Executor) to change the thread.
If the subscription to the returned Uni is cancelled, the subscription to the unis from the
iterable are also cancelled.
Uni.any for a static version of this operator, like
Uni first = Uni.any().of(uni1, uni2);UniOnFailure<T> onFailure()
onFailure(Predicate) but applied to all failures fired by the upstream uni.
It allows configuring the on failure behavior (recovery, retry...).UniOnFailure<T> onFailure(Predicate<? super Throwable> predicate)
UniOnFailure) is applied.
For instance, to only when an IOException is fired as failure you can use:
uni.onFailure(IOException.class).recoverWithItem("hello")
The fallback value (hello) will only be used if the upstream uni fire a failure of type
IOException.
predicate - the predicate, null means applied to all failuresUniOnFailure<T> onFailure(Class<? extends Throwable> typeOfFailure)
UniOnFailure) is applied.
For instance, to only when an IOException is fired as failure you can use:
uni.onFailure(IOException.class).recoverWithItem("hello")
The fallback value (hello) will only be used if the upstream uni fire a failure of type
IOException.
typeOfFailure - the class of exception, must not be nullUniIfNoItem<T> ifNoItem()
Uni reacting when a no item event is fired by the upstream uni during the specified time
period.
This Uni detects if this Uni does not emit an item before the configured timeout.
Examples:
uni.ifNoItem().after(Duration.ofMillis(1000).fail() // Propagate a TimeOutException
uni.ifNoItem().after(Duration.ofMillis(1000).recoverWithValue("fallback") // Inject a fallback item on timeout
uni.ifNoItem().after(Duration.ofMillis(1000).on(myExecutor)... // Configure the executor calling on timeout actions
uni.ifNoItem().after(Duration.ofMillis(1000).retry().atMost(5) // Retry five times
Uni<T> emitOn(Executor executor)
Uni invoking the UniSubscriber.onItem(Object) and
UniSubscriber.onFailure(Throwable) on the supplied Executor.
Instead of receiving the item event on the thread firing the event, this method influences the
threading context to switch to a thread from the given executor.
executor - the executor to use, must not be nullUni@Deprecated default Uni<T> subscribeOn(Executor executor)
runSubscriptionOn(Executor) insteadUni, executes the subscription to the upstream Uni on a thread
from the given executor. As a result, the UniSubscriber.onSubscribe(UniSubscription) method will be called
on this thread (except mentioned otherwise)executor - the executor to use, must not be nullUniUni<T> runSubscriptionOn(Executor executor)
Uni, executes the subscription to the upstream Uni on a thread
from the given executor. As a result, the UniSubscriber.onSubscribe(UniSubscription) method will be called
on this thread (except mentioned otherwise)executor - the executor to use, must not be nullUniUni<T> cache()
Uni and replays it for all further UniSubscriber.default <O> Uni<O> map(Function<? super T,? extends O> mapper)
Uni by applying a (synchronous) function to it.
This method is equivalent to uni.onItem().apply(x -> ...)
For asynchronous composition, look at flatMap.O - the output typemapper - the mapper function, must not be nullUni computing an item of type <O>.default Uni<T> invoke(Consumer<? super T> callback)
Uni invoking the given callback when the item event is fired. Note that the
item can be null.
If the callback throws an exception, this exception is propagated to the downstream as failure.
This method is a shortcut on UniOnItem.invoke(Consumer)
callback - the callback, must not be nullUnidefault Uni<T> invokeUni(Function<? super T,? extends Uni<?>> action)
Uni invoking the given @{code action} when the item event is received. Note that
the received item can be null.
Unlike invoke(Consumer), the passed function returns a Uni. When the produced Uni sends
its item, this item is discarded, and the original item is forwarded downstream. If the produced
Uni fails, the failure is propagated downstream. If the callback throws an exception, this exception
is propagated downstream as failure.
This method is a shortcut on UniOnItem.invokeUni(Function)
default <O> Uni<O> flatMap(Function<? super T,Uni<? extends O>> mapper)
Uni produced by
the given mapper.
The mapper is called with the item event of the current Uni and produces an Uni, possibly
using another type of item (R). The events fired by produced Uni are forwarded to the
Uni returned by this method.
This operation is generally named flatMap.
This method is a shortcut on UniOnItem.transformToUni(Function) onItem().transformToUni(mapper)}.
default <O> Uni<O> chain(Function<? super T,Uni<? extends O>> mapper)
Uni emits an item, execute the given mapper. This mapper produces another
Uni. The downstream receives the events emitted by this produced Uni.
This operation allows chaining asynchronous operations: when the upstream completes with an item, run
the mapper and emits the item (or failure) sent by the produced Uni:
Uni<Session> uni = getSomeSession();
return uni.chain(session -> session.persist(fruit))
.chain(session -> session.flush())
.map(x -> Response.ok(fruit).status(201).build());
The mapper is called with the item event of the current Uni and produces an Uni, possibly
using another type of item (R). The events fired by produced Uni are forwarded to the
Uni returned by this method.
This operation is generally named flatMap.
This method is a shortcut for onItem().transformToUni(mapper).
O - the type of itemmapper - the function called with the item of the this Uni and producing the Uni,
must not be null, must not return null.Uni that would fire events from the uni produced by the mapper function, possibly
in an asynchronous manner.then(Supplier)default <O> Uni<O> then(Supplier<Uni<? extends O>> supplier)
Uni emits an item, execute the given supplier. Unlike chain(Function),
the received item is not required to run the supplier, and so omitted. The supplier produces another
Uni. The downstream receives the events emitted by this produced Uni.
This operation allows chaining asynchronous operation when you don't need the previous result: when the
upstream completes with an item, run the supplier and emits the item (or failure) sent by the produced
Uni:
String id = ...;
Session session = getSomeSession();
session.find(Fruit.class, id)
.chain(fruit -> session.remove(fruit)
.then(() -> session.flush());
This method is a shortcut for onItem().transformToUni(ignored -> supplier.get()).O - the type of itemsupplier - the function called when the item of the this Uni is emitted and producing the Uni,
must not be null, must not return null.Uni that would fire events from the uni produced by the mapper function, possibly
in an asynchronous manner.chain(Function)default Uni<T> eventually(Runnable action)
finally block in Java.
String id = ...;
Session session = getSomeSession();
session.find(Fruit.class, id)
.chain(fruit -> session.remove(fruit)
.then(() -> session.flush())
.eventually(() -> session.close());
This method is a shortcut for UniOnItemOrFailure.invoke(BiConsumer):
onItemOrFailure().invoke((item, err) -> action.run())
action - an action to perform, must not be null.Uni that emits events once the action has completed.onItemOrFailure()default <O> Uni<T> eventually(Supplier<Uni<? extends O>> supplier)
Uni emits an item or a failure, invoke a Uni supplier then invoke the supplied Uni.
When the supplied Uni emits an item then it is ignored, and when it emits a failure it is reported.
This is equivalent to a finally block in Java.
String id = ...;
Session session = getSomeSession();
session.find(Fruit.class, id)
.chain(fruit -> session.remove(fruit)
.eventually(() -> session.close());
This method is a shortcut for UniOnItemOrFailure.invokeUni(BiFunction):
onItemOrFailure().invokeUni((item, err) -> supplier.get())
O - the type of the itemsupplier - a Uni supplier, cannot be null and cannot return null.Uni that emits events once the supplied Uni emits an item or a failure.onItemOrFailure()UniConvert<T> convert()
Uni to other types such as CompletionStage
Examples:
uni.convert().toCompletionStage(); // Convert to CompletionStage using convenience method
uni.convert().with(BuiltinConverters.toCompletionStage()); // Convert to CompletionStage using BuiltInConverters
uni.convert().with(uni -> x); // Convert with a custom lambda converter
Uni instanceUniConvertMulti<T> toMulti()
Multi from this Uni.
When a subscriber subscribes to the returned Multi and request an item, it subscribes
to this Uni and the events from this Uni are propagated to the Multi:
Uni emits a non-null item - this item is propagated to the Multi
and followed with the completion eventUni emits a null item - the Multi fires the completion eventUni emits a failure, this failure event is propagated by the Multi
It's important to note that the subscription to this Uni happens when the subscriber to the produced
Multi requests items, and not at subscription time.
Multi, never nullUniOnEvent<T> on()
Uni (item, failure) or
by the subscriber (cancellation, subscription)UniRepeat<T> repeat()
Uni into a Multi either a specific amount of times or indefinitely.
Each time, a new subscription is attempted on the Uni.
Cancelling the subscription stops the repeating behavior.UniOnTerminate<T> onTermination()
Copyright © 2019–2020 SmallRye. All rights reserved.