camunda_04_quickstart
目標
- 設計和部署第一個簡單流程
- 使用Java實現流程處理
- 完成流程啟動測試
設計第一個流程
建模注意事項
- 模型文件和部署名其實并不重要,最重要是流程的
Id, ProcessEngine是按照這個Id管理流程. - 流程要設定為
Executable.
步驟
-
使用Model新建一個 camunda 7的模型文件, 命名為payment.bpm , 流程Id 設定為
payment-retrieval, 并完成部署.


-
在流程中增加一個Service task, 類型選
External,Topic設定為charge-card


-
BPMN xml 代碼
<?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:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:modeler="http://camunda.org/schema/modeler/1.0" id="Definitions_1xvrd7w" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="5.2.0" modeler:executionPlatform="Camunda Platform" modeler:executionPlatformVersion="7.17.0">
<bpmn:process id="payment-retrieval" name="付款流程" isExecutable="true">
<bpmn:startEvent id="StartEvent_1" name="付款請求">
<bpmn:outgoing>Flow_0mcdkpl</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_0mcdkpl" sourceRef="StartEvent_1" targetRef="Activity_Charge" />
<bpmn:serviceTask id="Activity_Charge" name="刷卡付款" camunda:type="external" camunda:topic="charge-card">
<bpmn:incoming>Flow_0mcdkpl</bpmn:incoming>
<bpmn:outgoing>Flow_16yugnw</bpmn:outgoing>
</bpmn:serviceTask>
<bpmn:endEvent id="EndEvent_1" name="付款完成">
<bpmn:incoming>Flow_16yugnw</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_16yugnw" sourceRef="Activity_Charge" targetRef="EndEvent_1" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="payment-retrieval">
<bpmndi:BPMNEdge id="Flow_0mcdkpl_di" bpmnElement="Flow_0mcdkpl">
<di:waypoint x="215" y="117" />
<di:waypoint x="270" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="Flow_16yugnw_di" bpmnElement="Flow_16yugnw">
<di:waypoint x="370" y="117" />
<di:waypoint x="432" y="117" />
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="_BPMNShape_StartEvent_2" bpmnElement="StartEvent_1">
<dc:Bounds x="179" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="175" y="142" width="45" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Activity_1dxqxj0_di" bpmnElement="Activity_Charge">
<dc:Bounds x="270" y="77" width="100" height="80" />
<bpmndi:BPMNLabel />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0qnrvvd_di" bpmnElement="EndEvent_1">
<dc:Bounds x="432" y="99" width="36" height="36" />
<bpmndi:BPMNLabel>
<dc:Bounds x="428" y="142" width="45" height="14" />
</bpmndi:BPMNLabel>
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
開發Java實現刷卡付款業務
maven 國內源
maven 配置文件設置國內源, 文件:conf\settings.xml
<mirrors>
<mirror>
<id>aliyun</id>
<name>mirror from maven aliyun</name>
<url>https://maven.aliyun.com/repository/central</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
pom.xml
</dependency>
<dependency>
<groupId>org.camunda.bpm</groupId>
<artifactId>camunda-external-task-client</artifactId>
<version>7.17.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.1</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.1</version>
</dependency>
</dependencies>
java 業務代碼
camunda 的 ExternalTaskClient 是一個Listener類, 我們程序使用 ExternalTaskClient 就可以成為一個后臺daemon程序, 后面再專門寫個博客分析一下 camunda-external-task-client jar.
在我們的程序中, 使用ExternalTaskClient可以非常方便監聽流程中的External Task Service設定的 topic, 完成業務處理, 并標記該Task為結束.
package javatest;
import java.text.MessageFormat;
import java.util.logging.Logger;
import org.camunda.bpm.client.ExternalTaskClient;
public class ChargeCardWorker {
private final static Logger logger = Logger.getLogger(ChargeCardWorker.class.getName());
public static void main(String[] args) {
logger.info("ExternalTaskClient setup");
ExternalTaskClient client = ExternalTaskClient.create().baseUrl("http://localhost:8080/engine-rest")
.asyncResponseTimeout(10000).build();
// 訂閱并處理第1個ExternalTask的topic
client.subscribe("charge-card").lockDuration(1000).handler((externalTask, externalTaskService) -> {
// 獲取流程信息
String item = (String) externalTask.getVariable("item");
Long amount = (Long) externalTask.getVariable("amount");
// 業務邏輯
logger.info(
MessageFormat.format("Charging credit card with an amount of {0} for the item {1}", amount, item));
// 完成 Task
externalTaskService.complete(externalTask);
}).open();
// 訂閱并處理第2個ExternalTask的topic
// 省略
logger.info("Listening topics...");
}
}
啟動流程
確保java程序提前運行, 然后使用VS code+Rest Client 發起一個啟動流程命令:
POST http://localhost:8080/engine-rest/process-definition/key/payment-retrieval/start HTTP/1.1
Content-Type: application/json
{
"variables": {
"amount": {
"value":555,
"type":"long"
},
"item": {
"value": "item-xyz"
}
}
}
Rest API的返回信息:


Java程序端的輸出:



浙公網安備 33010602011771號