Flowable getting started series article 48 - Http tasks

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>
attributeneeddescribe
connectTimeoutNo,Connection timeout in milliseconds. The default setting is 5000.
socketTimeoutNo,Socket timeout in milliseconds. The default setting is 5000.
connectionRequestTimeoutNo,The connection request timed out in milliseconds. The default setting is 5000.
requestRetryLimitNo,Request retry limit (0 means no retry). The default setting is 3.
disableCertVerifyNo,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:

attributeneeddescribe
requestMethodyesRequest method (GET, POST, PUT, DELETE).
requestUrlyesRequest URL (example)- http://flowable.org ).
requestHeadersNo,Line delimited Http request header. Example - content type: application / json authorization: basic aGFRlc3Q=
requestBodyNo,Request body example - ${sampleBody}
request timeoutNo,Request timeout in milliseconds (example - 5000). The default value is 0, which means there is no timeout.
disallowRedirectsNo,Marked to prohibit Http redirection. The default is false. (e.g. - true).
failStatusCodesNo,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
handleStatusCodesNo,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
ignoreExceptionNo,Flag for ignoring exceptions, capturing and saving exception messages as. errorMessage.
saveRequestVariablesNo,Mark save request variables. By default, only response related variables are saved in execution.
saveResponseParametersNo,Tag saves all response variables, including HTTP status, title, etc. By default, only the response body is saved in execution.
resultVariablePrefixNo,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.
httpActivityBehaviorClassNo,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.

variableOptionaldescribe
responseProtocolyesHttp version
responseReasonyesHttp response reason phrase.
responseStatusCodeyesHTTP response status code (example - 200).
responseHeaders responseyesLine delimited Http response headers. Example - content type: application / jsoncontent length: 777
responseBodyyesThe response body as a string, if any.
error messageyesIgnore 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:

Tags: Java Flowable oa bpm

Posted on Sat, 06 Nov 2021 21:19:34 -0400 by 5kyy8lu3