Class DurableFuture<T>

java.lang.Object
dev.restate.sdk.DurableFuture<T>
Type Parameters:
T - type of the future result
Direct Known Subclasses:
Awakeable, CallDurableFuture, Select

public abstract class DurableFuture<T> extends Object
A 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 Details

    • DurableFuture

      public DurableFuture()
  • Method Details

    • asyncResult

      protected abstract dev.restate.sdk.endpoint.definition.AsyncResult<T> asyncResult()
    • serviceExecutor

      protected abstract Executor serviceExecutor()
    • await

      public final T await() throws dev.restate.sdk.common.TerminalException
      Wait for this DurableFuture 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 catch AbortedExecutionException as well, which will prevent the service invocation to orderly suspend.

      Throws:
      dev.restate.sdk.common.TerminalException - if this future was completed with a failure
    • await

      public final T await(Duration timeout) throws dev.restate.sdk.common.TerminalException
      Same as await(), but throws a TimeoutException if this DurableFuture doesn't complete before the provided timeout.
      Throws:
      dev.restate.sdk.common.TerminalException
    • withTimeout

      public final DurableFuture<T> withTimeout(Duration timeout)
      Returns:
      a DurableFuture that throws a TimeoutException if this future doesn't complete before the provided timeout.
    • map

      public final <U> DurableFuture<U> map(dev.restate.common.function.ThrowingFunction<T,U> mapper)
      Map the success result of this DurableFuture.
      Parameters:
      mapper - the mapper to execute if this DurableFuture completes with success. The mapper can throw a TerminalException, thus failing the resulting DurableFuture.
      Returns:
      a new DurableFuture with the mapped result, when completed
    • map

      public final <U> DurableFuture<U> map(dev.restate.common.function.ThrowingFunction<T,U> successMapper, dev.restate.common.function.ThrowingFunction<dev.restate.sdk.common.TerminalException,U> failureMapper)
      Map both the success and the failure result of this DurableFuture.
      Parameters:
      successMapper - the mapper to execute if this DurableFuture completes with success. The mapper can throw a TerminalException, thus failing the resulting DurableFuture.
      failureMapper - the mapper to execute if this DurableFuture completes with failure. The mapper can throw a TerminalException, thus failing the resulting DurableFuture.
      Returns:
      a new DurableFuture with the mapped result, when completed
    • mapFailure

      public final DurableFuture<T> mapFailure(dev.restate.common.function.ThrowingFunction<dev.restate.sdk.common.TerminalException,T> failureMapper)
      Map the failure result of this DurableFuture.
      Parameters:
      failureMapper - the mapper to execute if this DurableFuture completes with failure. The mapper can throw a TerminalException, thus failing the resulting DurableFuture.
      Returns:
      a new DurableFuture with the mapped result, when completed
    • any

      public static DurableFuture<Integer> any(DurableFuture<?> first, DurableFuture<?> second, DurableFuture<?>... others)
      Create an DurableFuture that awaits any of the given futures. The resulting DurableFuture returns the index of the completed future in the provided list.
      See Also:
    • any

      public static DurableFuture<Integer> any(List<DurableFuture<?>> durableFutures)
      Create an DurableFuture that awaits any of the given futures. The resulting DurableFuture 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 an DurableFuture that awaits all the given futures.

      The behavior is the same as CompletableFuture.allOf(CompletableFuture[]).

    • all

      public static DurableFuture<Void> all(List<DurableFuture<?>> durableFutures)
      Create an DurableFuture 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[]).