Class DaprWorkflowContextImpl
- All Implemented Interfaces:
WorkflowContext
-
Constructor Summary
ConstructorDescriptionDaprWorkflowContextImpl
(com.microsoft.durabletask.TaskOrchestrationContext context) Constructor for DaprWorkflowContextImpl.DaprWorkflowContextImpl
(com.microsoft.durabletask.TaskOrchestrationContext context, Saga saga) DaprWorkflowContextImpl
(com.microsoft.durabletask.TaskOrchestrationContext context, org.slf4j.Logger logger) Constructor for DaprWorkflowContextImpl.DaprWorkflowContextImpl
(com.microsoft.durabletask.TaskOrchestrationContext context, org.slf4j.Logger logger, Saga saga) Constructor for DaprWorkflowContextImpl. -
Method Summary
Modifier and TypeMethodDescription<V> com.microsoft.durabletask.Task<List<V>>
Returns a newTask
that is completed when all the givenTask
s complete.com.microsoft.durabletask.Task<com.microsoft.durabletask.Task<?>>
Returns a newTask
that is completed when any of the tasks intasks
completes.<V> com.microsoft.durabletask.Task<V>
callActivity
(String name, Object input, com.microsoft.durabletask.TaskOptions options, Class<V> returnType) Asynchronously invokes an activity by name and with the specified input value and returns a newTask
that completes when the activity completes.<V> com.microsoft.durabletask.Task<V>
callSubWorkflow
(String name, Object input, String instanceID, com.microsoft.durabletask.TaskOptions options, Class<V> returnType) Asynchronously invokes another workflow as a sub-workflow and returns aTask
that completes when the sub-workflow completes.void
Completes the current workflow.void
continueAsNew
(Object input) Restarts the orchestration with a new input and clears its history.void
continueAsNew
(Object input, boolean preserveUnprocessedEvents) Restarts the orchestration with a new input and clears its history.com.microsoft.durabletask.Task<Void>
createTimer
(Duration duration) Creates a durable timer that expires after the specified delay.Gets the current orchestration time in UTC.<T> T
Gets the deserialized input of the current task orchestration.Gets the instance ID of the current workflow.org.slf4j.Logger
Get a logger only whenisReplaying
is false.getName()
Gets the name of the current workflow.get saga context.boolean
Gets a value indicating whether the workflow is currently replaying a previous execution.newUuid()
Create a new UUID that is safe for replay within a workflow.<V> com.microsoft.durabletask.Task<Void>
waitForExternalEvent
(String name) Waits for an event to be raised namedname
and returns aTask
that completes when the event is received.<V> com.microsoft.durabletask.Task<Void>
waitForExternalEvent
(String name, Duration timeout) Waits for an event to be raised namedname
and returns aTask
that completes when the event is received or is canceled whentimeout
expires.<V> com.microsoft.durabletask.Task<V>
waitForExternalEvent
(String name, Duration timeout, Class<V> dataType) Waits for an event to be raised namedname
and returns aTask
that completes when the event is received or is canceled whentimeout
expires.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface io.dapr.workflows.WorkflowContext
anyOf, callActivity, callActivity, callActivity, callActivity, callActivity, callSubWorkflow, callSubWorkflow, callSubWorkflow, callSubWorkflow, callSubWorkflow, createTimer, waitForExternalEvent
-
Constructor Details
-
DaprWorkflowContextImpl
public DaprWorkflowContextImpl(com.microsoft.durabletask.TaskOrchestrationContext context) throws IllegalArgumentException Constructor for DaprWorkflowContextImpl.- Parameters:
context
- TaskOrchestrationContext- Throws:
IllegalArgumentException
- if context is null
-
DaprWorkflowContextImpl
public DaprWorkflowContextImpl(com.microsoft.durabletask.TaskOrchestrationContext context, org.slf4j.Logger logger) throws IllegalArgumentException Constructor for DaprWorkflowContextImpl.- Parameters:
context
- TaskOrchestrationContextlogger
- Logger- Throws:
IllegalArgumentException
- if context or logger is null
-
DaprWorkflowContextImpl
public DaprWorkflowContextImpl(com.microsoft.durabletask.TaskOrchestrationContext context, Saga saga) throws IllegalArgumentException - Throws:
IllegalArgumentException
-
DaprWorkflowContextImpl
public DaprWorkflowContextImpl(com.microsoft.durabletask.TaskOrchestrationContext context, org.slf4j.Logger logger, Saga saga) throws IllegalArgumentException Constructor for DaprWorkflowContextImpl.- Parameters:
context
- TaskOrchestrationContextlogger
- Loggersaga
- saga object, if null, saga is disabled- Throws:
IllegalArgumentException
- if context or logger is null
-
-
Method Details
-
getLogger
public org.slf4j.Logger getLogger()Get a logger only whenisReplaying
is false. Otherwise, return a NOP (no operation) logger.- Specified by:
getLogger
in interfaceWorkflowContext
- Returns:
- Logger
-
getName
Gets the name of the current workflow.- Specified by:
getName
in interfaceWorkflowContext
- Returns:
- the name of the current workflow
-
getInstanceId
Gets the instance ID of the current workflow.- Specified by:
getInstanceId
in interfaceWorkflowContext
- Returns:
- the instance ID of the current workflow
-
getCurrentInstant
Gets the current orchestration time in UTC.- Specified by:
getCurrentInstant
in interfaceWorkflowContext
- Returns:
- the current orchestration time in UTC
-
complete
Completes the current workflow.- Specified by:
complete
in interfaceWorkflowContext
- Parameters:
output
- the serializable output of the completed Workflow.
-
waitForExternalEvent
public <V> com.microsoft.durabletask.Task<V> waitForExternalEvent(String name, Duration timeout, Class<V> dataType) throws com.microsoft.durabletask.TaskCanceledException Waits for an event to be raised namedname
and returns aTask
that completes when the event is received or is canceled whentimeout
expires.If the current orchestration is not yet waiting for an event named
name
, then the event will be saved in the orchestration instance state and dispatched immediately when this method is called. This event saving occurs even if the current orchestrator cancels the wait operation before the event is received.Orchestrators can wait for the same event name multiple times, so waiting for multiple events with the same name is allowed. Each external event received by an orchestrator will complete just one task returned by this method.
- Specified by:
waitForExternalEvent
in interfaceWorkflowContext
- Type Parameters:
V
- the expected type of the event data payload- Parameters:
name
- the case-insensitive name of the event to wait fortimeout
- the amount of time to wait before canceling the returnedTask
dataType
- the expected class type of the event data payload- Returns:
- a new
Task
that completes when the external event is received or whentimeout
expires - Throws:
com.microsoft.durabletask.TaskCanceledException
- if the specifiedtimeout
value expires before the event is received
-
waitForExternalEvent
public <V> com.microsoft.durabletask.Task<Void> waitForExternalEvent(String name, Duration timeout) throws com.microsoft.durabletask.TaskCanceledException Waits for an event to be raised namedname
and returns aTask
that completes when the event is received or is canceled whentimeout
expires.See
waitForExternalEvent(String, Duration, Class)
for a full description.- Specified by:
waitForExternalEvent
in interfaceWorkflowContext
- Type Parameters:
V
- the expected type of the event data payload- Parameters:
name
- the case-insensitive name of the event to wait fortimeout
- the amount of time to wait before canceling the returnedTask
- Returns:
- a new
Task
that completes when the external event is received or whentimeout
expires - Throws:
com.microsoft.durabletask.TaskCanceledException
- if the specifiedtimeout
value expires before the event is received
-
waitForExternalEvent
public <V> com.microsoft.durabletask.Task<Void> waitForExternalEvent(String name) throws com.microsoft.durabletask.TaskCanceledException Waits for an event to be raised namedname
and returns aTask
that completes when the event is received.See
waitForExternalEvent(String, Duration, Class)
for a full description.- Specified by:
waitForExternalEvent
in interfaceWorkflowContext
- Type Parameters:
V
- the expected type of the event data payload- Parameters:
name
- the case-insensitive name of the event to wait for- Returns:
- a new
Task
that completes when the external event is received - Throws:
com.microsoft.durabletask.TaskCanceledException
-
isReplaying
public boolean isReplaying()Description copied from interface:WorkflowContext
Gets a value indicating whether the workflow is currently replaying a previous execution.Workflow functions are "replayed" after being unloaded from memory to reconstruct local variable state. During a replay, previously executed tasks will be completed automatically with previously seen values that are stored in the workflow history. Once the workflow reaches the point where it's no longer replaying existing history, this method will return
false
.You can use this method if you have logic that needs to run only when not replaying. For example, certain types of application logging may become too noisy when duplicated as part of replay. The application code could check to see whether the function is being replayed and then issue the log statements when this value is
false
.- Specified by:
isReplaying
in interfaceWorkflowContext
- Returns:
true
if the workflow is replaying, otherwisefalse
-
callActivity
public <V> com.microsoft.durabletask.Task<V> callActivity(String name, Object input, com.microsoft.durabletask.TaskOptions options, Class<V> returnType) Asynchronously invokes an activity by name and with the specified input value and returns a newTask
that completes when the activity completes. If the activity completes successfully, the returnedTask
's value will be the activity's output. If the activity fails, the returnedTask
will complete exceptionally with aTaskFailedException
.- Specified by:
callActivity
in interfaceWorkflowContext
- Type Parameters:
V
- the expected type of the activity output- Parameters:
name
- the name of the activity to callinput
- the serializable input to pass to the activityoptions
- additional options that control the execution and processing of the activityreturnType
- the expected class type of the activity output- Returns:
- a new
Task
that completes when the activity completes or fails
-
allOf
public <V> com.microsoft.durabletask.Task<List<V>> allOf(List<com.microsoft.durabletask.Task<V>> tasks) throws com.microsoft.durabletask.CompositeTaskFailedException Returns a newTask
that is completed when all the givenTask
s complete. If any of the givenTask
s complete with an exception, the returnedTask
will also complete with anCompositeTaskFailedException
containing details of the first encountered failure. The value of the returnedTask
is an ordered list of the return values of the given tasks. If no tasks are provided, returns aTask
completed with valuenull
.This method is useful for awaiting the completion of a set of independent tasks before continuing to the next step in the orchestration, as in the following example:
Task<String> t1 = ctx.callActivity("MyActivity", String.class); Task<String> t2 = ctx.callActivity("MyActivity", String.class); Task<String> t3 = ctx.callActivity("MyActivity", String.class); List<String> orderedResults = ctx.allOf(List.of(t1, t2, t3)).await();
Exceptions in any of the given tasks results in an unchecked
CompositeTaskFailedException
. This exception can be inspected to obtain failure details of individualTask
s.try { List<String> orderedResults = ctx.allOf(List.of(t1, t2, t3)).await(); } catch (CompositeTaskFailedException e) { List<Exception> exceptions = e.getExceptions() }
- Specified by:
allOf
in interfaceWorkflowContext
- Type Parameters:
V
- the return type of theTask
objects- Parameters:
tasks
- the list ofTask
objects- Returns:
- the values of the completed
Task
objects in the same order as the source list - Throws:
com.microsoft.durabletask.CompositeTaskFailedException
- if the specifiedtimeout
value expires before the event is received
-
anyOf
public com.microsoft.durabletask.Task<com.microsoft.durabletask.Task<?>> anyOf(List<com.microsoft.durabletask.Task<?>> tasks) Returns a newTask
that is completed when any of the tasks intasks
completes. SeeWorkflowContext.anyOf(Task[])
for more detailed information.- Specified by:
anyOf
in interfaceWorkflowContext
- Parameters:
tasks
- the list ofTask
objects- Returns:
- a new
Task
that is completed when any of the givenTask
s complete - See Also:
-
createTimer
Creates a durable timer that expires after the specified delay.Specifying a long delay (for example, a delay of a few days or more) may result in the creation of multiple, internally-managed durable timers. The orchestration code doesn't need to be aware of this behavior. However, it may be visible in framework logs and the stored history state.
- Specified by:
createTimer
in interfaceWorkflowContext
- Parameters:
duration
- the amount of time before the timer should expire- Returns:
- a new
Task
that completes after the specified delay
-
getInput
Gets the deserialized input of the current task orchestration.- Specified by:
getInput
in interfaceWorkflowContext
- Type Parameters:
T
- the expected type of the workflow input- Parameters:
targetType
- theClass
object associated withV
- Returns:
- the deserialized input as an object of type
V
ornull
if no input was provided.
-
callSubWorkflow
public <V> com.microsoft.durabletask.Task<V> callSubWorkflow(String name, @Nullable Object input, @Nullable String instanceID, @Nullable com.microsoft.durabletask.TaskOptions options, Class<V> returnType) Asynchronously invokes another workflow as a sub-workflow and returns aTask
that completes when the sub-workflow completes. If the sub-workflow completes successfully, the returnedTask
's value will be the activity's output. If the sub-workflow fails, the returnedTask
will complete exceptionally with aTaskFailedException
.A sub-workflow has its own instance ID, history, and status that is independent of the parent workflow that started it. There are many advantages to breaking down large orchestrations into sub-workflows:
- Splitting large orchestrations into a series of smaller sub-workflows can make code more maintainable.
- Distributing orchestration logic across multiple compute nodes concurrently is useful if orchestration logic otherwise needs to coordinate a lot of tasks.
- Memory usage and CPU overhead can be reduced by keeping the history of parent orchestrations smaller.
Because sub-workflows are independent of their parents, terminating a parent orchestration does not affect any sub-workflows. sub-workflows must be terminated independently using their unique instance ID, which is specified using the
instanceID
parameter- Specified by:
callSubWorkflow
in interfaceWorkflowContext
- Type Parameters:
V
- the expected type of the sub-workflow output- Parameters:
name
- the name of the workflow to invokeinput
- the serializable input to send to the sub-workflowinstanceID
- the unique ID of the sub-workflowoptions
- additional options that control the execution and processing of the activityreturnType
- the expected class type of the sub-workflow output- Returns:
- a new
Task
that completes when the sub-workflow completes or fails
-
continueAsNew
Restarts the orchestration with a new input and clears its history. SeeWorkflowContext.continueAsNew(Object, boolean)
for a full description.- Specified by:
continueAsNew
in interfaceWorkflowContext
- Parameters:
input
- the serializable input data to re-initialize the instance with
-
continueAsNew
Restarts the orchestration with a new input and clears its history.This method is primarily designed for eternal orchestrations, which are orchestrations that may not ever complete. It works by restarting the orchestration, providing it with a new input, and truncating the existing orchestration history. It allows an orchestration to continue running indefinitely without having its history grow unbounded. The benefits of periodically truncating history include decreased memory usage, decreased storage volumes, and shorter orchestrator replays when rebuilding state.
The results of any incomplete tasks will be discarded when an orchestrator calls
continueAsNew
. For example, if a timer is scheduled and thencontinueAsNew
is called before the timer fires, the timer event will be discarded. The only exception to this is external events. By default, if an external event is received by an orchestration but not yet processed, the event is saved in the orchestration state unit it is received by a call toWorkflowContext.waitForExternalEvent(java.lang.String, java.time.Duration, java.lang.Class<V>)
. These events will remain in memory even after an orchestrator restarts usingcontinueAsNew
. This behavior can be disabled by specifyingfalse
for thepreserveUnprocessedEvents
parameter value.Orchestrator implementations should complete immediately after calling the
continueAsNew
method.- Specified by:
continueAsNew
in interfaceWorkflowContext
- Parameters:
input
- the serializable input data to re-initialize the instance withpreserveUnprocessedEvents
-true
to push unprocessed external events into the new orchestration history, otherwisefalse
-
newUuid
Create a new UUID that is safe for replay within a workflow.The default implementation of this method creates a name-based UUID using the algorithm from RFC 4122 ยง4.3. The name input used to generate this value is a combination of the workflow instance ID and an internally managed sequence number.
- Specified by:
newUuid
in interfaceWorkflowContext
- Returns:
- a deterministic UUID
-
getSagaContext
Description copied from interface:WorkflowContext
get saga context.- Specified by:
getSagaContext
in interfaceWorkflowContext
- Returns:
- saga context
-