Skip to content

Conditional Workflows

yogeshnachnani edited this page Apr 11, 2016 · 4 revisions

The code for the example below can be viewed at SimpleDecisionWorkflow

There will be cases where a workflow needs to execute differently based on different conditions during execution time.

Such decisions can be taken from within task methods.

@Workflow(version = 1)
public void startWorkflow() {
    /* Some Task that returns data that may or may not be valid */
    final Promise<TaskData> taskDataPromise = simpleTaskExecutor.performSimpleTask();
     /*
      Determining if the data is good or bad can only happen after the data is ready.
      We cannot write a simple if statement in this method itself.
      This is because, in a Flux production environment, the startWorkflow() method simply creates the workflow in the Flux engine
      The actual execution and orchestration is handled by Flux.
      The real returned data is made available to the below function once
      performSimpleTask() is successfully completed
    */
    notifyBadData(taskDataPromise);
    }

    /**
     * Idempotent operation that would use the notificationService to notify about badly created data
     * @param taskDataPromise
     */
    @Task(version = 1, timeout = 1000l, retries = 2)
    public void notifyBadData(Promise<TaskData> taskDataPromise) {
        if (taskDataPromise.get().isGood()) {
            // Do nothing
        } else {
            /* notify method is also annotated with @Task */
            notificationService.notify(taskDataPromise.get().getId());
        }
    }

As mentioned, the decision needs to be taken within a task.

Here is the flow of execution in the production environment:

  • The call to startWorkFlow is intercepted by the Flux Client
  • Calls to every @Task methods are also intercepted. This is how the Flux Client determines the workflow definition
  • The workflow definition with 2 states [or tasks] - performSimpleTask() and notifyBadData - is submitted for execution
  • During execution of notifyBadData, the call to notify is intercepted. This is then submitted as another task to the already executing state machine
  • The call to notifyBadData returns
  • Execution of notify is triggered since all its dependencies are met
Clone this wiki locally