Class DurableFuture<T>
- Type Parameters:
T
- type of the future result
- Direct Known Subclasses:
Awakeable
,CallDurableFuture
,Select
DurableFuture
allows to await an asynchronous result. Once await()
is called,
the execution stops until the asynchronous result is available.
The result can be either a success or a failure. In case of a failure, await()
will
throw a TerminalException
.
NOTE: This interface MUST NOT be accessed concurrently since it can lead to different orderings of user actions, corrupting the execution of the invocation.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic DurableFuture
<Void> all
(DurableFuture<?> first, DurableFuture<?> second, DurableFuture<?>... others) Create anDurableFuture
that awaits all the given futures.static DurableFuture
<Void> all
(List<DurableFuture<?>> durableFutures) Create anDurableFuture
that awaits all the given futures.static DurableFuture
<Integer> any
(DurableFuture<?> first, DurableFuture<?> second, DurableFuture<?>... others) Create anDurableFuture
that awaits any of the given futures.static DurableFuture
<Integer> any
(List<DurableFuture<?>> durableFutures) Create anDurableFuture
that awaits any of the given futures.protected abstract AsyncResult
<T> final T
await()
Wait for thisDurableFuture
to complete.final T
Same asawait()
, but throws aTimeoutException
if thisDurableFuture
doesn't complete before the providedtimeout
.final <U> DurableFuture
<U> map
(ThrowingFunction<T, U> mapper) Map the success result of thisDurableFuture
.final <U> DurableFuture
<U> map
(ThrowingFunction<T, U> successMapper, ThrowingFunction<TerminalException, U> failureMapper) Map both the success and the failure result of thisDurableFuture
.final DurableFuture
<T> mapFailure
(ThrowingFunction<TerminalException, T> failureMapper) Map the failure result of thisDurableFuture
.protected abstract Executor
final DurableFuture
<T> withTimeout
(Duration timeout)
-
Constructor Details
-
DurableFuture
public DurableFuture()
-
-
Method Details
-
asyncResult
-
serviceExecutor
-
await
Wait for thisDurableFuture
to complete.Executing this method may trigger the suspension of the function.
NOTE: You should never wrap this function in a try-catch catching
Throwable
, as it will catchAbortedExecutionException
as well, which will prevent the service invocation to orderly suspend.- Throws:
TerminalException
- if this future was completed with a failure
-
await
Same asawait()
, but throws aTimeoutException
if thisDurableFuture
doesn't complete before the providedtimeout
.- Throws:
TerminalException
-
withTimeout
- Returns:
- a
DurableFuture
that throws aTimeoutException
if this future doesn't complete before the providedtimeout
.
-
map
Map the success result of thisDurableFuture
.- Parameters:
mapper
- the mapper to execute if thisDurableFuture
completes with success. The mapper can throw aTerminalException
, thus failing the resultingDurableFuture
.- Returns:
- a new
DurableFuture
with the mapped result, when completed
-
map
public final <U> DurableFuture<U> map(ThrowingFunction<T, U> successMapper, ThrowingFunction<TerminalException, U> failureMapper) Map both the success and the failure result of thisDurableFuture
.- Parameters:
successMapper
- the mapper to execute if thisDurableFuture
completes with success. The mapper can throw aTerminalException
, thus failing the resultingDurableFuture
.failureMapper
- the mapper to execute if thisDurableFuture
completes with failure. The mapper can throw aTerminalException
, thus failing the resultingDurableFuture
.- Returns:
- a new
DurableFuture
with the mapped result, when completed
-
mapFailure
Map the failure result of thisDurableFuture
.- Parameters:
failureMapper
- the mapper to execute if thisDurableFuture
completes with failure. The mapper can throw aTerminalException
, thus failing the resultingDurableFuture
.- Returns:
- a new
DurableFuture
with the mapped result, when completed
-
any
public static DurableFuture<Integer> any(DurableFuture<?> first, DurableFuture<?> second, DurableFuture<?>... others) Create anDurableFuture
that awaits any of the given futures. The resultingDurableFuture
returns the index of the completed future in the provided list.- See Also:
-
any
Create anDurableFuture
that awaits any of the given futures. The resultingDurableFuture
returns the index of the completed future in the provided list.An empty list is not supported and will throw
IllegalArgumentException
.- See Also:
-
all
public static DurableFuture<Void> all(DurableFuture<?> first, DurableFuture<?> second, DurableFuture<?>... others) Create anDurableFuture
that awaits all the given futures.The behavior is the same as
CompletableFuture.allOf(CompletableFuture[])
. -
all
Create anDurableFuture
that awaits all the given futures.An empty list is not supported and will throw
IllegalArgumentException
.The behavior is the same as
CompletableFuture.allOf(CompletableFuture[])
.
-