HTTP tasks allow you to issue HTTP requests and enhance the integration function of Flowable. Note that the HTTP task is not a formal task of the BPMN 2.0 specification (so there is no special icon). Therefore, in Flowable, HTTP task is implemented as a special service task.
1. Http client configuration
The Flowable engine sends Http requests through configurable Http clients. The following properties can be set in the flowable.cfg.xml configuration file:
<bean id="processEngineConfiguration" class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <!-- http client configurations --> <property name="httpClientConfig" ref="httpClientConfig"/> </bean> <bean id="httpClientConfig" class="org.flowable.engine.cfg.HttpClientConfig"> <property name="connectTimeout" value="5000"/> <property name="socketTimeout" value="5000"/> <property name="connectionRequestTimeout" value="5000"/> <property name="requestRetryLimit" value="5"/> </bean>
attribute | need | describe |
---|---|---|
connectTimeout | No, | Connection timeout in milliseconds. The default setting is 5000. |
socketTimeout | No, | Socket timeout in milliseconds. The default setting is 5000. |
connectionRequestTimeout | No, | The connection request timed out in milliseconds. The default setting is 5000. |
requestRetryLimit | No, | Request retry limit (0 means no retry). The default setting is 3. |
disableCertVerify | No, | Flag to disable SSL certificate authentication. Set to false by default. |
2. Define Http tasks
HTTP task is implemented as a special service task, which is defined by setting the service task type 'HTTP'.
<serviceTask id="httpGet" flowable:type="http">
You can also override the default Http Task behavior by providing a custom implementation. The custom implementation should extend org.flowable.http.HttpActivityBehavior and override the perform () method. The field httpActivityBehaviorClass should be set in the task definition. The default value of this field is org.flowable.http.impl.HttpActivityBehaviorImpl. Currently, HttpActivityBehaviorImpl is based on Apache Http Client. Since the Apache Http Client can be customized in many ways, all possible options are not used in the HttpClient configuration. To create a custom client.
<serviceTask id ="httpGet"flowable: type ="http"> <extensionElements> <flowable: field name ="httpActivityBehaviorClass"> <Liquidity: String> <![CDATA [org.example.flowable.HttpActivityBehaviorCustomImpl]]> </Flowable: String> </Flowable: Fields> </ extensionElements> </ sericeTask>
3. Http task configuration
Http tasks are configured through field injection. All values of these properties can contain EL expressions that are resolved at run time during process execution. The following properties can be set:
attribute | need | describe |
---|---|---|
requestMethod | yes | Request method (GET, POST, PUT, DELETE). |
requestUrl | yes | Request URL (example)- http://flowable.org ). |
requestHeaders | No, | Line delimited Http request header. Example - content type: application / json authorization: basic aGFRlc3Q= |
requestBody | No, | Request body example - ${sampleBody} |
request timeout | No, | Request timeout in milliseconds (example - 5000). The default value is 0, which means there is no timeout. |
disallowRedirects | No, | Marked to prohibit Http redirection. The default is false. (e.g. - true). |
failStatusCodes | No, | A comma separated list of Http response status codes to fail the request and raise the error as a FlowableException. Example: 400404500503 example: 400,5XX |
handleStatusCodes | No, | Comma separated tasks will throw a list of BpmnError status codes. The error code of BpmnError is HTTP. For example, the 404 status code is set to the error code HTTP404. The 3XX status code is thrown only when the disallowdirects field is also set. When the status code in handlestatus codes is set in failStatusCodes, the status code in failStatusCodes will be overwritten. For example: 400404500503 for example: 3XX, 4XX, 5XX |
ignoreException | No, | Flag for ignoring exceptions, capturing and saving exception messages as. errorMessage. |
saveRequestVariables | No, | Mark save request variables. By default, only response related variables are saved in execution. |
saveResponseParameters | No, | Tag saves all response variables, including HTTP status, title, etc. By default, only the response body is saved in execution. |
resultVariablePrefix | No, | Prefix of execution variable name. If the prefix is not set, the variable is saved as a name. fieldName. For example, for a task with id task7, the requestUrl is saved as task7.requestUrl. |
httpActivityBehaviorClass | No, | The full class name of the custom extension of org.flowable.http.HttpActivityBehavior. |
In addition to the fields provided, the following will be set as variables upon successful execution according to the saveResponseParameters flag.
variable | Optional | describe |
---|---|---|
responseProtocol | yes | Http version |
responseReason | yes | Http response reason phrase. |
responseStatusCode | yes | HTTP response status code (example - 200). |
responseHeaders response | yes | Line delimited Http response headers. Example - content type: application / jsoncontent length: 777 |
responseBody | yes | The response body as a string, if any. |
error message | yes | Ignore error messages, if any. |
4. Result variable
Remember that all execution variable names above are prefixed with the calculated value of resultVariablePrefix. For example, the response status code can be accessed in another activity, such as task7.responseStatusCode. Here, task7 is the id of the service task. To override this behavior, set resultVariablePrefix as needed.
5. Usage example
The following XML fragment shows an example of using Http tasks.
<serviceTask id="httpGet" flowable:type="http"> <extensionElements> <flowable:field name="requestMethod" stringValue="GET" /> <flowable:field name="requestUrl" stringValue="http://flowable.org" /> <flowable:field name="requestHeaders"> <flowable:expression> <![CDATA[ Accept: text/html Cache-Control: no-cache ]]> </flowable:expression> </flowable:field> <flowable:field name="requestTimeout"> <flowable:expression> <![CDATA[ ${requestTimeout} ]]> </flowable:expression> </flowable:field> <flowable:field name="resultVariablePrefix"> <flowable:string>task7</flowable:string> </flowable:field> </extensionElements> </serviceTask>
6. Error handling
By default, Http Task throws a FlowableException when Connection, IO or any unhandled exception occurs. But by default, it does not handle any redirection / client / server error http status codes. We can configure tasks to handle exceptions and http status by setting the failStatusCodes and / or handlestatus codes fields. The BpmnError raised by handlestatus codes should be handled like a normal BPMN exception with a corresponding boundary error handler. Here are some examples of exception handling and Http Task retry.
Failed on 400 and 5XX asynchronous http tasks and retry with failedJobRetryTimeCycle
<serviceTask id="failGet" name="Fail test" flowable:async="true" flowable:type="http"> <extensionElements> <flowable:field name="requestMethod"> <flowable:string><![CDATA[GET]]></flowable:string> </flowable:field> <flowable:field name="requestUrl"> <flowable:string><![CDATA[http://localhost:9798/api/fail]]></flowable:string> </flowable:field> <flowable:field name="failStatusCodes"> <flowable:string><![CDATA[400, 5XX]]></flowable:string> </flowable:field> <flowable:failedJobRetryTimeCycle>R3/PT5S</flowable:failedJobRetryTimeCycle> </extensionElements> </serviceTask>
Process 400 as BmpnError
<serviceTask id="handleGet" name="HTTP Task" flowable:type="http"> <extensionElements> <flowable:field name="requestMethod"> <flowable:string><![CDATA[GET]]></flowable:string> </flowable:field> <flowable:field name="requestUrl"> <flowable:string><![CDATA[http://localhost:9798/api/fail]]></flowable:string> </flowable:field> <flowable:field name="handleStatusCodes"> <flowable:string><![CDATA[4XX]]></flowable:string> </flowable:field> </extensionElements> </serviceTask> <boundaryEvent id="catch400" attachedToRef="handleGet"> <errorEventDefinition errorRef="HTTP400"></errorEventDefinition> </boundaryEvent>
Ignore exceptions.
<serviceTask id="ignoreTask" name="Fail test" flowable:type="http"> <extensionElements> <flowable:field name="requestMethod"> <flowable:string><![CDATA[GET]]></flowable:string> </flowable:field> <flowable:field name="requestUrl"> <flowable:string><![CDATA[http://nohost:9798/api]]></flowable:string> </flowable:field> <flowable:field name="ignoreException"> <flowable:string><![CDATA[true]]></flowable:string> </flowable:field> </extensionElements> </serviceTask>
The above article is from Pangu BPM Research Institute: http://vue.pangubpm.com/
Article translation submission: https://github.com/qiudaoke/flowable-userguide
For more articles, you can focus on WeChat official account: