Class AbstractUni<T>
- java.lang.Object
-
- io.smallrye.mutiny.operators.AbstractUni<T>
-
- All Implemented Interfaces:
Uni<T>
- Direct Known Subclasses:
UniOperator
public abstract class AbstractUni<T> extends java.lang.Object implements Uni<T>
-
-
Constructor Summary
Constructors Constructor Description AbstractUni()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description UniAwait<T>await()Awaits (blocking the caller thread) until the item or a failure is emitted by the observedUni.UniAwait<T>awaitUsing(Context context)Awaits (blocking the caller thread) until the item or a failure is emitted by the observedUni.Uni<T>cache()UniConvert<T>convert()Converts anUnito other types such asCompletionStageUni<T>emitOn(java.util.concurrent.Executor executor)Produces a newUniinvoking theUniSubscriber.onItem(Object)andUniSubscriber.onFailure(Throwable)on the suppliedExecutor.UniIfNoItem<T>ifNoItem()Produces aUnireacting when a no item event is fired by the upstream uni during the specified time period.Uni<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".Uni<T>log(java.lang.String identifier)Log events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.UniMemoize<T>memoize()Configure memoization of theUniitem or failure.UniOnCancel<T>onCancellation()Configures actions to be performed when the subscriber cancels the subscription.UniOnFailure<T>onFailure()LikeUni.onFailure(Predicate)but applied to all failures fired by the upstream uni.UniOnFailure<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 returnedUniOnFailure) is applied.UniOnFailure<T>onFailure(java.util.function.Predicate<? super java.lang.Throwable> predicate)Configures a predicate filtering the failures on which the behavior (specified with the returnedUniOnFailure) is applied.UniOnItem<T>onItem()Configures the action to execute when the observedUniemits the item (potentiallynull).UniOnItemOrFailure<T>onItemOrFailure()Configures the action to execute when the observedUniemits either an item (potentiallynull)) or a failure.UniOnSubscribe<T>onSubscribe()Configures the action to execute when the observedUnisends aUniSubscription.UniOnSubscribe<T>onSubscription()Configures the action to execute when the observedUnisends aUniSubscription.UniOnTerminate<T>onTermination()Configures actions to be performed on termination, that is, on item, on failure, or when the subscriber cancels the subscription.UniRepeat<T>repeat()Allows configuring repeating behavior.Uni<T>runSubscriptionOn(java.util.concurrent.Executor executor)UniSubscribe<T>subscribe()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.Multi<T>toMulti()<R> Uni<R>withContext(java.util.function.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, wait
-
Methods 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
-
-
-
-
Method Detail
-
subscribe
public abstract void subscribe(UniSubscriber<? super T> subscriber)
-
subscribe
public static <T> void subscribe(Uni<? extends T> upstream, UniSubscriber<? super T> subscriber)
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
public UniSubscribe<T> 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.- Specified by:
subscribein interfaceUni<T>- Returns:
- the object to configure the subscription.
- See Also:
uni.await() for waiting (blocking the caller thread) until the resolution of the observed Uni.
-
onItem
public UniOnItem<T> 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
public UniIfNoItem<T> 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).retry().atMost(5) // Retry five times
-
onFailure
public UniOnFailure<T> 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
public UniOnFailure<T> onFailure(java.util.function.Predicate<? super java.lang.Throwable> predicate)
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
public UniOnFailure<T> onFailure(java.lang.Class<? extends java.lang.Throwable> typeOfFailure)
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.
-
onSubscribe
public UniOnSubscribe<T> onSubscribe()
Description copied from interface:UniConfigures the action to execute when the observedUnisends aUniSubscription. 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().call(sub -> Uni.createFrom(1).onItem().delayIt().by(Duration.ofSecond(1)));- Specified by:
onSubscribein interfaceUni<T>- Returns:
- the object to configure the action to execution on subscription.
-
onSubscription
public UniOnSubscribe<T> 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
public UniOnItemOrFailure<T> 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
public UniAwait<T> 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
public UniAwait<T> awaitUsing(Context context)
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
public Uni<T> emitOn(java.util.concurrent.Executor executor)
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.
-
runSubscriptionOn
public Uni<T> runSubscriptionOn(java.util.concurrent.Executor executor)
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
public UniMemoize<T> memoize()
Description copied from interface:UniConfigure memoization of theUniitem or failure.
-
convert
public UniConvert<T> 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- Specified by:
convertin interfaceUni<T>- Returns:
- the object to convert an
Uniinstance - See Also:
UniConvert
-
toMulti
public Multi<T> 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
-
onTermination
public UniOnTerminate<T> 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
public UniOnCancel<T> 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
public Uni<T> log(java.lang.String identifier)
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.- Specified by:
login interfaceUni<T>- Parameters:
identifier- an identifier of this operator to be used in log events- Returns:
- a new
Uni - See Also:
Infrastructure.setOperatorLogger(Infrastructure.OperatorLogger)
-
log
public Uni<T> 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.- Specified by:
login interfaceUni<T>- Returns:
- a new
Uni - See Also:
Uni.log(String),Infrastructure.setOperatorLogger(Infrastructure.OperatorLogger)
-
withContext
public <R> Uni<R> withContext(java.util.function.BiFunction<Uni<T>,Context,Uni<R>> builder)
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.
-
-