[BPMN] Service Task with REST-API (OTHER) Example

หลังจากได้ลองไปแล้วกับ REST API ผ่าน Service Task บน BPMN ไป 2 เรื่อง

คราวนี้ก็มาลองแบบที่เหลือบ้างว่าอันไหน Work หรือไม่ Work ครับ โดยผมได้สร้าง BPMN ที่มีกระบวนการทำงาน ดังรูปครับ

  • ถ้าสังเกตุในแบบจำลองที่ผมทำ มันมีสัญลักษณ์ที่ถูกเขียนกำกับว่า JSON-Server ตัวนั้น คือ DataStore อันนี้ในตัว BPMN Engine ไม่ได้สนใจครับ แต่เป็นสัญลักษณ์ทีแสดงให้เห็นภาพรวมของกระบวนการให้ครบ
  • ไฟล์ BPMN ครับ ถ้าสนใจพวก Config สามารถแงะตามได้ครับ
    <?xml version="1.0" encoding="UTF-8"?>
    <bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="Definitions_1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="1.11.3">
    <bpmn:process id="Post_WS_HTTPCONNECTOR_TEST" name="Post_WS_HTTPCONNECTOR_TEST" isExecutable="true" camunda:versionTag="1">
    <bpmn:startEvent id="StartEvent_1" name="">
    <bpmn:extensionElements>
    <camunda:formData />
    </bpmn:extensionElements>
    <bpmn:outgoing>SequenceFlow_1fpz9fj</bpmn:outgoing>
    </bpmn:startEvent>
    <bpmn:sequenceFlow id="SequenceFlow_1fpz9fj" sourceRef="StartEvent_1" targetRef="ServiceTask_01za9xm" />
    <bpmn:dataStoreReference id="DataStoreReference_0rddqs2" name="JSON-SERVER" />
    <bpmn:serviceTask id="ServiceTask_01za9xm" name="RequestTest GET">
    <bpmn:extensionElements>
    <camunda:connector>
    <camunda:inputOutput>
    <camunda:inputParameter name="url">http://localhost:3000/tests/2</camunda:inputParameter>
    <camunda:inputParameter name="method">GET</camunda:inputParameter>
    <camunda:inputParameter name="headers">
    <camunda:map>
    <camunda:entry key="accept">application/json</camunda:entry>
    </camunda:map>
    </camunda:inputParameter>
    <camunda:outputParameter name="response">
    <camunda:script scriptFormat="groovy"><![CDATA[out:println "response:" + response;
    out:println "statusCode:" + statusCode;
    S(response);]]></camunda:script>
    </camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:connectorId>http-connector</camunda:connectorId>
    </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_1fpz9fj</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_0opd2f1</bpmn:outgoing>
    <bpmn:dataOutputAssociation id="DataOutputAssociation_0lt6ly3">
    <bpmn:targetRef>DataStoreReference_0rddqs2</bpmn:targetRef>
    </bpmn:dataOutputAssociation>
    </bpmn:serviceTask>
    <bpmn:endEvent id="EndEvent_0m762ne" name="">
    <bpmn:incoming>SequenceFlow_1v6d4al</bpmn:incoming>
    </bpmn:endEvent>
    <bpmn:serviceTask id="ServiceTask_144bv9x" name="RequestTest POST">
    <bpmn:extensionElements>
    <camunda:connector>
    <camunda:inputOutput>
    <camunda:inputParameter name="url">http://localhost:3000/tests/</camunda:inputParameter>
    <camunda:inputParameter name="method">POST</camunda:inputParameter>
    <camunda:inputParameter name="headers">
    <camunda:map>
    <camunda:entry key="accept">application/json</camunda:entry>
    <camunda:entry key="Content-Type">application/json</camunda:entry>
    </camunda:map>
    </camunda:inputParameter>
    <camunda:inputParameter name="payload"><![CDATA[{"Test Payload": "NEW PAYLOAD FROM POST"}]]></camunda:inputParameter>
    <camunda:outputParameter name="response">
    <camunda:script scriptFormat="groovy"><![CDATA[out:println "response:" + response;
    out:println "statusCode:" + statusCode;
    S(response);]]></camunda:script>
    </camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:connectorId>http-connector</camunda:connectorId>
    </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_0fzs1bx</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_13cxqow</bpmn:outgoing>
    <bpmn:dataOutputAssociation id="DataOutputAssociation_0im1d3y">
    <bpmn:targetRef>DataStoreReference_0rddqs2</bpmn:targetRef>
    </bpmn:dataOutputAssociation>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="Task_13d411k" name="RequestTest PATCH">
    <bpmn:extensionElements>
    <camunda:connector>
    <camunda:inputOutput>
    <camunda:inputParameter name="url">http://localhost:3000/tests/2</camunda:inputParameter>
    <camunda:inputParameter name="method">PATCH</camunda:inputParameter>
    <camunda:inputParameter name="headers">
    <camunda:map>
    <camunda:entry key="accept">application/json</camunda:entry>
    <camunda:entry key="Content-Type">application/json</camunda:entry>
    </camunda:map>
    </camunda:inputParameter>
    <camunda:inputParameter name="payload"><![CDATA[{"Test Payload": "PATCH OLD 12 - NEW 21"}]]></camunda:inputParameter>
    <camunda:outputParameter name="response">
    <camunda:script scriptFormat="groovy"><![CDATA[out:println "response:" + response;
    out:println "statusCode:" + statusCode;
    S(response);]]></camunda:script>
    </camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:connectorId>http-connector</camunda:connectorId>
    </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_0opd2f1</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_0fzs1bx</bpmn:outgoing>
    <bpmn:dataOutputAssociation id="DataOutputAssociation_1wxlwlo">
    <bpmn:targetRef>DataStoreReference_0rddqs2</bpmn:targetRef>
    </bpmn:dataOutputAssociation>
    </bpmn:serviceTask>
    <bpmn:sequenceFlow id="SequenceFlow_0opd2f1" sourceRef="ServiceTask_01za9xm" targetRef="Task_13d411k" />
    <bpmn:sequenceFlow id="SequenceFlow_0fzs1bx" sourceRef="Task_13d411k" targetRef="ServiceTask_144bv9x" />
    <bpmn:sequenceFlow id="SequenceFlow_13cxqow" sourceRef="ServiceTask_144bv9x" targetRef="Task_1nkplgq" />
    <bpmn:sequenceFlow id="SequenceFlow_082a265" sourceRef="Task_1nkplgq" targetRef="Task_0d6wbxi" />
    <bpmn:sequenceFlow id="SequenceFlow_1v6d4al" sourceRef="Task_0d6wbxi" targetRef="EndEvent_0m762ne" />
    <bpmn:serviceTask id="Task_1nkplgq" name="RequestTest&#10;Delete">
    <bpmn:extensionElements>
    <camunda:connector>
    <camunda:inputOutput>
    <camunda:inputParameter name="url">http://localhost:3000/tests/3</camunda:inputParameter>
    <camunda:inputParameter name="method">DELETE</camunda:inputParameter>
    <camunda:inputParameter name="headers">
    <camunda:map>
    <camunda:entry key="accept">application/json</camunda:entry>
    </camunda:map>
    </camunda:inputParameter>
    <camunda:outputParameter name="response">
    <camunda:script scriptFormat="groovy"><![CDATA[out:println "response:" + response;
    out:println "statusCode:" + statusCode;
    S(response);]]></camunda:script>
    </camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:connectorId>http-connector</camunda:connectorId>
    </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_13cxqow</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_082a265</bpmn:outgoing>
    <bpmn:dataOutputAssociation id="DataOutputAssociation_1ugo5a1">
    <bpmn:targetRef>DataStoreReference_0rddqs2</bpmn:targetRef>
    </bpmn:dataOutputAssociation>
    </bpmn:serviceTask>
    <bpmn:serviceTask id="Task_0d6wbxi" name="RequestTest&#10;PUT">
    <bpmn:extensionElements>
    <camunda:connector>
    <camunda:inputOutput>
    <camunda:inputParameter name="url">http://localhost:3000/tests/1</camunda:inputParameter>
    <camunda:inputParameter name="method">PUT</camunda:inputParameter>
    <camunda:inputParameter name="headers">
    <camunda:map>
    <camunda:entry key="accept">application/json</camunda:entry>
    <camunda:entry key="Content-Type">application/json</camunda:entry>
    </camunda:map>
    </camunda:inputParameter>
    <camunda:inputParameter name="payload"><![CDATA[{"Test PUT": "PUT"}]]></camunda:inputParameter>
    <camunda:outputParameter name="response">
    <camunda:script scriptFormat="groovy"><![CDATA[out:println "response:" + response;
    out:println "statusCode:" + statusCode;
    S(response);]]></camunda:script>
    </camunda:outputParameter>
    </camunda:inputOutput>
    <camunda:connectorId>http-connector</camunda:connectorId>
    </camunda:connector>
    </bpmn:extensionElements>
    <bpmn:incoming>SequenceFlow_082a265</bpmn:incoming>
    <bpmn:outgoing>SequenceFlow_1v6d4al</bpmn:outgoing>
    <bpmn:dataOutputAssociation id="DataOutputAssociation_0torvjt">
    <bpmn:targetRef>DataStoreReference_0rddqs2</bpmn:targetRef>
    </bpmn:dataOutputAssociation>
    </bpmn:serviceTask>
    </bpmn:process>
    <bpmn:message id="Message_0g6u9tl" name="Message_09re6rq" />
    <bpmndi:BPMNDiagram id="BPMNDiagram_1">
    <bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Post_WS_HTTPCONNECTOR_TEST">
    <bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
    <dc:Bounds x="101" y="296" width="36" height="36" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="74" y="332" width="90" height="20" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNEdge id="SequenceFlow_1fpz9fj_di" bpmnElement="SequenceFlow_1fpz9fj">
    <di:waypoint xsi:type="dc:Point" x="137" y="314" />
    <di:waypoint xsi:type="dc:Point" x="194" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="125" y="289" width="90" height="20" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNShape id="DataStoreReference_0rddqs2_di" bpmnElement="DataStoreReference_0rddqs2">
    <dc:Bounds x="519" y="439" width="50" height="50" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="505" y="489" width="79" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape id="ServiceTask_01za9xm_di" bpmnElement="ServiceTask_01za9xm">
    <dc:Bounds x="194" y="274" width="100" height="80" />
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape id="EndEvent_0m762ne_di" bpmnElement="EndEvent_0m762ne">
    <dc:Bounds x="922" y="296" width="36" height="36" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="895" y="332" width="90" height="20" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape id="ServiceTask_144bv9x_di" bpmnElement="ServiceTask_144bv9x">
    <dc:Bounds x="494" y="274" width="100" height="80" />
    </bpmndi:BPMNShape>
    <bpmndi:BPMNEdge id="DataOutputAssociation_0lt6ly3_di" bpmnElement="DataOutputAssociation_0lt6ly3">
    <di:waypoint xsi:type="dc:Point" x="294" y="341" />
    <di:waypoint xsi:type="dc:Point" x="517" y="464" />
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="DataOutputAssociation_0im1d3y_di" bpmnElement="DataOutputAssociation_0im1d3y">
    <di:waypoint xsi:type="dc:Point" x="544" y="354" />
    <di:waypoint xsi:type="dc:Point" x="544" y="439" />
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNShape id="ServiceTask_024fm0m_di" bpmnElement="Task_13d411k">
    <dc:Bounds x="343" y="274" width="100" height="80" />
    </bpmndi:BPMNShape>
    <bpmndi:BPMNEdge id="SequenceFlow_0opd2f1_di" bpmnElement="SequenceFlow_0opd2f1">
    <di:waypoint xsi:type="dc:Point" x="294" y="314" />
    <di:waypoint xsi:type="dc:Point" x="343" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="318.5" y="293" width="0" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="SequenceFlow_0fzs1bx_di" bpmnElement="SequenceFlow_0fzs1bx">
    <di:waypoint xsi:type="dc:Point" x="443" y="314" />
    <di:waypoint xsi:type="dc:Point" x="494" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="423.5" y="293" width="90" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="SequenceFlow_13cxqow_di" bpmnElement="SequenceFlow_13cxqow">
    <di:waypoint xsi:type="dc:Point" x="594" y="314" />
    <di:waypoint xsi:type="dc:Point" x="640" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="572" y="293" width="90" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="DataOutputAssociation_1wxlwlo_di" bpmnElement="DataOutputAssociation_1wxlwlo">
    <di:waypoint xsi:type="dc:Point" x="438" y="349" />
    <di:waypoint xsi:type="dc:Point" x="517" y="449" />
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="SequenceFlow_082a265_di" bpmnElement="SequenceFlow_082a265">
    <di:waypoint xsi:type="dc:Point" x="740" y="314" />
    <di:waypoint xsi:type="dc:Point" x="782" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="761" y="293" width="0" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="SequenceFlow_1v6d4al_di" bpmnElement="SequenceFlow_1v6d4al">
    <di:waypoint xsi:type="dc:Point" x="882" y="314" />
    <di:waypoint xsi:type="dc:Point" x="922" y="314" />
    <bpmndi:BPMNLabel>
    <dc:Bounds x="902" y="293" width="0" height="12" />
    </bpmndi:BPMNLabel>
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="DataOutputAssociation_1ugo5a1_di" bpmnElement="DataOutputAssociation_1ugo5a1">
    <di:waypoint xsi:type="dc:Point" x="670" y="354" />
    <di:waypoint xsi:type="dc:Point" x="569" y="453" />
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNEdge id="DataOutputAssociation_0torvjt_di" bpmnElement="DataOutputAssociation_0torvjt">
    <di:waypoint xsi:type="dc:Point" x="825" y="354" />
    <di:waypoint xsi:type="dc:Point" x="569" y="465" />
    </bpmndi:BPMNEdge>
    <bpmndi:BPMNShape id="ServiceTask_13efsgx_di" bpmnElement="Task_1nkplgq">
    <dc:Bounds x="640" y="274" width="100" height="80" />
    </bpmndi:BPMNShape>
    <bpmndi:BPMNShape id="ServiceTask_0k412yb_di" bpmnElement="Task_0d6wbxi">
    <dc:Bounds x="782" y="274" width="100" height="80" />
    </bpmndi:BPMNShape>
    </bpmndi:BPMNPlane>
    </bpmndi:BPMNDiagram>
    </bpmn:definitions>

โดยแต่ละ Service Task มีการเรียกใช้ REST-API Method ที่แตกต่างกันครับ ได้แก่

  • GET
  • PATCH
  • POST
  • DELETE
  • PUT

ทดสอบ Run ครับ โดยผมใช้ Mock API ของ JSON-Server อีกเช่นเคยครับ

  • โดยมีการกำหนดข้อมูลในไฟล์ db.json ดังนี้
    {
      "tests": [
        {
          "id": 1,
          "Test Payload": 1,
          "createdAt": 1530434271285
        },
        {
          "id": 2,
          "Test Payload": 12,
          "createdAt": 1530434271288
        },
        {
          "id": 3,
          "Test Payload": 123,
          "createdAt": 1530434271288
        }
      ]
    }
  • เมื่อกด Run ครับดู Response ที่ได้จากการ Request ไปครับ

สำหรับ Blog นี้เขียนเสร็จ ปุบได้ขอ Request แก้ Document ของตัว Camunda เลย เพราะมันใส่ไม่ครบครับ ^__^


Discover more from naiwaen@DebuggingSoft

Subscribe to get the latest posts to your email.