Interface ThreadBuilder


  • public interface ThreadBuilder
    This 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.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.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      WfRunVariable addVariable​(java.lang.String name, java.lang.Object typeOrDefaultVal)
      Defines a Variable in the `ThreadSpec` and returns a handle to it.
      UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName, WfRunVariable userId)
      Adds a User Task Node, and assigns it to a specific user
      UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName, WfRunVariable userId, WfRunVariable userGroup)
      Adds a User Task Node, and assigns it to a specific user
      UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName, WfRunVariable userId, java.lang.String userGroup)
      Adds a User Task Node, and assigns it to a specific user
      UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName, java.lang.String userId)
      Adds a User Task Node, and assigns it to a specific user
      UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName, java.lang.String userId, java.lang.String userGroup)
      Adds a User Task Node, and assigns it to a specific user
      UserTaskOutput assignTaskToUserGroup​(java.lang.String userTaskDefName, WfRunVariable userGroup)
      Adds a User Task Node, and assigns it to a group of users.
      UserTaskOutput assignTaskToUserGroup​(java.lang.String userTaskDefName, java.lang.String userGroup)
      Adds a User Task Node, and assigns it to a group of users.
      void complete()
      Adds an EXIT node with no Failure defined.
      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()`.
      void doIf​(WorkflowCondition condition, IfElseBody doIf)
      Conditionally executes some workflow code; equivalent to an if() statement in programming.
      void doIfElse​(WorkflowCondition condition, IfElseBody doIf, IfElseBody doElse)
      Conditionally executes one of two workflow code branches; equivalent to an if/else statement in programming.
      void doWhile​(WorkflowCondition condition, ThreadFunc whileBody)
      Conditionally executes some workflow code; equivalent to an while() statement in programming.
      NodeOutput execute​(java.lang.String taskName, java.lang.Object... args)
      Adds a TASK node to the ThreadSpec.
      void fail​(java.lang.Object output, java.lang.String failureName, java.lang.String message)
      Adds an EXIT node with a Failure defined.
      void fail​(java.lang.String failureName, java.lang.String message)
      Adds an EXIT node with a Failure defined.
      LHFormatString format​(java.lang.String format, WfRunVariable... args)
      Creates a formatted string using WfRunVariables as arguments.
      void handleException​(NodeOutput node, java.lang.String exceptionName, ThreadFunc handler)
      Adds a Failure Handler to the Node specified by the provided NodeOutput.
      void mutate​(WfRunVariable lhs, io.littlehorse.sdk.common.proto.VariableMutationType type, java.lang.Object rhs)
      Adds a VariableMutation to the last Node
      void reassignToGroupOnDeadline​(UserTaskOutput userTaskOutput, int deadlineSeconds)
      Schedule Reassignment of a UserTask to a userGroup upon reaching the Deadline.
      void reassignToUserOnDeadline​(UserTaskOutput userTaskOutput, java.lang.String userId, int deadlineSeconds)
      Schedule Reassignment of a UserTask to a userId upon reaching the Deadline.
      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.
      void scheduleReminderTask​(UserTaskOutput userTask, int delaySeconds, java.lang.String taskDefName, java.lang.Object... args)
      Adds a Reminder Task to a User Task Node.
      void scheduleReminderTask​(UserTaskOutput userTask, WfRunVariable delaySeconds, java.lang.String taskDefName, java.lang.Object... args)
      Adds a Reminder Task to a User Task Node.
      void sleepSeconds​(java.lang.Object seconds)
      Adds a SLEEP node which makes the ThreadRun sleep for a specified number of seconds.
      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).
      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.
      NodeOutput waitForEvent​(java.lang.String externalEventDefName)
      Adds an EXTERNAL_EVENT node which blocks until an 'ExternalEvent' of the specified type arrives.
      NodeOutput waitForThreads​(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.
      • assignTaskToUser

        UserTaskOutput assignTaskToUser​(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.
      • assignTaskToUser

        UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName,
                                        java.lang.String userId,
                                        java.lang.String 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.
        userGroup - is the User's group
        Returns:
        a NodeOutput.
      • reassignToGroupOnDeadline

        void reassignToGroupOnDeadline​(UserTaskOutput userTaskOutput,
                                       int 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.
      • reassignToUserOnDeadline

        void reassignToUserOnDeadline​(UserTaskOutput userTaskOutput,
                                      java.lang.String userId,
                                      int deadlineSeconds)
        Schedule Reassignment of a UserTask to a userId upon reaching the Deadline. This method is used to schedule the reassignment of a UserTask to a userId when the specified UserTask user assignment reaches its deadline in seconds.
        Parameters:
        userTaskOutput - that is currently assigned to a UserGroup.
        userId - that will be assigned to the UserTask after reaching the Deadline
        deadlineSeconds - Time in seconds after which the UserTask will be automatically reassigned to the UserGroup.
      • assignTaskToUser

        UserTaskOutput assignTaskToUser​(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.
      • assignTaskToUser

        UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName,
                                        WfRunVariable userId,
                                        java.lang.String 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.
        userGroup - is the User's group
        Returns:
        a NodeOutput.
      • assignTaskToUser

        UserTaskOutput assignTaskToUser​(java.lang.String userTaskDefName,
                                        WfRunVariable userId,
                                        WfRunVariable 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.
        userGroup - is the User's group
        Returns:
        a NodeOutput.
      • assignTaskToUserGroup

        UserTaskOutput assignTaskToUserGroup​(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)
        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.
      • assignTaskToUserGroup

        UserTaskOutput assignTaskToUserGroup​(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.
      • 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.lang.Object... 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.lang.Object... 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.
      • 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.
      • 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)
        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.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, `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).