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> ExecutionFlow<T> async(Executor executor, Supplier<? extends ExecutionFlow<T>> supplier) Create a flow by invoking a supplier asynchronously.default voidcancel()Send an optional hint to the upstream producer that the result of this flow is no longer needed and can be discarded.voidcompleteTo(CompletableFuture<T> completableFuture) Completes the flow to the completable future.static <T> ExecutionFlow<T> empty()Create a simple flow representing an empty state.static <K> ExecutionFlow<K> Create a simple flow representing an error.<R> ExecutionFlow<R> flatMap(Function<? super T, ? extends ExecutionFlow<? extends R>> transformer) Map a not-empty value to a new flow.static <K> ExecutionFlow<K> just(@Nullable K value) Create a simple flow representing a value.<R> ExecutionFlow<R> Map a not-empty value.voidonComplete(BiConsumer<? super T, Throwable> fn) Invokes a provided function when the flow is resolved, or immediately if it is already done.onErrorResume(Function<? super Throwable, ? extends ExecutionFlow<? extends T>> fallback) Supply a new flow if the existing flow is erroneous.putInContext(String key, Object value) Store a contextual value.default ExecutionFlow<T> putInContextIfAbsent(String key, Object value) Store a contextual value if it is absent.<R> ExecutionFlow<R> then(Supplier<? extends ExecutionFlow<? extends R>> supplier) Supply a new flow after the existing flow value is resolved.default ExecutionFlow<T> timeout(Duration timeout, ScheduledExecutorService scheduler, @Nullable BiConsumer<T, Throwable> onDiscard) Create a newExecutionFlowthat either returns the same result or, if the timeout expires before the result is received, aTimeoutException.default CompletableFuture<T> Converts the existing flow into the completable future.@Nullable ImperativeExecutionFlow<T> Create anImperativeExecutionFlowfrom this execution flow, if possible.default @Nullable ThrowableAlternative totryComplete()which will unwrap the flow's error.default @Nullable TAlternative 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
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
Map a not-empty value.- Type Parameters:
R- New value Type- Parameters:
transformer- The value transformer- Returns:
- a new flow
-
flatMap
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
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
ExecutionFlow<T> onErrorResume(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
Store a contextual value.- Parameters:
key- The keyvalue- The value- Returns:
- a new flow
-
putInContextIfAbsent
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
default ExecutionFlow<T> timeout(Duration timeout, ScheduledExecutorService scheduler, @Nullable BiConsumer<T, Throwable> onDiscard) Create a newExecutionFlowthat 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
@Nullable ImperativeExecutionFlow<T> tryComplete()Create anImperativeExecutionFlowfrom this execution flow, if possible. The flow will have its result immediately available.- Returns:
- The imperative flow, or
nullif 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
nullif 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
nullif 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
CompletableFuturethat 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 listenerfor cleanup, in case the upstream producer does not support cancellation or has already submitted the result.- Since:
- 4.8.0
-