Class Select<T>
java.lang.Object
dev.restate.sdk.Select<T>
- Type Parameters:
T- the output value
- All Implemented Interfaces:
DurableFuture<T>
Select lets you await concurrently for multiple
DurableFutures to complete, and for the
first one to complete, either return its value directly or map it.
Example:
// Using awakeables as example here
var a1 = ctx.awakeable(String.class);
var a2 = ctx.awakeable(MyObject.class);
var a3 = ctx.awakeable(String.class);
var result = Select.<String>select()
// Just select the a1 as is
.or(a1)
// When selecting a2, map the success result
.when(a2, myObject -> myObject.toString())
// When selecting a3, map failure as another failure
.when(a3, ThrowingFunction.identity(), ex -> {
throw new TerminalException("a3 failed, too bad!");
})
// Finally await for the result
.await();
-
Method Summary
Modifier and TypeMethodDescriptionfinal Tawait()Wait for thisDurableFutureto complete.final TSame asDurableFuture.await(), but throws aTimeoutExceptionif thisDurableFuturedoesn'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.or(DurableFuture<T> durableFuture) Add the givenDurableFutureto this select.static <T> Select<T> select()Create a newSelectoperation.when(DurableFuture<U> durableFuture, ThrowingFunction<U, T> successMapper) Add the givenDurableFutureto this select.when(DurableFuture<U> durableFuture, ThrowingFunction<U, T> successMapper, ThrowingFunction<TerminalException, T> failureMapper) Add the givenDurableFutureto this select.final DurableFuture<T> withTimeout(Duration timeout)
-
Method Details
-
select
-
or
Add the givenDurableFutureto this select.- Returns:
- this, so it can be used fluently.
-
when
Add the givenDurableFutureto this select. If it completes first, the success result will be mapped usingsuccessMapper, otherwise in case ofTerminalException, the exception will be thrown as is.- Parameters:
durableFuture- theDurableFutureto add to this selectsuccessMapper- the mapper to execute if the givenDurableFuturecompletes with success. The mapper can throw aTerminalException, thus failing the resulting operation.- Returns:
- this, so it can be used fluently.
-
when
public <U> Select<T> when(DurableFuture<U> durableFuture, ThrowingFunction<U, T> successMapper, ThrowingFunction<TerminalException, T> failureMapper) Add the givenDurableFutureto this select. If it completes first, the success result will be mapped usingsuccessMapper, otherwise in case ofTerminalException, the exception will be mapped usingfailureMapper.- Parameters:
durableFuture- theDurableFutureto add to this selectsuccessMapper- the mapper to execute if the givenDurableFuturecompletes with success. The mapper can throw aTerminalException, thus failing the resulting operation.failureMapper- the mapper to execute if the givenDurableFuturecompletes with failure. The mapper can throw aTerminalException, thus failing the resulting operation.- Returns:
- this, so it can be used fluently.
-
await
Description copied from interface:DurableFutureWait for thisDurableFutureto 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 catchAbortedExecutionExceptionas well, which will prevent the service invocation to orderly suspend.- Specified by:
awaitin interfaceDurableFuture<T>- Throws:
TerminalException- if this future was completed with a failure
-
await
Description copied from interface:DurableFutureSame asDurableFuture.await(), but throws aTimeoutExceptionif thisDurableFuturedoesn't complete before the providedtimeout.- Specified by:
awaitin interfaceDurableFuture<T>- Throws:
TerminalException
-
withTimeout
- Specified by:
withTimeoutin interfaceDurableFuture<T>- Returns:
- a
DurableFuturethat throws aTimeoutExceptionif this future doesn't complete before the providedtimeout.
-
map
Description copied from interface:DurableFutureMap the success result of thisDurableFuture.- Specified by:
mapin interfaceDurableFuture<T>- Parameters:
mapper- the mapper to execute if thisDurableFuturecompletes with success. The mapper can throw aTerminalException, thus failing the resultingDurableFuture.- Returns:
- a new
DurableFuturewith the mapped result, when completed
-
map
public final <U> DurableFuture<U> map(ThrowingFunction<T, U> successMapper, ThrowingFunction<TerminalException, U> failureMapper) Description copied from interface:DurableFutureMap both the success and the failure result of thisDurableFuture.- Specified by:
mapin interfaceDurableFuture<T>- Parameters:
successMapper- the mapper to execute if thisDurableFuturecompletes with success. The mapper can throw aTerminalException, thus failing the resultingDurableFuture.failureMapper- the mapper to execute if thisDurableFuturecompletes with failure. The mapper can throw aTerminalException, thus failing the resultingDurableFuture.- Returns:
- a new
DurableFuturewith the mapped result, when completed
-
mapFailure
Description copied from interface:DurableFutureMap the failure result of thisDurableFuture.- Specified by:
mapFailurein interfaceDurableFuture<T>- Parameters:
failureMapper- the mapper to execute if thisDurableFuturecompletes with failure. The mapper can throw aTerminalException, thus failing the resultingDurableFuture.- Returns:
- a new
DurableFuturewith the mapped result, when completed
-