Class Workflow<S>
- Type Parameters:
S
- The type of the state for this workflow.
You can use workflows to implement business processes that span multiple services.
When implementing a workflow, you define a state type and a set of steps. Each step defines a call to be executed and the transition to the next step based on the result of the call. The workflow state can be updated after each successful step execution.
The runtime keeps track of the state of the workflow and the current step. If the workflow is stopped for any reason, it can be resumed from the last known state and step.
Workflow methods that handle incoming commands should return an Workflow.Effect
describing the next processing actions.
Concrete classes can accept the following types to the constructor:
ComponentClient
HttpClientProvider
TimerScheduler
Materializer
Config
WorkflowContext
- Custom types provided by a
DependencyProvider
from the service setup
Concrete class must be annotated with ComponentId
.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic class
static class
static interface
An Effect is a description of what the runtime needs to do after the command is handled.static interface
An effect that is known to be read only and does not update the state of the entity.static class
static interface
static class
static class
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected final CommandContext
Additional context and metadata for a command handler.protected final S
Returns the state as currently stored.abstract Workflow.WorkflowDef
<S> protected final Workflow.Effect.Builder
<S> effects()
Returns the initial empty state object.maxRetries
(int maxRetries) Starts defining a recover strategy for the workflow or a specific step.Start a step definition with a given step name.final TimerScheduler
timers()
Returns aTimerScheduler
that can be used to schedule further in time.workflow()
-
Constructor Details
-
Workflow
public Workflow()
-
-
Method Details
-
step
Start a step definition with a given step name.- Parameters:
name
- Step name.- Returns:
- Step builder.
-
emptyState
Returns the initial empty state object. This object will be passed into the command and step handlers, until a new state replaces it.Also known as "zero state" or "neutral state".
The default implementation of this method returns
null
. It can be overridden to return a more sensible initial state. -
commandContext
Additional context and metadata for a command handler.It will throw an exception if accessed from constructor.
- Throws:
IllegalStateException
- if accessed outside a handler method
-
timers
Returns aTimerScheduler
that can be used to schedule further in time. -
currentState
Returns the state as currently stored.Note that modifying the state directly will not update it in storage. To save the state, one must call {
effects().updateState()
}.This method can only be called when handling a command. Calling it outside a method (eg: in the constructor) will raise a IllegalStateException exception.
- Throws:
IllegalStateException
- if accessed outside a handler method
-
definition
- Returns:
- A workflow definition in a form of steps and transitions between them.
-
effects
-
workflow
-
maxRetries
Starts defining a recover strategy for the workflow or a specific step.- Parameters:
maxRetries
- number of retries before giving up.- Returns:
- MaxRetries strategy.
-