Class Select<T>

java.lang.Object
dev.restate.sdk.DurableFuture<T>
dev.restate.sdk.Select<T>
Type Parameters:
T - the output value

public final class Select<T> extends 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 Details

    • select

      public static <T> Select<T> select()
      Create a new Select operation.
      Type Parameters:
      T - The return of the select.
    • or

      public Select<T> or(DurableFuture<T> durableFuture)
      Add the given DurableFuture to this select.
      Returns:
      this, so it can be used fluently.
    • when

      public <U> Select<T> when(DurableFuture<U> durableFuture, dev.restate.common.function.ThrowingFunction<U,T> successMapper)
      Add the given DurableFuture to this select. If it completes first, the success result will be mapped using successMapper, otherwise in case of TerminalException, the exception will be thrown as is.
      Parameters:
      durableFuture - the DurableFuture to add to this select
      successMapper - the mapper to execute if the given DurableFuture completes with success. The mapper can throw a TerminalException, thus failing the resulting operation.
      Returns:
      this, so it can be used fluently.
    • when

      public <U> Select<T> when(DurableFuture<U> durableFuture, dev.restate.common.function.ThrowingFunction<U,T> successMapper, dev.restate.common.function.ThrowingFunction<dev.restate.sdk.common.TerminalException,T> failureMapper)
      Add the given DurableFuture to this select. If it completes first, the success result will be mapped using successMapper, otherwise in case of TerminalException, the exception will be mapped using failureMapper.
      Parameters:
      durableFuture - the DurableFuture to add to this select
      successMapper - the mapper to execute if the given DurableFuture completes with success. The mapper can throw a TerminalException, thus failing the resulting operation.
      failureMapper - the mapper to execute if the given DurableFuture completes with failure. The mapper can throw a TerminalException, thus failing the resulting operation.
      Returns:
      this, so it can be used fluently.
    • asyncResult

      protected dev.restate.sdk.endpoint.definition.AsyncResult<T> asyncResult()
      Specified by:
      asyncResult in class DurableFuture<T>
    • serviceExecutor

      protected Executor serviceExecutor()
      Specified by:
      serviceExecutor in class DurableFuture<T>