Class DurableContextImpl

java.lang.Object
software.amazon.lambda.durable.context.BaseContextImpl
software.amazon.lambda.durable.context.DurableContextImpl
All Implemented Interfaces:
AutoCloseable, BaseContext, DurableContext

public class DurableContextImpl extends BaseContextImpl implements DurableContext
User-facing API for defining durable operations within a workflow.

Provides methods for creating steps, waits, chained invokes, callbacks, and child contexts. Each method creates a checkpoint-backed operation that survives Lambda interruptions.

  • Method Details

    • createRootContext

      public static DurableContextImpl createRootContext(ExecutionManager executionManager, DurableConfig durableConfig, com.amazonaws.services.lambda.runtime.Context lambdaContext)
      Creates a root context (contextId = null)

      The context itself always has a null contextId (making it a root context).

      Parameters:
      executionManager - the execution manager
      durableConfig - the durable configuration
      lambdaContext - the Lambda context
      Returns:
      a new root DurableContext
    • createChildContext

      public DurableContextImpl createChildContext(String childContextId, String childContextName)
      Creates a child context.
      Parameters:
      childContextId - the child context's ID (the CONTEXT operation's operation ID)
      childContextName - the name of the child context
      Returns:
      a new DurableContext for the child context
    • createStepContext

      public StepContextImpl createStepContext(String stepOperationId, String stepOperationName, int attempt)
      Creates a step context for executing step operations.
      Parameters:
      stepOperationId - the ID of the step operation (used for thread registration)
      stepOperationName - the name of the step operation
      attempt - the current retry attempt number (0-based)
      Returns:
      a new StepContext instance
    • stepAsync

      public <T> DurableFuture<T> stepAsync(String name, TypeToken<T> resultType, Function<StepContext,T> func, StepConfig config)
      Description copied from interface: DurableContext
      Asynchronously executes a durable step using a TypeToken and custom configuration.

      This is the core stepAsync implementation. All other step/stepAsync overloads delegate here.

      Specified by:
      stepAsync in interface DurableContext
      Type Parameters:
      T - the result type
      Parameters:
      name - the unique operation name within this context
      resultType - the type token for deserialization of generic types
      func - the function to execute, receiving a StepContext
      config - the step configuration (retry strategy, semantics, custom SerDes)
      Returns:
      a future representing the step result
    • waitAsync

      public DurableFuture<Void> waitAsync(String name, Duration duration)
      Description copied from interface: DurableContext
      Asynchronously suspends execution for the specified duration.
      Specified by:
      waitAsync in interface DurableContext
      Parameters:
      name - the unique operation name within this context
      duration - the duration to wait
      Returns:
      a future that completes when the wait duration has elapsed
    • invokeAsync

      public <T, U> DurableFuture<T> invokeAsync(String name, String functionName, U payload, TypeToken<T> resultType, InvokeConfig config)
      Description copied from interface: DurableContext
      Asynchronously invokes another Lambda function using a TypeToken and custom configuration.

      This is the core invokeAsync implementation. All other invoke/invokeAsync overloads delegate here.

      Specified by:
      invokeAsync in interface DurableContext
      Type Parameters:
      T - the result type
      U - the payload type
      Parameters:
      name - the unique operation name within this context
      functionName - the ARN or name of the Lambda function to invoke
      payload - the input payload to send to the target function
      resultType - the type token for deserialization of generic result types
      config - the invoke configuration (custom SerDes for result and payload)
      Returns:
      a future representing the invocation result
    • createCallback

      public <T> DurableCallbackFuture<T> createCallback(String name, TypeToken<T> resultType, CallbackConfig config)
      Description copied from interface: DurableContext
      Creates a callback operation that suspends execution until an external system completes it.

      This is the core createCallback implementation. Returns a DurableCallbackFuture containing a callback ID that external systems use to report completion via the Lambda Durable API.

      Specified by:
      createCallback in interface DurableContext
      Type Parameters:
      T - the result type
      Parameters:
      name - the unique operation name within this context
      resultType - the type token for deserialization of generic result types
      config - the callback configuration (custom SerDes)
      Returns:
      a future containing the callback ID and eventual result
    • runInChildContextAsync

      public <T> DurableFuture<T> runInChildContextAsync(String name, TypeToken<T> resultType, Function<DurableContext,T> func, RunInChildContextConfig config)
      Runs a function in a child context, blocking until it completes.

      Child contexts provide isolated operation ID namespaces, allowing nested workflows to be composed without ID collisions. On replay, the child context's operations are replayed independently.

      Specified by:
      runInChildContextAsync in interface DurableContext
      Parameters:
      name - the operation name within this context
      resultType - the result class for deserialization
      func - the function to execute, receiving a child DurableContext
      config - the configuration for the child context
      Returns:
      the DurableFuture wrapping the child context result
    • mapAsync

      public <I, O> DurableFuture<MapResult<O>> mapAsync(String name, Collection<I> items, TypeToken<O> resultType, DurableContext.MapFunction<I,O> function, MapConfig config)
      Specified by:
      mapAsync in interface DurableContext
    • parallel

      public ParallelDurableFuture parallel(String name, ParallelConfig config)
      Description copied from interface: DurableContext
      Creates a ParallelDurableFuture for executing multiple branches concurrently.
      Specified by:
      parallel in interface DurableContext
      config - the parallel execution configuration
      Returns:
      a new ParallelDurableFuture for registering and executing branches
    • waitForCallbackAsync

      public <T> DurableFuture<T> waitForCallbackAsync(String name, TypeToken<T> resultType, BiConsumer<String,StepContext> func, WaitForCallbackConfig waitForCallbackConfig)
      Description copied from interface: DurableContext
      Asynchronously executes a submitter and waits for an external callback using a TypeToken and custom configuration.

      This is the core waitForCallbackAsync implementation. All other waitForCallback/waitForCallbackAsync overloads delegate here. Internally creates a child context containing a callback operation and a step that runs the submitter function.

      Specified by:
      waitForCallbackAsync in interface DurableContext
      Type Parameters:
      T - the result type
      Parameters:
      name - the unique operation name within this context
      resultType - the type token for deserialization of generic result types
      func - the submitter function, receiving the callback ID and a StepContext
      waitForCallbackConfig - the configuration for both the callback and submitter step
      Returns:
      a future representing the callback result
    • waitForConditionAsync

      public <T> DurableFuture<T> waitForConditionAsync(String name, TypeToken<T> resultType, BiFunction<T,StepContext,WaitForConditionResult<T>> checkFunc, WaitForConditionConfig<T> config)
      Description copied from interface: DurableContext
      Asynchronously polls a condition function until it signals done, using a TypeToken and custom configuration.

      This is the core waitForConditionAsync implementation. All other waitForCondition/waitForConditionAsync overloads delegate here.

      Specified by:
      waitForConditionAsync in interface DurableContext
      Type Parameters:
      T - the type of state being polled
      Parameters:
      name - the unique operation name within this context
      resultType - the type token for deserialization of generic types
      checkFunc - the function that evaluates the condition and returns a WaitForConditionResult
      config - the waitForCondition configuration (wait strategy, custom SerDes)
      Returns:
      a future representing the final state value
    • getLogger

      public DurableLogger getLogger()
      Description copied from interface: BaseContext
      Gets a logger with additional information of the current execution context.
      Specified by:
      getLogger in interface BaseContext
      Returns:
      a DurableLogger instance
    • close

      public void close()
      Clears the logger's thread properties. Called during context destruction to prevent memory leaks and ensure clean state for subsequent executions.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface BaseContext