Class AbstractUni<T>
- java.lang.Object
-
- io.smallrye.mutiny.operators.AbstractUni<T>
-
- All Implemented Interfaces:
Uni<T>
- Direct Known Subclasses:
UniCreateFromCompletionStage
,UniCreateFromCompletionStageWithState
,UniCreateFromDeferredSupplier
,UniCreateFromDeferredSupplierWithState
,UniCreateFromEmitterWithState
,UniCreateFromFailureSupplier
,UniCreateFromFuture
,UniCreateFromItemSupplier
,UniCreateFromItemWithState
,UniCreateFromKnownFailure
,UniCreateFromKnownItem
,UniCreateFromPublisher
,UniCreateWithEmitter
,UniJoinAll
,UniJoinFirst
,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
.Uni<T>
cache()
Caches the events (item or failure) of thisUni
and replays it for all furtherUniSubscriber
.UniConvert<T>
convert()
Converts anUni
to other types such asCompletionStage
Uni<T>
emitOn(java.util.concurrent.Executor executor)
Produces a newUni
invoking theUniSubscriber.onItem(Object)
andUniSubscriber.onFailure(Throwable)
on the suppliedExecutor
.UniIfNoItem<T>
ifNoItem()
Produces aUni
reacting 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 theUni
item 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 observedUni
emits the item (potentiallynull
).UniOnItemOrFailure<T>
onItemOrFailure()
Configures the action to execute when the observedUni
emits either an item (potentiallynull
)) or a failure.UniOnSubscribe<T>
onSubscribe()
Configures the action to execute when the observedUni
sends aUniSubscription
.UniOnSubscribe<T>
onSubscription()
Configures the action to execute when the observedUni
sends 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 theUni
to start resolving the item and allows configuring how the signals are propagated (using aUniSubscriber
, callbacks, or aCompletionStage
.abstract void
subscribe(UniSubscriber<? super T> subscriber)
static <T> void
subscribe(Uni<? extends T> upstream, UniSubscriber<? super T> subscriber)
Encapsulates subscription to slightly optimize the AbstractUni case.Multi<T>
toMulti()
-
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
call, call, chain, chain, eventually, eventually, flatMap, invoke, invoke, map, plug, replaceIfNullWith, replaceIfNullWith, replaceWith, replaceWith, replaceWith, replaceWithNull, replaceWithVoid, stage, 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:Uni
Requests theUni
to 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:
subscribe
in 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:Uni
Configures the action to execute when the observedUni
emits 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:Uni
Produces aUni
reacting when a no item event is fired by the upstream uni during the specified time period.This
Uni
detects if thisUni
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
-
onFailure
public UniOnFailure<T> onFailure()
Description copied from interface:Uni
LikeUni.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:Uni
Configures a predicate filtering the failures on which the behavior (specified with the returnedUniOnFailure
) 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 typeIOException
.
-
onFailure
public UniOnFailure<T> onFailure(java.lang.Class<? extends java.lang.Throwable> typeOfFailure)
Description copied from interface:Uni
Configures a type of failure filtering the failures on which the behavior (specified with the returnedUniOnFailure
) 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 typeIOException
.
-
onSubscribe
public UniOnSubscribe<T> onSubscribe()
Description copied from interface:Uni
Configures the action to execute when the observedUni
sends 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:
onSubscribe
in interfaceUni<T>
- Returns:
- the object to configure the action to execution on subscription.
-
onSubscription
public UniOnSubscribe<T> onSubscription()
Description copied from interface:Uni
Configures the action to execute when the observedUni
sends 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:
onSubscription
in interfaceUni<T>
- Returns:
- the object to configure the action to execution on subscription.
-
onItemOrFailure
public UniOnItemOrFailure<T> onItemOrFailure()
Description copied from interface:Uni
Configures the action to execute when the observedUni
emits either an item (potentiallynull
)) or a failure. UnlikeUni.onItem()
andUni.onFailure()
the action would handle both cases in on "go".- Specified by:
onItemOrFailure
in 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:Uni
Awaits (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
-
emitOn
public Uni<T> emitOn(java.util.concurrent.Executor executor)
Description copied from interface:Uni
Produces a newUni
invoking theUniSubscriber.onItem(Object)
andUniSubscriber.onFailure(Throwable)
on the suppliedExecutor
.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.
-
runSubscriptionOn
public Uni<T> runSubscriptionOn(java.util.concurrent.Executor executor)
Description copied from interface:Uni
When a subscriber subscribes to thisUni
, executes the subscription to the upstreamUni
on a thread from the given executor. As a result, theUniSubscriber.onSubscribe(UniSubscription)
method will be called on this thread (except mentioned otherwise)- Specified by:
runSubscriptionOn
in interfaceUni<T>
- Parameters:
executor
- the executor to use, must not benull
- Returns:
- a new
Uni
-
memoize
public UniMemoize<T> memoize()
Description copied from interface:Uni
Configure memoization of theUni
item or failure.
-
cache
public Uni<T> cache()
Description copied from interface:Uni
Caches the events (item or failure) of thisUni
and replays it for all furtherUniSubscriber
.
-
convert
public UniConvert<T> convert()
Description copied from interface:Uni
Converts anUni
to other types such asCompletionStage
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
- Specified by:
convert
in interfaceUni<T>
- Returns:
- the object to convert an
Uni
instance - See Also:
UniConvert
-
toMulti
public Multi<T> toMulti()
Description copied from interface:Uni
Creates an instance ofMulti
from thisUni
.When a subscriber subscribes to the returned
Multi
and request an item, it subscribes to thisUni
and the events from thisUni
are propagated to theMulti
:- if this
Uni
emits a non-null
item - this item is propagated to theMulti
and followed with the completion event - if this
Uni
emits anull
item - theMulti
fires the completion event - if this
Uni
emits a failure, this failure event is propagated by theMulti
It's important to note that the subscription to this
Uni
happens when the subscriber to the producedMulti
requests items, and not at subscription time. - if this
-
onTermination
public UniOnTerminate<T> onTermination()
Description copied from interface:Uni
Configures actions to be performed on termination, that is, on item, on failure, or when the subscriber cancels the subscription.- Specified by:
onTermination
in interfaceUni<T>
- Returns:
- the object to configure the termination actions.
-
onCancellation
public UniOnCancel<T> onCancellation()
Description copied from interface:Uni
Configures actions to be performed when the subscriber cancels the subscription.- Specified by:
onCancellation
in interfaceUni<T>
- Returns:
- the object to configure the cancellation actions.
-
log
public Uni<T> log(java.lang.String identifier)
Description copied from interface:Uni
Log events (onSubscribe, onItem, ...) as they come from the upstream or the subscriber.Events will be logged as long as the
Uni
hasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructure
class.- Specified by:
log
in 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:Uni
Log 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
Uni
hasn't been cancelled or terminated. Logging is framework-agnostic and can be configured in theInfrastructure
class.- Specified by:
log
in interfaceUni<T>
- Returns:
- a new
Uni
- See Also:
Uni.log(String)
,Infrastructure.setOperatorLogger(Infrastructure.OperatorLogger)
-
-