Class SimpleWorkflowExecution

java.lang.Object
io.axoniq.framework.workflow.runtime.execution.SimpleWorkflowExecution
All Implemented Interfaces:
WorkflowExecution, DescribableComponent

public final class SimpleWorkflowExecution extends Object implements WorkflowExecution
Workflow instance implementation.
Since:
5.4.0
Author:
Allard Buijze, Simon Zambrovski, Stefan Dragisic, Steven van Beelen
  • Constructor Details

    • SimpleWorkflowExecution

      public SimpleWorkflowExecution(String workflowId, Map<String,@Nullable Object> initial, ProcessingContext processingContext, WorkflowConfiguration<?> workflowConfiguration, WorkflowContext workflowContext)
      Constructs a new instance.
      Parameters:
      workflowId - workflow id.
      initial - initial payload of workflow instance.
      processingContext - processing context.
      workflowConfiguration - workflow configuration.
      workflowContext - author-facing context created by the factory
  • Method Details

    • execute

      public CompletableFuture<Void> execute(Consumer<WorkflowExecution> terminationHandler)
      Executes the workflow body.

      The body runs inside a unit of work spanning the instance's entire lifetime. It is created from the non-transactional WorkflowExecutionOperationsDelegation.workflowBodyUnitOfWorkFactory() so that no transactional resources are held while the instance is parked.

      Specified by:
      execute in interface WorkflowExecution
      Parameters:
      terminationHandler - termination handler, which is executed after the execution has reached a terminal WorkflowStatus
      Returns:
      a future completing when the workflow body has stopped
    • awaitStateChange

      public void awaitStateChange(Predicate<WorkflowState> predicate) throws InterruptedException
      Description copied from interface: WorkflowExecution
      Applies queued tasks until the condition is satisfied.
      Specified by:
      awaitStateChange in interface WorkflowExecution
      Parameters:
      predicate - condition on the workflow state
      Throws:
      InterruptedException - if interrupted while waiting
    • onEvent

      public void onEvent(EventMessage eventMessage, ProcessingContext processingContext)
      Description copied from interface: WorkflowExecution
      Delivers an event to the workflow execution.
      Specified by:
      onEvent in interface WorkflowExecution
      Parameters:
      eventMessage - event message to deliver
      processingContext - processing context of the delivered event
    • stopForShutdown

      public void stopForShutdown()
      Description copied from interface: WorkflowExecution
      Interrupt all running steps without producing any step/workflow cancellation events. This method is for abrupt process-level teardown (e.g. an engine shutdown lifecycle hook): it completes in-flight step futures with a non-cancellation failure so the running step is removed from bookkeeping and no <Step>Cancelled event is published. The workflow's state in the event store is left at its most recent <Step>Started entry so the step can resume on the next app start. Safe to call from any thread. Stops only in-memory execution as part of engine shutdown.

      This operation preserves the durable workflow state for replay and produces no step or workflow cancellation events. It must never be used to cancel or otherwise terminate a workflow.

      Specified by:
      stopForShutdown in interface WorkflowExecution
    • interruptWorkflowDriver

      public void interruptWorkflowDriver()
      Description copied from interface: WorkflowExecution
      Interrupts the workflow driver so it can re-evaluate pending external control requests.

      This is a wake-up mechanism only. It does not mutate workflow state or publish workflow events.

      Specified by:
      interruptWorkflowDriver in interface WorkflowExecution
    • addCheckpointLatch

      public void addCheckpointLatch(Runnable latch)
      Description copied from interface: WorkflowExecution
      Adds a latch task to this WorkflowExecution representing a point in time when a checkpoint can be made.
      Specified by:
      addCheckpointLatch in interface WorkflowExecution
      Parameters:
      latch - the latch to run when reached in this WorkflowExecution's task queue
    • hasUnsafeCheckpointWork

      public boolean hasUnsafeCheckpointWork()
      Description copied from interface: WorkflowExecution
      Checks whether checkpoint advancement is unsafe because workflow work is active, queued, or blocked through a barrier.
      Specified by:
      hasUnsafeCheckpointWork in interface WorkflowExecution
      Returns:
      true if checkpoint advancement is unsafe, false otherwise
    • registerCheckpointWorkStateListener

      public void registerCheckpointWorkStateListener(WorkflowExecution.CheckpointWorkStateListener listener)
      Description copied from interface: WorkflowExecution
      Registers a listener for transitions of checkpoint-relevant work.
      Specified by:
      registerCheckpointWorkStateListener in interface WorkflowExecution
      Parameters:
      listener - listener to notify when checkpoint work becomes safe or unsafe
    • getNextTask

      public @Nullable Consumer<WorkflowExecution> getNextTask()
      Description copied from interface: WorkflowExecution
      Returns and removes the next queued task.
      Specified by:
      getNextTask in interface WorkflowExecution
      Returns:
      next task, or null when no task is queued
    • appendTask

      public void appendTask(Consumer<WorkflowExecution> task)
      Description copied from interface: WorkflowExecution
      Appends a task for execution by the workflow control thread.
      Specified by:
      appendTask in interface WorkflowExecution
      Parameters:
      task - task to execute
    • hasTasks

      public boolean hasTasks()
      Description copied from interface: WorkflowExecution
      Indicates whether the workflow execution has queued tasks.
      Specified by:
      hasTasks in interface WorkflowExecution
      Returns:
      true when tasks are queued
    • isRunning

      public boolean isRunning()
      Description copied from interface: WorkflowExecution
      Returns whether the workflow execution runtime is running.
      Specified by:
      isRunning in interface WorkflowExecution
      Returns:
      true if the workflow execution runtime is running
    • appendWorkflowEvent

      public CompletableFuture<Void> appendWorkflowEvent(EventMessage eventMessage, Context parentContext)
      Description copied from interface: WorkflowExecution
      Appends a workflow-owned event from the given parent context.
      Specified by:
      appendWorkflowEvent in interface WorkflowExecution
      Parameters:
      eventMessage - event to append
      parentContext - context the append unit of work derives from
      Returns:
      a future that completes when the event has been appended
    • processingContext

      public ProcessingContext processingContext()
      Description copied from interface: WorkflowExecution
      Returns the processing context of the workflow execution.
      Specified by:
      processingContext in interface WorkflowExecution
      Returns:
      processing context
    • state

      public WorkflowState state()
      Description copied from interface: WorkflowExecution
      Returns the current workflow state.
      Specified by:
      state in interface WorkflowExecution
      Returns:
      workflow state
    • workflowExecutionOperations

      public WorkflowExecutionOperations workflowExecutionOperations()
      Description copied from interface: WorkflowExecution
      Returns the runtime operations for the current execution.
      Specified by:
      workflowExecutionOperations in interface WorkflowExecution
      Returns:
      runtime operations for the current execution
    • workflowName

      public String workflowName()
      Description copied from interface: WorkflowExecution
      Returns the human-readable workflow name.
      Specified by:
      workflowName in interface WorkflowExecution
      Returns:
      returns the human-readable name of the workflow
    • workflowId

      public String workflowId()
      Description copied from interface: WorkflowExecution
      Returns the workflow identifier.
      Specified by:
      workflowId in interface WorkflowExecution
      Returns:
      unique workflow execution identifier
    • workflowConfiguration

      public WorkflowConfiguration<?> workflowConfiguration()
      Description copied from interface: WorkflowExecution
      Returns the workflow configuration.
      Specified by:
      workflowConfiguration in interface WorkflowExecution
      Returns:
      workflow configuration
    • initializeState

      public void initializeState(WorkflowState state)
      Description copied from interface: WorkflowExecution
      Initializes this newly created execution with a workflow state.
      Specified by:
      initializeState in interface WorkflowExecution
      Parameters:
      state - workflow state loaded from a repository
    • restoreAppendPosition

      public void restoreAppendPosition(@Nullable ConsistencyMarker position)
      Description copied from interface: WorkflowExecution
      Restores the position from which this execution conditions its next append.
      Specified by:
      restoreAppendPosition in interface WorkflowExecution
      Parameters:
      position - position observed while restoring the workflow
    • workflowCancellation

      public WorkflowCancellation workflowCancellation()
    • describeTo

      public void describeTo(ComponentDescriptor descriptor)
      Description copied from interface: DescribableComponent
      Describe the properties of this DescribableComponent with the given descriptor.

      Components should call the appropriate describeProperty methods on the descriptor to register their properties. The descriptor is responsible for determining how these properties are formatted and structured in the final output.

      Best Practices: As a general rule, all relevant fields of a DescribableComponent implementation should be described in this method. However, developers have discretion to include only the fields that make sense in the context. Not every field may be meaningful for description purposes, especially internal implementation details. Furthermore, components might want to expose different information based on their current state. The final decision on what properties to include lies with the person implementing the describeTo method, who should focus on providing information that is useful for understanding the component's configuration and state.

      Example implementation:

       public void describeTo(ComponentDescriptor descriptor) {
           descriptor.describeProperty("name", this.name);
           descriptor.describeProperty("enabled", this.enabled);
           descriptor.describeProperty("configuration", this.configuration); // A nested component
           descriptor.describeProperty("handlers", this.eventHandlers);      // A collection
       }
       
      Specified by:
      describeTo in interface DescribableComponent
      Parameters:
      descriptor - The component descriptor to describe this DescribableComponentn its properties in.