Package io.littlehorse.sdk.wfsdk
Interface ThreadBuilder
-
public interface ThreadBuilderThis 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.WfRunVariableaddVariable(java.lang.String name, java.lang.Object typeOrDefaultVal, io.littlehorse.sdk.common.proto.IndexTypePb indexTypePb)Defines a Variable in the `ThreadSpec` and returns a handle to it.UserTaskOutputassignUserTaskToUser(java.lang.String userTaskDefName, WfRunVariable userId)Adds a User Task Node, and assigns it to a specific userUserTaskOutputassignUserTaskToUser(java.lang.String userTaskDefName, java.lang.String userId)Adds a User Task Node, and assigns it to a specific userUserTaskOutputassignUserTaskToUserGroup(java.lang.String userTaskDefName, WfRunVariable userGroup)Adds a User Task Node, and assigns it to a group of users.UserTaskOutputassignUserTaskToUserGroup(java.lang.String userTaskDefName, java.lang.String userGroup)Adds a User Task Node, and assigns it to a group of users.WorkflowConditioncondition(java.lang.Object lhs, io.littlehorse.sdk.common.proto.ComparatorPb 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.NodeOutputexecute(java.lang.String taskName, java.lang.Object... 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)voidhandleException(NodeOutput node, java.lang.String exceptionName, ThreadFunc handler)Adds a Failure Handler to the Node specified by the provided NodeOutput.voidmutate(WfRunVariable lhs, io.littlehorse.sdk.common.proto.VariableMutationTypePb type, java.lang.Object rhs)Adds a VariableMutation to the last NodevoidregisterInterruptHandler(java.lang.String interruptName, ThreadFunc handler)Registers an Interrupt Handler, such that when an ExternalEvent arrives with the specified type, this ThreadRun is interrupted.voidscheduleTaskAfter(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.lang.Object... args)Adds a TASK node to the ThreadSpec.voidscheduleTaskAfter(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.lang.Object... args)Adds a TASK node to the ThreadSpec.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.NodeOutputwaitForEvent(java.lang.String externalEventDefName)Adds an EXTERNAL_EVENT node which blocks until an 'ExternalEvent' of the specified type arrives.NodeOutputwaitForThreads(SpawnedThread... threadsToWaitFor)Adds a WAIT_FOR_THREAD node which waits for a Child ThreadRun to complete.
-
-
-
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
-
execute
NodeOutput execute(java.lang.String taskName, java.lang.Object... 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.
-
assignUserTaskToUser
UserTaskOutput assignUserTaskToUser(java.lang.String userTaskDefName, java.lang.String userId)
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.- Returns:
- a NodeOutput.
-
assignUserTaskToUser
UserTaskOutput assignUserTaskToUser(java.lang.String userTaskDefName, WfRunVariable userId)
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.- Returns:
- a NodeOutput.
-
assignUserTaskToUserGroup
UserTaskOutput assignUserTaskToUserGroup(java.lang.String userTaskDefName, java.lang.String userGroup)
Adds a User Task Node, and assigns it to a group of users.- Parameters:
userTaskDefName- is the UserTaskDef to assign.userGroup- is the User Group to assign the task to.- Returns:
- a UserTaskOutput.
-
format
LHFormatString format(java.lang.String format, WfRunVariable... args)
-
assignUserTaskToUserGroup
UserTaskOutput assignUserTaskToUserGroup(java.lang.String userTaskDefName, WfRunVariable userGroup)
Adds a User Task Node, and assigns it to a group of users.- Parameters:
userTaskDefName- is the UserTaskDef to assign.userGroup- is the User Group (either WfRunVariable or String) to assign the task to.- Returns:
- a UserTaskOutput.
-
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 `VariableTypePb` 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.
-
addVariable
WfRunVariable addVariable(java.lang.String name, java.lang.Object typeOrDefaultVal, io.littlehorse.sdk.common.proto.IndexTypePb indexTypePb)
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 `VariableTypePb` 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.indexTypePb- This argument indicates the storage method for the variable. Set it to REMOTE for variables with low cardinality.- 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.
-
scheduleTaskAfter
void scheduleTaskAfter(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.lang.Object... args)
Adds a TASK node to the ThreadSpec.- 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.
-
scheduleTaskAfter
void scheduleTaskAfter(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.lang.Object... args)
Adds a TASK node to the ThreadSpec.- 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
NodeOutput waitForThreads(SpawnedThread... threadsToWaitFor)
Adds a WAIT_FOR_THREAD node which waits for a Child ThreadRun to complete.- Parameters:
threadsToWaitFor- is an array 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.
-
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)
Adds a Failure Handler to the Node specified by the provided NodeOutput.- Parameters:
node- is a handle to the Node for which we want to catch a failure.exceptionName- is the name of the specific exception to handle. If left null, then this Failure Handler catches all failures.handler- is a ThreadFunction defining a ThreadSpec that should be used to handle the failure.
-
condition
WorkflowCondition condition(java.lang.Object lhs, io.littlehorse.sdk.common.proto.ComparatorPb 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 ComparatorPb 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.VariableMutationTypePb 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, `VariableMutationTypePb.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).
-
-