Package io.micronaut.core.execution
Interface ExecutionFlow<T>
- Type Parameters:
T
- The flow type
- All Known Subinterfaces:
CompletableFutureExecutionFlow<T>
,DelayedExecutionFlow<T>
,ImperativeExecutionFlow<T>
,ReactiveExecutionFlow<T>
The execution flow class represents a data flow which state can be represented as a simple imperative flow or an async/reactive.
The state can be resolved or lazy - based on the implementation.
NOTE: The instance of the flow is not supposed to be used after a mapping operator is used.
- Since:
- 4.0.0
- Author:
- Denis Stepanov
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> @NonNull ExecutionFlow<T>
Create a flow by invoking a supplier asynchronously.default void
cancel()
Send an optional hint to the upstream producer that the result of this flow is no longer needed and can be discarded.void
completeTo
(@NonNull CompletableFuture<T> completableFuture) Completes the flow to the completable future.static <T> @NonNull ExecutionFlow<T>
empty()
Create a simple flow representing an empty state.static <K> @NonNull ExecutionFlow<K>
Create a simple flow representing an error.<R> @NonNull ExecutionFlow<R>
flatMap
(@NonNull Function<? super T, ? extends ExecutionFlow<? extends R>> transformer) Map a not-empty value to a new flow.static <K> @NonNull ExecutionFlow<K>
just
(K value) Create a simple flow representing a value.<R> @NonNull ExecutionFlow<R>
Map a not-empty value.void
onComplete
(@NonNull BiConsumer<? super T, Throwable> fn) Invokes a provided function when the flow is resolved, or immediately if it is already done.onErrorResume
(@NonNull Function<? super Throwable, ? extends ExecutionFlow<? extends T>> fallback) Supply a new flow if the existing flow is erroneous.putInContext
(@NonNull String key, @NonNull Object value) Store a contextual value.default @NonNull ExecutionFlow<T>
putInContextIfAbsent
(@NonNull String key, @NonNull Object value) Store a contextual value if it is absent.<R> @NonNull ExecutionFlow<R>
then
(@NonNull Supplier<? extends ExecutionFlow<? extends R>> supplier) Supply a new flow after the existing flow value is resolved.default @NonNull ExecutionFlow<T>
timeout
(@NonNull Duration timeout, @NonNull ScheduledExecutorService scheduler, @Nullable BiConsumer<T, Throwable> onDiscard) Create a newExecutionFlow
that either returns the same result or, if the timeout expires before the result is received, aTimeoutException
.default @NonNull CompletableFuture<T>
Converts the existing flow into the completable future.Create anImperativeExecutionFlow
from this execution flow, if possible.Alternative totryComplete()
which will unwrap the flow's error.default T
Alternative totryComplete()
which will unwrap the flow's value.
-
Method Details
-
just
Create a simple flow representing a value.- Type Parameters:
K
- The value type- Parameters:
value
- The value- Returns:
- a new flow
-
error
Create a simple flow representing an error.- Type Parameters:
K
- The value type- Parameters:
e
- The exception- Returns:
- a new flow
-
empty
Create a simple flow representing an empty state.- Type Parameters:
T
- The flow value type- Returns:
- a new flow
-
async
@NonNull static <T> @NonNull ExecutionFlow<T> async(@NonNull @NonNull Executor executor, @NonNull @NonNull Supplier<? extends ExecutionFlow<T>> supplier) Create a flow by invoking a supplier asynchronously.- Type Parameters:
T
- The flow value type- Parameters:
executor
- The executorsupplier
- The supplier- Returns:
- a new flow
-
map
@NonNull <R> @NonNull ExecutionFlow<R> map(@NonNull @NonNull Function<? super T, ? extends R> transformer) Map a not-empty value.- Type Parameters:
R
- New value Type- Parameters:
transformer
- The value transformer- Returns:
- a new flow
-
flatMap
@NonNull <R> @NonNull ExecutionFlow<R> flatMap(@NonNull @NonNull Function<? super T, ? extends ExecutionFlow<? extends R>> transformer) Map a not-empty value to a new flow.- Type Parameters:
R
- New value Type- Parameters:
transformer
- The value transformer- Returns:
- a new flow
-
then
@NonNull <R> @NonNull ExecutionFlow<R> then(@NonNull @NonNull Supplier<? extends ExecutionFlow<? extends R>> supplier) Supply a new flow after the existing flow value is resolved.- Type Parameters:
R
- New value Type- Parameters:
supplier
- The supplier- Returns:
- a new flow
-
onErrorResume
@NonNull @NonNull ExecutionFlow<T> onErrorResume(@NonNull @NonNull Function<? super Throwable, ? extends ExecutionFlow<? extends T>> fallback) Supply a new flow if the existing flow is erroneous.- Parameters:
fallback
- The fallback- Returns:
- a new flow
-
putInContext
@NonNull @NonNull ExecutionFlow<T> putInContext(@NonNull @NonNull String key, @NonNull @NonNull Object value) Store a contextual value.- Parameters:
key
- The keyvalue
- The value- Returns:
- a new flow
-
putInContextIfAbsent
@NonNull default @NonNull ExecutionFlow<T> putInContextIfAbsent(@NonNull @NonNull String key, @NonNull @NonNull Object value) Store a contextual value if it is absent.- Parameters:
key
- The keyvalue
- The value- Returns:
- a new flow
- Since:
- 4.8.0
-
onComplete
Invokes a provided function when the flow is resolved, or immediately if it is already done.- Parameters:
fn
- The function
-
completeTo
Completes the flow to the completable future.- Parameters:
completableFuture
- The completable future- Since:
- 4.8
-
timeout
@NonNull default @NonNull ExecutionFlow<T> timeout(@NonNull @NonNull Duration timeout, @NonNull @NonNull ScheduledExecutorService scheduler, @Nullable @Nullable BiConsumer<T, Throwable> onDiscard) Create a newExecutionFlow
that either returns the same result or, if the timeout expires before the result is received, aTimeoutException
.- Parameters:
timeout
- The timeoutscheduler
- Scheduler to schedule the timeout taskonDiscard
- An optional consumer to be called on the value of this flow if the flow completes after the timeout has expired and thus the value is discarded- Returns:
- A new flow that will produce either the same value or a
TimeoutException
-
tryComplete
Create anImperativeExecutionFlow
from this execution flow, if possible. The flow will have its result immediately available.- Returns:
- The imperative flow, or
null
if this flow is not complete or does not support this operation
-
tryCompleteValue
Alternative totryComplete()
which will unwrap the flow's value.- Returns:
- The imperative flow then returns its value, or
null
if this flow is not complete or does not support this operation - Since:
- 4.3
-
tryCompleteError
Alternative totryComplete()
which will unwrap the flow's error.- Returns:
- The imperative flow then returns its error, or
null
if this flow is not complete or does not support this operation - Since:
- 4.3
-
toCompletableFuture
Converts the existing flow into the completable future.- Returns:
- a
CompletableFuture
that represents the state if this flow.
-
cancel
default void cancel()Send an optional hint to the upstream producer that the result of this flow is no longer needed and can be discarded. This is an optional operation, and has no effect if the flow has already completed. After a cancellation, a flow might never complete.If this flow contains a resource that needs to be cleaned up (e.g. an
InputStream
), the caller should still add acompletion listener
for cleanup, in case the upstream producer does not support cancellation or has already submitted the result.- Since:
- 4.8.0
-