Class BaseWorkflowContext
- All Implemented Interfaces:
WorkflowContext
- Direct Known Subclasses:
SimpleWorkflowContext
This context exposes the core primitives used by workflow authors: executing external work, waiting for events,
mutating workflow payload, publishing events, sleeping, failing, and cancelling. Non-blocking methods return a
WorkflowStepResult that can be awaited later, while methods prefixed with await block until the step
completes or fails.
- Since:
- 5.4.0
- Author:
- Simon Zambrovski, Stefan Dragisic, Allrad Buijze, Steven van Beelen
-
Constructor Summary
ConstructorsConstructorDescriptionBaseWorkflowContext(String workflowId, Map<String, @Nullable Object> payload, ProcessingContext processingContext, WorkflowConfiguration<?> workflowConfiguration) Creates a workflow context for a single workflow instance. -
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Tapply(T stepDefinition, UnaryOperator<T> customizer) Applies a customizer to a step definition after checking both arguments fornull.awaitEvent(String stepName, EventCondition eventCondition) Starts a wait-for step with default settings and blocks until a matching event arrives.awaitEvent(String stepName, EventCondition eventCondition, UnaryOperator<WaitForStepDefinition> customizer) Starts a wait-for step and blocks until a matching event arrives.awaitExecute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor) Starts an execute step with default settings and blocks until it completes.awaitExecute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor, UnaryOperator<ExecuteStepDefinition> customizer) Starts an execute step and blocks until it completes.voidawaitModifyPayload(String stepName, PayloadModification modification) Updates the workflow payload with default settings and blocks until the change has been applied.voidawaitModifyPayload(String stepName, PayloadModification modification, UnaryOperator<PayloadStepDefinition> customizer) Updates the workflow payload and blocks until the change has been applied.voidawaitPublish(String stepName, EventMessage event, UnaryOperator<PublishStepDefinition> customizer) Publishes a business event as a durable workflow step and blocks until the step is recorded.voidawaitSleep(String stepName, UnaryOperator<WaitForStepDefinition> customizer) Starts a sleep step and blocks until the timeout expires.voidcancel()Cancels the workflow using the default cancellation step definition.voidcancel(UnaryOperator<CancelWorkflowDefinition> customizer) Cancels the workflow and publishes the configured cancellation event.voidcancelStep(String stepName) Cancels a single running step using the default step cancellation definition.voidcancelStep(String stepName, UnaryOperator<CancelStepDefinition> customizer) Cancels a single running step without terminating the workflow.defaultCancelStepDefinition(String stepName) Creates the default single-step cancellation definition.Creates the default workflow cancellation definition.defaultExecuteStepDefinition(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor) Creates the default execute step definition used by the convenience overloads in this class.Creates the default workflow failure definition.protected VersionStepDefinitiondefaultMigrateVersionStepDefinition(String changeId, String newVersion) Creates the default version step definition used by themigrateVersion(String, String, UnaryOperator)convenience overloads.defaultPayloadStepDefinition(String stepName, PayloadModification modification) Creates the default payload modification step definition used by the convenience overloads in this class.defaultPublishStepDefinition(String stepName, EventMessage event) Creates the default publish step definition used by thepublish(String, EventMessage, UnaryOperator)convenience overloads.Returns the retry policy currently applied to newly created execute step definitions.Returns the timeout currently applied to newly created step definitions.defaultWaitForStepDefinition(String stepName, EventCondition eventCondition) Creates the default wait-for step definition used by the convenience overloads in this class.static Associations.MatcherDeprecated, for removal: This API element is subject to removal in a future version.Starts an execute step with the default step definition settings.execute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor, UnaryOperator<ExecuteStepDefinition> customizer) Starts an execute step that invokes the given payload processor.voidFails the workflow using the default failure step definition.voidfail(Throwable cause, UnaryOperator<FailWorkflowDefinition> customizer) Fails the workflow and publishes the configured failure event.static ValueRetrievermetadataProperty(String propertyName) Deprecated, for removal: This API element is subject to removal in a future version.useEventAssociationsUtils.metadataProperty(String)instead.booleanmigrateVersion(String changeId, String newVersion) Migrates this workflow tonewVersionforchangeIdwith default settings (default event-name customizer).booleanmigrateVersion(String changeId, String newVersion, UnaryOperator<VersionStepDefinition> customizer) Migrates this workflow tonewVersionforchangeIdand returns whether the new branch is in effect: Already recorded for thischangeId→ returnstrueiff the recorded version is>=newVersion. Not recorded but workflow already atnewVersion→ returnstruewithout publishing a redundant step. Not recorded andnewVersionstrictly greater → publishes a migration step and returnstrue; if the replay-drift guard fires (in-flight workflow ran past this point under old code), returnsfalse. Downgrades (requested<current, no recorded step) raiseIllegalArgumentException.modifyPayload(String stepName, PayloadModification modification) Starts a payload update step with default settings.modifyPayload(String stepName, PayloadModification modification, UnaryOperator<PayloadStepDefinition> customizer) Starts a step that updates the workflow payload.static ValueRetrieverpayloadProperty(String propertyName) Deprecated, for removal: This API element is subject to removal in a future version.useEventAssociationsUtils.payloadProperty(String)instead.publish(String stepName, EventMessage event, UnaryOperator<PublishStepDefinition> customizer) Publishes a business event as a durable workflow step and returns a handle to await later.protected QualifiedNameResolves the qualified message name for the given message type.voidsetDefaultRetryPolicy(RetryPolicy defaultRetryPolicy) Sets the retry policy applied to newly created execute step definitions unless a customizer overrides it.voidsetDefaultTimeout(Duration defaultTimeout) Sets the timeout applied to newly created execute and wait-for step definitions unless a step-specific customizer overrides it.sleep(String stepName, UnaryOperator<WaitForStepDefinition> customizer) Starts a sleep step.waitForEvent(String stepName, EventCondition eventCondition) Starts a wait-for step with default settings.waitForEvent(String stepName, EventCondition eventCondition, UnaryOperator<WaitForStepDefinition> customizer) Starts a wait-for step that completes when a matching event is observed.Methods inherited from class io.axoniq.framework.workflow.runtime.execution.AbstractWorkflowContext
allMatch, anyMatch, awaitEvent, awaitExecute, awaitModifyPayload, awaitPublish, cancel, cancelStep, containsStep, execute, fail, getStep, migrateVersion, modifyPayload, noneMatch, processingContext, publish, resolveComponent, waitForEvent, workflowDefinitionId, workflowId, workflowPayload, workflowStatus, workflowStepNames
-
Constructor Details
-
BaseWorkflowContext
public BaseWorkflowContext(String workflowId, Map<String, @Nullable Object> payload, ProcessingContext processingContext, WorkflowConfiguration<?> workflowConfiguration) Creates a workflow context for a single workflow instance.Applications normally obtain this context from the runtime through a
WorkflowDefinitioncallback instead of constructing it directly.- Parameters:
workflowId- unique identifier of the workflow instancepayload- initial workflow payloadprocessingContext- processing context for the current messageworkflowConfiguration- runtime configuration for this workflow
-
-
Method Details
-
equalsTo
@Deprecated(since="0.2.0", forRemoval=true) public static Associations.Matcher equalsTo(Object value) Deprecated, for removal: This API element is subject to removal in a future version.useEventAssociationsUtils.equalsTo(Object)instead.Creates a matcher that requires an event association value to equal the given value.- Parameters:
value- expected association value- Returns:
- matcher that can be used when building event conditions
-
payloadProperty
@Deprecated(since="0.2.0", forRemoval=true) public static ValueRetriever payloadProperty(String propertyName) Deprecated, for removal: This API element is subject to removal in a future version.useEventAssociationsUtils.payloadProperty(String)instead.Creates a payload-property association source for the simple association DSL.- Parameters:
propertyName- payload property name- Returns:
- payload-property retriever
-
metadataProperty
@Deprecated(since="0.2.0", forRemoval=true) public static ValueRetriever metadataProperty(String propertyName) Deprecated, for removal: This API element is subject to removal in a future version.useEventAssociationsUtils.metadataProperty(String)instead.Creates a metadata-property association source for the simple association DSL.- Parameters:
propertyName- metadata key- Returns:
- metadata-property retriever
-
apply
Applies a customizer to a step definition after checking both arguments fornull. Further customization is possible using the providedcustomizerunary operator.- Type Parameters:
T- concrete step definition type- Parameters:
stepDefinition- definition to customizecustomizer- function that mutates or replaces the definition- Returns:
- customized step definition
-
execute
public WorkflowStepResult execute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor, UnaryOperator<ExecuteStepDefinition> customizer) Starts an execute step that invokes the given payload processor.The step is scheduled immediately and returns a handle that can be awaited later. Use the
inputPayloadto provide step-local input in addition to the current workflow payload. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the stepinputPayload- step-specific input valuesprocessor- function that performs the step workcustomizer- customizer for timeout, mapping, retries, or metadata- Returns:
- handle for the running step
-
execute
public WorkflowStepResult execute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor) Starts an execute step with the default step definition settings.- Parameters:
stepName- logical name of the stepinputPayload- step-specific input valuesprocessor- function that performs the step work- Returns:
- handle for the running step
-
awaitExecute
public Map<String,@Nullable Object> awaitExecute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor, UnaryOperator<ExecuteStepDefinition> customizer) Starts an execute step and blocks until it completes. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the stepinputPayload- step-specific input valuesprocessor- function that performs the step workcustomizer- customizer for timeout, mapping, retries, or metadata- Returns:
- payload produced by the completed step
- See Also:
-
awaitExecute
public Map<String,@Nullable Object> awaitExecute(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor) Starts an execute step with default settings and blocks until it completes.- Parameters:
stepName- logical name of the stepinputPayload- step-specific input valuesprocessor- function that performs the step work- Returns:
- payload produced by the completed step
- See Also:
-
waitForEvent
public WorkflowStepResult waitForEvent(String stepName, EventCondition eventCondition, UnaryOperator<WaitForStepDefinition> customizer) Starts a wait-for step that completes when a matching event is observed. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the waiting stepeventCondition- event selection criteriacustomizer- customizer for timeout, mapping, or metadata- Returns:
- handle for the waiting step
-
waitForEvent
Starts a wait-for step with default settings.- Parameters:
stepName- logical name of the waiting stepeventCondition- event selection criteria- Returns:
- handle for the waiting step
-
awaitEvent
public Map<String,@Nullable Object> awaitEvent(String stepName, EventCondition eventCondition, UnaryOperator<WaitForStepDefinition> customizer) Starts a wait-for step and blocks until a matching event arrives. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the waiting stepeventCondition- event selection criteriacustomizer- customizer for timeout, mapping, or metadata- Returns:
- payload extracted from the matching event
- See Also:
-
awaitEvent
Starts a wait-for step with default settings and blocks until a matching event arrives.- Parameters:
stepName- logical name of the waiting stepeventCondition- event selection criteria- Returns:
- payload extracted from the matching event
- See Also:
-
sleep
Starts a sleep step. Further customization is possible using the providedcustomizerunary operator.The actual delay is usually configured through the supplied customizer by setting a timeout on the step definition.
- Parameters:
stepName- logical name of the sleep stepcustomizer- customizer that typically sets the timeout- Returns:
- handle for the sleeping step
-
awaitSleep
Starts a sleep step and blocks until the timeout expires. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the sleep stepcustomizer- customizer that typically sets the timeout- See Also:
-
modifyPayload
public WorkflowStepResult modifyPayload(String stepName, PayloadModification modification, UnaryOperator<PayloadStepDefinition> customizer) Starts a step that updates the workflow payload. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the payload update stepmodification- transformation to apply to the current payloadcustomizer- customizer for step metadata- Returns:
- handle for the payload update step
-
modifyPayload
Starts a payload update step with default settings.- Parameters:
stepName- logical name of the payload update stepmodification- transformation to apply to the current payload- Returns:
- handle for the payload update step
-
migrateVersion
public boolean migrateVersion(String changeId, String newVersion, UnaryOperator<VersionStepDefinition> customizer) Migrates this workflow tonewVersionforchangeIdand returns whether the new branch is in effect:- Already recorded for this
changeId→ returnstrueiff the recorded version is>=newVersion. - Not recorded but workflow already at
newVersion→ returnstruewithout publishing a redundant step. - Not recorded and
newVersionstrictly greater → publishes a migration step and returnstrue; if the replay-drift guard fires (in-flight workflow ran past this point under old code), returnsfalse.
<current, no recorded step) raiseIllegalArgumentException. Call at most once perchangeIdper workflow body.Use it to fork workflow logic safely:
if (ctx.migrateVersion("payment-redesign", "0.0.2")) { ctx.awaitExecute("processPayment", PaymentService::processV2); } else { ctx.awaitExecute("chargePayment", PaymentService::chargeV1); }- Parameters:
changeId- developer-chosen identifier describing the change.newVersion- new workflow version to record (semver string, e.g."0.0.2").customizer- customizer for the version step definition (e.g. event name customizer).- Returns:
trueif the workflow is at (or past)newVersionfor thischangeId;falseif it stays on the legacy branch.
- Already recorded for this
-
migrateVersion
Migrates this workflow tonewVersionforchangeIdwith default settings (default event-name customizer).- Parameters:
changeId- developer-chosen identifier describing the change.newVersion- new workflow version to record (semver string, e.g."0.0.2").- Returns:
trueif the workflow is at (or past)newVersionfor thischangeId.- See Also:
-
awaitModifyPayload
public void awaitModifyPayload(String stepName, PayloadModification modification, UnaryOperator<PayloadStepDefinition> customizer) Updates the workflow payload and blocks until the change has been applied. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the payload update stepmodification- transformation to apply to the current payloadcustomizer- customizer for step metadata- See Also:
-
awaitModifyPayload
Updates the workflow payload with default settings and blocks until the change has been applied.- Parameters:
stepName- logical name of the payload update stepmodification- transformation to apply to the current payload- See Also:
-
publish
public WorkflowStepResult publish(String stepName, EventMessage event, UnaryOperator<PublishStepDefinition> customizer) Publishes a business event as a durable workflow step and returns a handle to await later. Further customization is possible using the providedcustomizerunary operator.Exactly one event is appended: the given
eventenriched with workflow metadata (workflowId,stepName,stepType,stepPrimitive). Its type, payload, identifier and timestamp are published as-is, so the event other handlers and workflows receive is the event you publish. The workflow payload is not modified. On replay the step is found in the workflow state and nothing is published again.- Parameters:
stepName- logical name of the publish stepevent- event to publishcustomizer- customizer for the publish step definition- Returns:
- handle for the publish step
-
awaitPublish
public void awaitPublish(String stepName, EventMessage event, UnaryOperator<PublishStepDefinition> customizer) Publishes a business event as a durable workflow step and blocks until the step is recorded. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- logical name of the publish stepevent- event to publishcustomizer- customizer for the publish step definition- See Also:
-
fail
Fails the workflow and publishes the configured failure event. Further customization is possible using the providedcustomizerunary operator.- Parameters:
cause- reason the workflow should failcustomizer- customizer for failure metadata
-
fail
Fails the workflow using the default failure step definition.- Parameters:
cause- reason the workflow should fail
-
cancel
Cancels the workflow and publishes the configured cancellation event. Further customization is possible using the providedcustomizerunary operator.- Parameters:
customizer- customizer for cancellation metadata and cause
-
cancel
public void cancel()Cancels the workflow using the default cancellation step definition. -
cancelStep
Cancels a single running step without terminating the workflow. Further customization is possible using the providedcustomizerunary operator.- Parameters:
stepName- name of the step to cancelcustomizer- customizer for cancellation metadata and cause
-
cancelStep
Cancels a single running step using the default step cancellation definition.- Parameters:
stepName- name of the step to cancel
-
defaultExecuteStepDefinition
@Internal public ExecuteStepDefinition defaultExecuteStepDefinition(String stepName, Map<String, @Nullable Object> inputPayload, PayloadProcessor processor) Creates the default execute step definition used by the convenience overloads in this class.- Parameters:
stepName- logical name of the stepinputPayload- step-specific input valuesprocessor- function that performs the step work- Returns:
- default execute step definition
-
defaultWaitForStepDefinition
@Internal public WaitForStepDefinition defaultWaitForStepDefinition(String stepName, EventCondition eventCondition) Creates the default wait-for step definition used by the convenience overloads in this class.- Parameters:
stepName- logical name of the waiting stepeventCondition- event selection criteria- Returns:
- default wait-for step definition
-
defaultPayloadStepDefinition
@Internal public PayloadStepDefinition defaultPayloadStepDefinition(String stepName, PayloadModification modification) Creates the default payload modification step definition used by the convenience overloads in this class.- Parameters:
stepName- logical name of the payload update stepmodification- transformation to apply to the current payload- Returns:
- default payload step definition
-
defaultPublishStepDefinition
@Internal public PublishStepDefinition defaultPublishStepDefinition(String stepName, EventMessage event) Creates the default publish step definition used by thepublish(String, EventMessage, UnaryOperator)convenience overloads.- Parameters:
stepName- logical name of the publish stepevent- event to publish- Returns:
- default publish step definition
-
defaultMigrateVersionStepDefinition
protected VersionStepDefinition defaultMigrateVersionStepDefinition(String changeId, String newVersion) Creates the default version step definition used by themigrateVersion(String, String, UnaryOperator)convenience overloads.- Parameters:
changeId- developer-chosen identifier describing the change.newVersion- new workflow version to record (semver string).- Returns:
- default version step definition.
-
defaultFailWorkflowDefinition
Creates the default workflow failure definition.- Returns:
- default fail step definition
-
defaultCancelWorkflowDefinition
Creates the default workflow cancellation definition.- Returns:
- default cancel workflow definition
-
defaultCancelStepDefinition
Creates the default single-step cancellation definition.- Parameters:
stepName- name of the step to cancel- Returns:
- default cancel step definition
-
resolve
Resolves the qualified message name for the given message type.- Parameters:
messageType- event or command class to resolve- Returns:
- qualified name for the provided class
-
setDefaultTimeout
Sets the timeout applied to newly created execute and wait-for step definitions unless a step-specific customizer overrides it.- Parameters:
defaultTimeout- default timeout for newly created steps
-
defaultTimeout
Returns the timeout currently applied to newly created step definitions.- Returns:
- default timeout for newly created steps
-
setDefaultRetryPolicy
Sets the retry policy applied to newly created execute step definitions unless a customizer overrides it.- Parameters:
defaultRetryPolicy- default retry policy for execute steps
-
defaultRetryPolicy
Returns the retry policy currently applied to newly created execute step definitions.- Returns:
- default retry policy for execute steps
-
EventAssociationsUtils.equalsTo(Object)instead.