Execute the Flowable workflow by REST API call and get the result

1.1k Views Asked by At

I am starting and completing the Flowable process using a REST API call. Then how can wait and get the final "ServiceTask" result send it back back to the caller?

basic-process.bpmn20.xml:

<process id="basicprocess" name="Basic Process" isExecutable="true">

    <startEvent id="startEvent"/>
    <sequenceFlow sourceRef="startEvent" targetRef="getInput"/>

    <userTask id="getInput" name="Get input from user" />
    <sequenceFlow sourceRef="getInput" targetRef="decision"/>

    <exclusiveGateway id="decision"/>
    <sequenceFlow sourceRef="decision" targetRef="firstServiceTask">
      <conditionExpression xsi:type="tFormalExpression">
        <![CDATA[
          ${number>100}
        ]]>
      </conditionExpression>
    </sequenceFlow>
    <sequenceFlow  sourceRef="decision" targetRef="secondServiceTask">
      <conditionExpression xsi:type="tFormalExpression">
        <![CDATA[
          ${number<=100}
        ]]>
      </conditionExpression>
    </sequenceFlow>

    <serviceTask id="firstServiceTask" name="Number is greater than predefined target"
        flowable:class="demo.service.tasks.FirstServiceTask"/>
    <sequenceFlow sourceRef="firstServiceTask" targetRef="greaterEnd"/>

    <serviceTask id="secondServiceTask" name="Number is less than predefined target"
        flowable:class="demo.service.tasks.SecondServiceTask"/>
    <sequenceFlow sourceRef="secondServiceTask" targetRef="lesserEnd"/>

    <endEvent id="greaterEnd"/>

    <endEvent id="lesserEnd"/>

  </process>

REST API call:

@RestController
@SuppressWarnings("rawtypes")
public class DefinitionsController {

    @Autowired
    private RepositoryService mRepositoryService;

    @Autowired
    private RuntimeService mRuntimeService;

    @Autowired
    private TaskService mTaskService;

    @PostMapping("/start-service")
    public String startService(@RequestBody String input) {
        Integer request = Integer.parseInt(input);
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("number", request);

        ProcessInstance instance = mRuntimeService.startProcessInstanceByKey("basicprocess", variables);

        Task userTask = mTaskService.createTaskQuery().processInstanceId(instance.getId()).taskDefinitionKey("getInput").singleResult();
        mTaskService.complete(userTask.getId());

        // How can I invoke the result of FirstServiceTask to return as a response value     
        // result = "FirstServiceTask has been executed";
        return "FirstServiceTask has been executed"
    }

}

ServiceTask:

public class FirstServiceTask implements JavaDelegate{

    @Override
    public void execute(DelegateExecution execution) {
        System.err.println("Came in first service task");

        // Need to invoke this result vale from REST API call for passing to caller as result
        String result = "FirstServiceTask has been executed";
    }

}
1

There are 1 best solutions below

0
On

Once you complete the first task, there is no wait phase. Hence all sequence flows and service tasks are executed in same transaction. In your java delegate, you can set the result via

execution.setVariable("varName", result);

And then in your rest service, once your task is complete, you can retrieve the process variables from the runtimeService.