- All Implemented Interfaces:
Uni<T>
- Direct Known Subclasses:
UniOperator
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionawait()Awaits (blocking the caller thread) until the item or a failure is emitted by the observedUni.awaitUsing(Context context) Awaits (blocking the caller thread) until the item or a failure is emitted by the observedUni.cache()convert()Converts anUnito other types such asCompletionStageProduces a newUniinvoking theUniSubscriber.onItem(Object)andUniSubscriber.onFailure(Throwable)on the suppliedExecutor.ifNoItem()Produces aUnireacting when a no item event is fired by the upstream uni during the specified time period.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.memoize()Configure memoization of theUniitem or failure.Configures actions to be performed when the subscriber cancels the subscription.LikeUni.onFailure(Predicate)but applied to all failures fired by the upstream uni.<E extends Throwable>
UniOnFailure<T, E> Configures a type of failure filtering the failures on which the behavior (specified with the returnedUniOnFailure) is applied.Configures a predicate filtering the failures on which the behavior (specified with the returnedUniOnFailure) is applied.onItem()Configures the action to execute when the observedUniemits the item (potentiallynull).Configures the action to execute when the observedUniemits either an item (potentiallynull)) or a failure.Configures the action to execute when the observedUnisends aUniSubscription.Configures actions to be performed on termination, that is, on item, on failure, or when the subscriber cancels the subscription.repeat()Allows configuring repeating behavior.runSubscriptionOn(Executor executor) Requests theUnito start resolving the item and allows configuring how the signals are propagated (using aUniSubscriber, callbacks, or aCompletionStage.abstract voidsubscribe(UniSubscriber<? super T> subscriber) static <T> voidsubscribe(Uni<? extends T> upstream, UniSubscriber<? super T> subscriber) Encapsulates subscription to slightly optimize the AbstractUni case.toMulti()<R> Uni<R> withContext(BiFunction<Uni<T>, Context, Uni<R>> builder) Materialize the subscriberContextfor a sub-pipeline.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.smallrye.mutiny.Uni
attachContext, call, call, chain, chain, eventually, eventually, flatMap, invoke, invoke, map, plug, replaceIfNullWith, replaceIfNullWith, replaceWith, replaceWith, replaceWith, replaceWithNull, replaceWithVoid, stage, subscribeAsCompletionStage, subscribeAsCompletionStage
-
Constructor Details
-
AbstractUni
public AbstractUni()
-
-
Method Details
-
subscribe
-
subscribe
Encapsulates subscription to slightly optimize the AbstractUni case.In the case of AbstractUni, it avoid creating the UniSubscribe group instance.
- Type Parameters:
T- the type of item- Parameters:
upstream- the upstream, must not benull(not checked)subscriber- the subscriber, must not benull(not checked)
-
subscribe
Description copied from interface:UniRequests theUnito start resolving the item and allows configuring how the signals are propagated (using aUniSubscriber, callbacks, or aCompletionStage. UnlikeUni.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. -
onItem
Description copied from interface:UniConfigures the action to execute when the observedUniemits the item (potentiallynull).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) -
ifNoItem
Description copied from interface:UniProduces aUnireacting when a no item event is fired by the upstream uni during the specified time period.This
Unidetects if thisUnidoes 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)).fail().onFailure().retry().atMost(5) // Retry five times -
onFailure
Description copied from interface:UniLikeUni.onFailure(Predicate)but applied to all failures fired by the upstream uni. It allows configuring the on failure behavior (recovery, retry...). -
onFailure
Description copied from interface:UniConfigures a predicate filtering the failures on which the behavior (specified with the returnedUniOnFailure) is applied.For instance, to only when an
IOExceptionis 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 typeIOException. -
onFailure
Description copied from interface:UniConfigures a type of failure filtering the failures on which the behavior (specified with the returnedUniOnFailure) is applied.For instance, to only when an
IOExceptionis 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 typeIOException. -
onSubscription
Description copied from interface:UniConfigures the action to execute when the observedUnisends aUniSubscription. The downstream does not have a subscription yet. It will be passed once the configured action completes.Example:
uni.onSubscription().invoke(sub -> System.out.println("subscribed")); // Delay the subscription by 1 second (or until an asynchronous action completes) uni.onSubscription().call(sub -> Uni.createFrom(1).onItem().delayIt().by(Duration.ofSecond(1)));- Specified by:
onSubscriptionin interfaceUni<T>- Returns:
- the object to configure the action to execution on subscription.
-
onItemOrFailure
Description copied from interface:UniConfigures the action to execute when the observedUniemits either an item (potentiallynull)) or a failure. UnlikeUni.onItem()andUni.onFailure()the action would handle both cases in on "go".- Specified by:
onItemOrFailurein interfaceUni<T>- Returns:
- the object to configure the action to execute when an item is emitted or when a failure is propagated.
-
await
Description copied from interface:UniAwaits (blocking the caller thread) until the item or a failure is emitted by the observedUni. If the observed uni fails, the failure is thrown. In the case of a checked exception, the exception is wrapped into aCompletionException.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 -
awaitUsing
Description copied from interface:UniAwaits (blocking the caller thread) until the item or a failure is emitted by the observedUni. If the observed uni fails, the failure is thrown. In the case of a checked exception, the exception is wrapped into aCompletionException.Examples:
Uni<T> uni = ...; T res = uni.awaitUsing(context).indefinitely(); // Await indefinitely until it get the item. T res = uni.awaitUsing(context).atMost(Duration.ofMillis(1000)); // Awaits at most 1s. After that, a TimeoutException is thrown Optional<T> res = uni.awaitUsing(context).asOptional().indefinitely(); // Retrieves the item as an Optional, empty if the item is null- Specified by:
awaitUsingin interfaceUni<T>- Parameters:
context- the context, cannot benull- Returns:
- the object to configure the retrieval.
-
emitOn
Description copied from interface:UniProduces a newUniinvoking theUniSubscriber.onItem(Object)andUniSubscriber.onFailure(Throwable)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.Be careful as this operator can lead to concurrency problems with non thread-safe objects such as CDI request-scoped beans.
-
runSubscriptionOn
Description copied from interface:UniWhen a subscriber subscribes to thisUni, executes the subscription to the upstreamUnion a thread from the given executor. As a result, theUniSubscriber.onSubscribe(UniSubscription)method will be called on this thread (except mentioned otherwise)- Specified by:
runSubscriptionOnin interfaceUni<T>- Parameters:
executor- the executor to use, must not benull- Returns:
- a new
Uni
-
memoize
Description copied from interface:UniConfigure memoization of theUniitem or failure. -
cache
-
convert
Description copied from interface:UniConverts anUnito other types such asCompletionStageExamples:
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 -
toMulti
Description copied from interface:UniCreates an instance ofMultifrom thisUni.When a subscriber subscribes to the returned
Multiand request an item, it subscribes to thisUniand the events from thisUniare propagated to theMulti:- if this
Uniemits a non-nullitem - this item is propagated to theMultiand followed with the completion event - if this
Uniemits anullitem - theMultifires the completion event - if this
Uniemits a failure, this failure event is propagated by theMulti
It's important to note that the subscription to this
Unihappens when the subscriber to the producedMultirequests items, and not at subscription time. - if this
-
repeat
Description copied from interface:Uni -
onTermination
Description copied from interface:UniConfigures actions to be performed on termination, that is, on item, on failure, or when the subscriber cancels the subscription.- Specified by:
onTerminationin interfaceUni<T>- Returns:
- the object to configure the termination actions.
-
onCancellation
Description copied from interface:UniConfigures actions to be performed when the subscriber cancels the subscription.- Specified by:
onCancellationin interfaceUni<T>- Returns:
- the object to configure the cancellation actions.
-
log
Description copied from interface:UniLog events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.Events will be logged as long as the
Unihasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructureclass. -
log
Description copied from interface:UniLog 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
Unihasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructureclass. -
withContext
Description copied from interface:UniMaterialize the subscriberContextfor a sub-pipeline.The provided function takes this
Uniand theContextas parameters, and returns aUnito build the sub-pipeline, as in:someUni.withContext((uni, ctx) -> uni.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.
-