Post Request to Receive Task

Hello there,

we’re having a receive task in our process where we wait a certain amount of months until somebody sends something to the system. Now we want to test this process using a post request from postman. We found a way using the REST API to find out the process instance id (flowable-taks/process-api/runtime/process-instances) and as there is no simple way to set a business key we now want to adress our post request against the process instance id.

Our first question would be if this is the right way of doing this and if yes, how we have to formulate the post request.

Best regards and many thanks in advance
Marius

Are you using the prebuilt Flowable-Rest application or a custom application with the REST API enabled? If you building a custom app, you can create your own endpoint but you’d have to make some assumptions about how many tasks will be active at a time. Imagine the following situation:

In this case there is a parallel gateway casing both send tasks to be executed followed by both receive tasks to await triggering. In this case, the process instance id is insufficient to determine which receive task needs to be triggered. If you know that only one triggerable task will be active at a time and don’t mind making that limitation permanent it would be easy to add an endpoint that takes the process instance id, queries for all active tasks and sends a trigger. Technically you could filter on task name but you’d need to deal with the complexity. You can do the same thing via a couple of calls to the REST API, if you don’t want to create your own end point.

As far as modeling the process goes, using a receive task is acceptable but if the message you are trying to receive comes in very quickly you might end up with a race condition between the message and Flowable’s database transaction. That can be solved using a transaction listener to do the actual sending:

    @Override
    public void execute(DelegateExecution delegateExecution) {
        ...

        Context.getTransactionContext().addTransactionListener(TransactionState.COMMITTED, new TransactionListener() {

            @Override
            public void execute(CommandContext commandContext) {
                rabbitTemplate.convertAndSend(exchange, routingKey, messageContent);
            }
        });
    }

It can be further refined by using a triggerable service task instead of a separate send and receive tasks.

Will

Hey Will,

first of all thanks for your valuable input. I’m afraid we still don’t get how we could post to our receive task.

Our receive task in theory will wait for weeks for it’s message so we don’t have a necessity to use triggerable tasks in this case.

We thought we could use the prebuilt Flowable REST application for this use case but if it’s necessary we also have no problem to create a custom REST API application. We just don’t know A how we should do this and B how to connect all of this to our flowable process model. Is there any specific step by step documentation for this procedure?

Best regards
Marius

Let’s assume we have a Spring Boot app with flowable-starter-rest and no security (for simpler curl requests) running on port 8989. This app has a single process named santaGiftRequest:

image

It contains a service task to send your wish list to Santa and then a receive task named “getPresents”. You can use the following sequence to fully execute it via the REST API:

  1. Starting the process:
╭─williamwitt@Diagon-Alley.local ~
╰─➤  curl -X POST \
  http://localhost:8989/process-api/runtime/process-instances \
  -H 'Content-Type: application/json' \
  -d '{
        "processDefinitionKey": "santaGiftRequest"
}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   733    0   686  100    47   133k   9400 --:--:-- --:--:-- --:--:--  143k
{
  "id": "e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
  "url": "http://localhost:8989/process-api/runtime/process-instances/e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
  "name": null,
  "businessKey": null,
  "suspended": false,
  "ended": false,
  "processDefinitionId": "santaGiftRequest:1:573a866f-1d00-11ea-a2ac-1ee190cb6f7e",
  "processDefinitionUrl": "http://localhost:8989/process-api/repository/process-definitions/santaGiftRequest:1:573a866f-1d00-11ea-a2ac-1ee190cb6f7e",
  "processDefinitionName": "Santa Gift Request",
  "processDefinitionDescription": null,
  "activityId": null,
  "startUserId": null,
  "startTime": "2019-12-12T11:28:56.727-06:00",
  "variables": [],
  "callbackId": null,
  "callbackType": null,
  "tenantId": "",
  "completed": false
}
  1. Find the execution
╭─williamwitt@Diagon-Alley.local ~
╰─➤  curl -X POST \
  http://localhost:8989/process-api/query/executions/ \
  -H 'Content-Type: application/json' \
  -d '{
        "processInstanceId": "e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
        "activityId": "getPresents"
}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   759    0   665  100    94   216k  31333 --:--:-- --:--:-- --:--:--  247k
{
  "data": [
    {
      "id": "e03b527e-1d04-11ea-a2ac-1ee190cb6f7e",
      "url": "http://localhost:8989/process-api/runtime/executions/e03b527e-1d04-11ea-a2ac-1ee190cb6f7e",
      "parentId": "e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
      "parentUrl": "http://localhost:8989/process-api/runtime/executions/e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
      "superExecutionId": null,
      "superExecutionUrl": null,
      "processInstanceId": "e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
      "processInstanceUrl": "http://localhost:8989/process-api/runtime/process-instances/e03b2b6c-1d04-11ea-a2ac-1ee190cb6f7e",
      "suspended": false,
      "activityId": "getPresents",
      "tenantId": ""
    }
  ],
  "total": 1,
  "start": 0,
  "sort": "processInstanceId",
  "order": "asc",
  "size": 1
}
  1. Trigger the execution
╭─williamwitt@Diagon-Alley.local ~
╰─➤  curl -X PUT \
  http://localhost:8989/process-api/runtime/executions/e03b527e-1d04-11ea-a2ac-1ee190cb6f7e \
  -H 'Content-Type: application/json' \
  -d '{
        "action": "trigger",
          "variables": [
    {
      "name": "myVariable",
      "type": "string",
      "value": "test"
    }
  ]
}' | jq
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   133    0     0  100   133      0  13300 --:--:-- --:--:-- --:--:-- 13300

Edit: If you’re using the prebuilt flowable-rest app, you’ll use basic authentication and the process endpoint is mapped at /flowable-rest/service/ instead of /process-api/

Will

Docs for doing this via the Java API:
https://flowable.org/docs/userguide/index.html#_xml_representation_2