在 Camunda BPMN 引擎中传递冗长的文本 - http 连接器

Passing lengthy texts in Camunda BPMN engine - http connector

我已经实现了通过 camunda http 连接器从 camunda bpmn 调用 http REST 端点。

在这种情况下,首先,将调用 BpmnInvoker REST 端点,提供 bpmnProcessId 作为路径参数和 Authorization 键作为 header 参数请求体 然后在 BpmnInvoker 中,将调用相关的 bpmn 图并传递 requestBody 值。在这些 bpmn 任务中,将有一些服务任务将调用 REST 端点。

要从 BPMN 图表调用 REST 端点,我使用 camunda http 连接器。在这种情况下,我需要将授权密钥作为 http header 传递给 REST 端点。

为此,我将授权密钥添加到变量映射并将其作为 bpmn 图中的变量并添加到图像和 bpmn 图中提到的 header 映射。

但问题在于,此授权通常超过 4000 个字符。因此,它不能通过 camunda 引擎作为字符串传递。 我尝试将它转换为另一个 object (例如:byte[] / StringBuffer 等)假设它将被存储为一个 blob,但在这种情况下它会在分配时给出 Class Cast Exception到 http header.

BPMNInvoke.java

@Path("/base")
public class BpmnInvoker {

    ProcessEngine defaultProcessEngine = BpmPlatform.getProcessEngineService().getDefaultProcessEngine();
    RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();

    @POST
    @Path("/path/{bpmnProcessId}")
    public Response start(@PathParam("bpmnProcessId") String bpmnProcessId, String requestBody, @HeaderParam("Authorization") String authorization) {

        Map<String, Object> variableMap = new HashMap<String, Object>();
        variableMap = IntegrationUtility.convertJSONStringToMap(requestBody);

        // Set the Authorization token to the variable map. 
        // Normally this is more than 4000 characters long dynamic key. 
        // It must be set as a http header value when invoking REST endpoint via http connector.
        variableMap.put("Authorization", authorization);

        // Invoke the bpmn diagram by calling the startProcessInstanceByKey on runtimeService.
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(bpmnProcessId, variableMap);

        // rest of the work after completing the process engine
    }

}

BPMN 图

----
----
    <bpmn:serviceTask id="Task_1ujngj7" name="Dummy Service 2">
      <bpmn:extensionElements>
        <camunda:connector>
          <camunda:inputOutput>
            <camunda:inputParameter name="url">http://localhost:8080/x-services/path/to/rest/service</camunda:inputParameter>
            <camunda:inputParameter name="method">POST</camunda:inputParameter>
            <camunda:inputParameter name="headers">
              <camunda:map>
                <camunda:entry key="Authorization">${Authorization}</camunda:entry>
                <camunda:entry key="Content-Type">application/json</camunda:entry>
              </camunda:map>
            </camunda:inputParameter>
            <camunda:inputParameter name="payload">${payload}</camunda:inputParameter>
            <camunda:outputParameter name="payload">${response}</camunda:outputParameter>
          </camunda:inputOutput>
          <camunda:connectorId>http-connector</camunda:connectorId>
        </camunda:connector>
      </bpmn:extensionElements>
      <bpmn:incoming>SequenceFlow_0w4wfbo</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_153ti9s</bpmn:outgoing>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="SequenceFlow_1kvov2r" sourceRef="Task_0i9s02e" targetRef="EndEvent_0o062nq" />
    <bpmn:serviceTask id="Task_0i9s02e" name="DummyServiceClear" camunda:delegateExpression="${dummyServiceClear}">
      <bpmn:incoming>SequenceFlow_153ti9s</bpmn:incoming>
      <bpmn:outgoing>SequenceFlow_1kvov2r</bpmn:outgoing>
    </bpmn:serviceTask>
----
----

试试这个:

import org.camunda.bpm.engine.variable.Variables;
import org.camunda.bpm.engine.variable.Variables.SerializationDataFormats;

variableMap.put("Authorization", Variables
                              .objectValue(authorization)
                                  // tells the engine to use java serialization for persisting the value 
                              .serializationDataFormat(SerializationDataFormats.JAVA)  
                              .create());