Package io.littlehorse.sdk.wfsdk
Interface WorkflowThread
-
public interface WorkflowThreadThis interface is what is used to define the logic of a ThreaSpec in a ThreadFunc.
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.StringHANDLER_INPUT_VARThis is the reserved Variable Name that can be used as a WfRunVariable in an Interrupt Handler or Exception Handler thread.
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description WfRunVariableaddVariable(java.lang.String name, java.lang.Object typeOrDefaultVal)Defines a Variable in the `ThreadSpec` and returns a handle to it.UserTaskOutputassignUserTask(java.lang.String userTaskDefName, java.lang.Object userId, java.lang.Object userGroup)Adds a User Task Node, and assigns it to a specific uservoidcancelUserTaskRunAfter(UserTaskOutput userTask, java.io.Serializable delaySeconds)Cancels a User Task Run if it exceeds a specified deadline.voidcancelUserTaskRunAfterAssignment(UserTaskOutput userTask, java.io.Serializable delaySeconds)Cancels a User Task Run if it exceeds a specified deadline after it is assignedvoidcomplete()Adds an EXIT node with no Failure defined.WorkflowConditioncondition(java.lang.Object lhs, io.littlehorse.sdk.common.proto.Comparator comparator, java.lang.Object rhs)Returns a WorkflowCondition that can be used in `ThreadBuilder::doIf()` or `ThreadBuilder::doElse()`.voiddoIf(WorkflowCondition condition, IfElseBody doIf)Conditionally executes some workflow code; equivalent to an if() statement in programming.voiddoIfElse(WorkflowCondition condition, IfElseBody doIf, IfElseBody doElse)Conditionally executes one of two workflow code branches; equivalent to an if/else statement in programming.voiddoWhile(WorkflowCondition condition, ThreadFunc whileBody)Conditionally executes some workflow code; equivalent to an while() statement in programming.TaskNodeOutputexecute(LHFormatString taskName, java.io.Serializable... args)Adds a TASK node to the ThreadSpec.TaskNodeOutputexecute(WfRunVariable taskName, java.io.Serializable... args)Adds a TASK node to the ThreadSpec.TaskNodeOutputexecute(java.lang.String taskName, java.io.Serializable... args)Adds a TASK node to the ThreadSpec.voidfail(java.lang.Object output, java.lang.String failureName, java.lang.String message)Adds an EXIT node with a Failure defined.voidfail(java.lang.String failureName, java.lang.String message)Adds an EXIT node with a Failure defined.LHFormatStringformat(java.lang.String format, WfRunVariable... args)Creates a formatted string using WfRunVariables as arguments.voidhandleAnyFailure(NodeOutput node, ThreadFunc handler)Attaches an Failure Handler to the specified NodeOutput, allowing it to manage any types of errors or exceptions.voidhandleError(NodeOutput node, io.littlehorse.sdk.common.proto.LHErrorType error, ThreadFunc handler)Attaches an Error Handler to the specified NodeOutput, allowing it to manage specific types of errors as defined by the 'error' parameter.voidhandleError(NodeOutput node, ThreadFunc handler)Attaches an Error Handler to the specified NodeOutput, allowing it to manage any types of errors.voidhandleException(NodeOutput node, ThreadFunc handler)Attaches an Exception Handler to the specified NodeOutput, enabling it to handle any types of exceptions.voidhandleException(NodeOutput node, java.lang.String exceptionName, ThreadFunc handler)Attaches an Exception Handler to the specified NodeOutput, enabling it to handle specific types of exceptions as defined by the 'exceptionName' parameter.voidmutate(WfRunVariable lhs, io.littlehorse.sdk.common.proto.VariableMutationType type, java.lang.Object rhs)Adds a VariableMutation to the last NodevoidreassignUserTask(UserTaskOutput userTask, java.lang.Object userId, java.lang.Object userGroup, java.lang.Object deadlineSeconds)Schedules the reassignment of a User Task to a specified userId and/or userGroup after a specified expiration.voidregisterInterruptHandler(java.lang.String interruptName, ThreadFunc handler)Registers an Interrupt Handler, such that when an ExternalEvent arrives with the specified type, this ThreadRun is interrupted.voidreleaseToGroupOnDeadline(UserTaskOutput userTaskOutput, java.lang.Object deadlineSeconds)Schedule Reassignment of a UserTask to a userGroup upon reaching the Deadline.voidscheduleReminderTask(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)Adds a Reminder Task to a User Task Node.voidscheduleReminderTask(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)Adds a Reminder Task to a User Task Node.voidscheduleReminderTaskOnAssignment(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)Adds a task reminder once a user is assigned to the UserTask.voidscheduleReminderTaskOnAssignment(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)Adds a task reminder once a user is assigned to UserTask.voidsleepSeconds(java.lang.Object seconds)Adds a SLEEP node which makes the ThreadRun sleep for a specified number of seconds.voidsleepUntil(WfRunVariable timestamp)Adds a SLEEP node which makes the ThreadRun sleep until a specified timestamp, provided as an INT WfRunVariable (note that INT in LH is a 64-bit integer).SpawnedThreadspawnThread(ThreadFunc threadFunc, java.lang.String threadName, java.util.Map<java.lang.String,java.lang.Object> inputVars)Adds a SPAWN_THREAD node to the ThreadSpec, which spawns a Child ThreadRun whose ThreadSpec is determined by the provided ThreadFunc.SpawnedThreadsspawnThreadForEach(WfRunVariable arrVar, java.lang.String threadName, ThreadFunc threadFunc)Given a WfRunVariable of type JSON_ARR, this function iterates over each object in that list and creates a Child ThreadRun for each item.SpawnedThreadsspawnThreadForEach(WfRunVariable arrVar, java.lang.String threadName, ThreadFunc threadFunc, java.util.Map<java.lang.String,java.lang.Object> inputVars)Given a WfRunVariable of type JSON_ARR, this function iterates over each object in that list and creates a Child ThreadRun for each item.voidthrowEvent(java.lang.String workflowEventDefName, java.io.Serializable content)EXPERIMENTAL: Makes the active ThreadSpec throw a WorkflowEvent with a specific WorkflowEventDef and provided content.NodeOutputwaitForEvent(java.lang.String externalEventDefName)Adds an EXTERNAL_EVENT node which blocks until an 'ExternalEvent' of the specified type arrives.WaitForThreadsNodeOutputwaitForThreads(SpawnedThreads threadsToWaitFor)Adds a WAIT_FOR_THREAD node which waits for a Child ThreadRun to complete.voidwithRetentionPolicy(io.littlehorse.sdk.common.proto.ThreadRetentionPolicy policy)Overrides the retention policy for all ThreadRun's of this ThreadSpec in the WfRun.
-
-
-
Field Detail
-
HANDLER_INPUT_VAR
static final java.lang.String HANDLER_INPUT_VAR
This is the reserved Variable Name that can be used as a WfRunVariable in an Interrupt Handler or Exception Handler thread.- See Also:
- Constant Field Values
-
-
Method Detail
-
withRetentionPolicy
void withRetentionPolicy(io.littlehorse.sdk.common.proto.ThreadRetentionPolicy policy)
Overrides the retention policy for all ThreadRun's of this ThreadSpec in the WfRun.- Parameters:
policy- is the Thread Retention Policy.
-
execute
TaskNodeOutput execute(java.lang.String taskName, java.io.Serializable... args)
Adds a TASK node to the ThreadSpec.- Parameters:
taskName- The name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.- Returns:
- A NodeOutput for that TASK node.
-
execute
TaskNodeOutput execute(WfRunVariable taskName, java.io.Serializable... args)
Adds a TASK node to the ThreadSpec.- Parameters:
taskName- a WfRunVariable containing the name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.- Returns:
- A NodeOutput for that TASK node.
-
execute
TaskNodeOutput execute(LHFormatString taskName, java.io.Serializable... args)
Adds a TASK node to the ThreadSpec.- Parameters:
taskName- an LHFormatString containing the name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.- Returns:
- A NodeOutput for that TASK node.
-
assignUserTask
UserTaskOutput assignUserTask(java.lang.String userTaskDefName, java.lang.Object userId, java.lang.Object userGroup)
Adds a User Task Node, and assigns it to a specific user- Parameters:
userTaskDefName- is the UserTaskDef to assign.userId- is the user id to assign it to. Can be either String or WfRunVariable. Can be null if userGroup not null.userGroup- is the user group to assign it to. Can be either String or WfRunvariable. Can be null if userId not null.- Returns:
- a NodeOutput.
-
releaseToGroupOnDeadline
void releaseToGroupOnDeadline(UserTaskOutput userTaskOutput, java.lang.Object deadlineSeconds)
Schedule Reassignment of a UserTask to a userGroup upon reaching the Deadline. This method is used to schedule the reassignment of a UserTask to a userGroup when the specified UserTask user assignment reaches its deadline in seconds.- Parameters:
userTaskOutput- that is currently assigned to a UserGroup.deadlineSeconds- Time in seconds after which the UserTask will be automatically reassigned to the UserGroup. Can be either String or WfRunVariable.
-
reassignUserTask
void reassignUserTask(UserTaskOutput userTask, java.lang.Object userId, java.lang.Object userGroup, java.lang.Object deadlineSeconds)
Schedules the reassignment of a User Task to a specified userId and/or userGroup after a specified expiration.- Parameters:
userTask- is the userTask to reschedule.userId- is the userId to which the task should be assigned. Must be either WfRunVariable or String. Can be null if userGroup not null.userGroup- is the userGroup to which the task should be reassigned. Must be either WfRunVariable or String. Can be null if userId not null.deadlineSeconds- is the expiration time after which the UserTask should be reassigned. Can be either WfRunVariable or String.
-
format
LHFormatString format(java.lang.String format, WfRunVariable... args)
Creates a formatted string using WfRunVariables as arguments. Example: format("Hello there, {0}, today is {1}", name, dayOfWeek);- Parameters:
format- is the format string.args- are the format args.- Returns:
- an LHFormatString object which can be used as a variable assignment in a WfSpec.
-
addVariable
WfRunVariable addVariable(java.lang.String name, java.lang.Object typeOrDefaultVal)
Defines a Variable in the `ThreadSpec` and returns a handle to it.- Parameters:
name- the name of the variable.typeOrDefaultVal- is either the type of the variable, from the `VariableType` enum, or an object representing the default value of the Variable. If an object (or primitive) is provided, the Task Worker Library casts the provided value to a VariableValue and sets that as the default.- Returns:
- a handle to the created WfRunVariable.
-
doIf
void doIf(WorkflowCondition condition, IfElseBody doIf)
Conditionally executes some workflow code; equivalent to an if() statement in programming.- Parameters:
condition- is the WorkflowCondition to be satisfied.doIf- is the block of ThreadSpec code to be executed if the provided WorkflowCondition is satisfied.
-
doIfElse
void doIfElse(WorkflowCondition condition, IfElseBody doIf, IfElseBody doElse)
Conditionally executes one of two workflow code branches; equivalent to an if/else statement in programming.- Parameters:
condition- is the WorkflowCondition to be satisfied.doIf- is the block of ThreadSpec code to be executed if the provided WorkflowCondition is satisfied.doElse- is the block of ThreadSpec code to be executed if the provided WorkflowCondition is NOT satisfied.
-
scheduleReminderTask
void scheduleReminderTask(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)
Adds a Reminder Task to a User Task Node.- Parameters:
userTask- is a reference to the UserTaskNode that we schedule the action after.delaySeconds- is the delay time after which the Task should be executed.taskDefName- The name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.
-
scheduleReminderTask
void scheduleReminderTask(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)
Adds a Reminder Task to a User Task Node.- Parameters:
userTask- is a reference to the UserTaskNode that we schedule the action after.delaySeconds- is the delay time after which the Task should be executed.taskDefName- The name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.
-
scheduleReminderTaskOnAssignment
void scheduleReminderTaskOnAssignment(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)
Adds a task reminder once a user is assigned to UserTask.- Parameters:
userTask- is a reference to the UserTaskNode that we schedule the action after.delaySeconds- is the delay time after which the Task should be executed.taskDefName- The name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.
-
cancelUserTaskRunAfter
void cancelUserTaskRunAfter(UserTaskOutput userTask, java.io.Serializable delaySeconds)
Cancels a User Task Run if it exceeds a specified deadline.- Parameters:
userTask- is a reference to the UserTaskNode that will be canceled after the deadlinedelaySeconds- is the delay time after which the User Task Run should be canceled
-
cancelUserTaskRunAfterAssignment
void cancelUserTaskRunAfterAssignment(UserTaskOutput userTask, java.io.Serializable delaySeconds)
Cancels a User Task Run if it exceeds a specified deadline after it is assigned- Parameters:
userTask- is a reference to the UserTaskNode that will be canceled after the deadlinedelaySeconds- is the delay time after which the User Task Run should be canceled
-
scheduleReminderTaskOnAssignment
void scheduleReminderTaskOnAssignment(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.io.Serializable... args)
Adds a task reminder once a user is assigned to the UserTask.- Parameters:
userTask- is a reference to the UserTaskNode that we schedule the action after.delaySeconds- is the delay time after which the Task should be executed.taskDefName- The name of the TaskDef to execute.args- The input parameters to pass into the Task Run. If the type of an arg is a `WfRunVariable`, then that WfRunVariable is passed in as the argument; otherwise, the library will attempt to cast the provided argument to a LittleHorse VariableValue and pass that literal value in.
-
doWhile
void doWhile(WorkflowCondition condition, ThreadFunc whileBody)
Conditionally executes some workflow code; equivalent to an while() statement in programming.- Parameters:
condition- is the WorkflowCondition to be satisfied.whileBody- is the block of ThreadFunc code to be executed while the provided WorkflowCondition is satisfied.
-
spawnThread
SpawnedThread spawnThread(ThreadFunc threadFunc, java.lang.String threadName, java.util.Map<java.lang.String,java.lang.Object> inputVars)
Adds a SPAWN_THREAD node to the ThreadSpec, which spawns a Child ThreadRun whose ThreadSpec is determined by the provided ThreadFunc.- Parameters:
threadFunc- is a ThreadFunc (can be a lambda function) that defines the logic for the child ThreadRun to execute.threadName- is the name of the child thread spec.inputVars- is a Map of all of the input variables to set for the child ThreadRun. If you don't need to set any input variables, leave this null.- Returns:
- a handle to the resulting SpawnedThread, which can be used in ThreadBuilder::waitForThread()
-
waitForThreads
WaitForThreadsNodeOutput waitForThreads(SpawnedThreads threadsToWaitFor)
Adds a WAIT_FOR_THREAD node which waits for a Child ThreadRun to complete.- Parameters:
threadsToWaitFor- set of SpawnedThread objects returned one or more calls to spawnThread.- Returns:
- a NodeOutput that can be used for timeouts or exception handling.
-
waitForEvent
NodeOutput waitForEvent(java.lang.String externalEventDefName)
Adds an EXTERNAL_EVENT node which blocks until an 'ExternalEvent' of the specified type arrives.- Parameters:
externalEventDefName- is the type of ExternalEvent to wait for.- Returns:
- a NodeOutput for this event.
-
fail
void fail(java.lang.Object output, java.lang.String failureName, java.lang.String message)Adds an EXIT node with a Failure defined. This causes a ThreadRun to fail, and the resulting Failure has the specified value, name, and human-readable message.- Parameters:
output- is a literal value (cast to VariableValue by the Library) or a WfRunVariable. The assigned value is the payload of the resulting Failure, which can be accessed by any Failure Handler ThreadRuns.failureName- is the name of the failure to throw.message- is a human-readable message.
-
complete
void complete()
Adds an EXIT node with no Failure defined. This causes the ThreadRun to complete gracefully. It is equivalent to putting a call to `return;` early in your function.
-
fail
void fail(java.lang.String failureName, java.lang.String message)Adds an EXIT node with a Failure defined. This causes a ThreadRun to fail, and the resulting Failure has the specified name and human-readable message.- Parameters:
failureName- is the name of the failure to throw.message- is a human-readable message.
-
registerInterruptHandler
void registerInterruptHandler(java.lang.String interruptName, ThreadFunc handler)Registers an Interrupt Handler, such that when an ExternalEvent arrives with the specified type, this ThreadRun is interrupted.- Parameters:
interruptName- The name of the ExternalEventDef to listen for.handler- A Thread Function defining a ThreadSpec to use to handle the Interrupt.
-
sleepSeconds
void sleepSeconds(java.lang.Object seconds)
Adds a SLEEP node which makes the ThreadRun sleep for a specified number of seconds.- Parameters:
seconds- is either an integer representing the number of seconds to sleep for, or it is a WfRunVariable which evaluates to a VariableTypePb.INT specifying the number of seconds to sleep for.
-
sleepUntil
void sleepUntil(WfRunVariable timestamp)
Adds a SLEEP node which makes the ThreadRun sleep until a specified timestamp, provided as an INT WfRunVariable (note that INT in LH is a 64-bit integer).- Parameters:
timestamp- a WfRunVariable which evaluates to a VariableTypePb.INT specifying the epoch timestamp (in milliseconds) to wait for.
-
handleException
void handleException(NodeOutput node, java.lang.String exceptionName, ThreadFunc handler)
Attaches an Exception Handler to the specified NodeOutput, enabling it to handle specific types of exceptions as defined by the 'exceptionName' parameter. If 'exceptionName' is null, the handler will catch all exceptions.- Parameters:
node- The NodeOutput instance to which the Exception Handler will be attached.exceptionName- The name of the specific exception to handle. If set to null, the handler will catch all exceptions.handler- A ThreadFunction defining a ThreadSpec that specifies how to handle the exception.
-
handleException
void handleException(NodeOutput node, ThreadFunc handler)
Attaches an Exception Handler to the specified NodeOutput, enabling it to handle any types of exceptions.- Parameters:
node- The NodeOutput instance to which the Exception Handler will be attached.handler- A ThreadFunction defining a ThreadSpec that specifies how to handle the exception.
-
handleError
void handleError(NodeOutput node, io.littlehorse.sdk.common.proto.LHErrorType error, ThreadFunc handler)
Attaches an Error Handler to the specified NodeOutput, allowing it to manage specific types of errors as defined by the 'error' parameter. If 'error' is set to null, the handler will catch all errors.- Parameters:
node- The NodeOutput instance to which the Error Handler will be attached.error- The type of error that the handler will manage.handler- A ThreadFunction defining a ThreadSpec that specifies how to handle the error.
-
handleError
void handleError(NodeOutput node, ThreadFunc handler)
Attaches an Error Handler to the specified NodeOutput, allowing it to manage any types of errors.- Parameters:
node- The NodeOutput instance to which the Error Handler will be attached.handler- A ThreadFunction defining a ThreadSpec that specifies how to handle the error.
-
handleAnyFailure
void handleAnyFailure(NodeOutput node, ThreadFunc handler)
Attaches an Failure Handler to the specified NodeOutput, allowing it to manage any types of errors or exceptions.- Parameters:
node- The NodeOutput instance to which the Error Handler will be attached.handler- A ThreadFunction defining a ThreadSpec that specifies how to handle the error.
-
condition
WorkflowCondition condition(java.lang.Object lhs, io.littlehorse.sdk.common.proto.Comparator comparator, java.lang.Object rhs)
Returns a WorkflowCondition that can be used in `ThreadBuilder::doIf()` or `ThreadBuilder::doElse()`.- Parameters:
lhs- is either a literal value (which the Library casts to a Variable Value) or a `WfRunVariable` representing the LHS of the expression.comparator- is a Comparator defining the comparator, for example, `ComparatorTypePb.EQUALS`.rhs- is either a literal value (which the Library casts to a Variable Value) or a `WfRunVariable` representing the RHS of the expression.- Returns:
- a WorkflowCondition.
-
mutate
void mutate(WfRunVariable lhs, io.littlehorse.sdk.common.proto.VariableMutationType type, java.lang.Object rhs)
Adds a VariableMutation to the last Node- Parameters:
lhs- is a handle to the WfRunVariable to mutate.type- is the mutation type to use, for example, `VariableMutationType.ASSIGN`.rhs- is either a literal value (which the Library casts to a Variable Value), a `WfRunVariable` which determines the right hand side of the expression, or a `NodeOutput` (which allows you to use the output of a Node Run to mutate variables).
-
throwEvent
void throwEvent(java.lang.String workflowEventDefName, java.io.Serializable content)EXPERIMENTAL: Makes the active ThreadSpec throw a WorkflowEvent with a specific WorkflowEventDef and provided content.- Parameters:
workflowEventDefName- is the name of the WorkflowEvent to throw.content- is the content of the WorkflowEvent that is thrown.
-
spawnThreadForEach
SpawnedThreads spawnThreadForEach(WfRunVariable arrVar, java.lang.String threadName, ThreadFunc threadFunc)
Given a WfRunVariable of type JSON_ARR, this function iterates over each object in that list and creates a Child ThreadRun for each item. The list item is provided as an input variable to the Child ThreadRun with the name `INPUT`.- Parameters:
arrVar- is a WfRunVariable of type JSON_ARR that we iterate over.threadName- is the name to assign to the created ThreadSpec.threadFunc- is the function that defnes the ThreadSpec.- Returns:
- a SpawnedThreads handle which we can use to wait for all child threads.
-
spawnThreadForEach
SpawnedThreads spawnThreadForEach(WfRunVariable arrVar, java.lang.String threadName, ThreadFunc threadFunc, java.util.Map<java.lang.String,java.lang.Object> inputVars)
Given a WfRunVariable of type JSON_ARR, this function iterates over each object in that list and creates a Child ThreadRun for each item. The list item is provided as an input variable to the Child ThreadRun with the name `INPUT`.- Parameters:
arrVar- is a WfRunVariable of type JSON_ARR that we iterate over.threadName- is the name to assign to the created ThreadSpec.threadFunc- is the function that defnes the ThreadSpec.inputVars- is a map of input variables to pass to each child ThreadRun in addition to the list item.- Returns:
- a SpawnedThreads handle which we can use to wait for all child threads.
-
-