目標(biāo)
創(chuàng)建一個大眾流程(請假流程)并且可以跑起來仇参。
需要的預(yù)備知識
camunda數(shù)據(jù)庫表的基本認(rèn)識 表介紹鏈接
流程圖的制作
- 下載camunda的繪圖工具 流程圖工具地址
-
繪制請假流程圖(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:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camunda="http://camunda.org/schema/1.0/bpmn" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" id="Definitions_0vl15c1" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.2.0">
<bpmn:process id="Process_007jh58" isExecutable="true">
<bpmn:startEvent id="StartEvent_1">
<bpmn:outgoing>Flow_1l7o4ts</bpmn:outgoing>
</bpmn:startEvent>
<bpmn:sequenceFlow id="Flow_1l7o4ts" sourceRef="StartEvent_1" targetRef="Activity_1almw37" />
<bpmn:exclusiveGateway id="Gateway_1l7w1cq">
<bpmn:incoming>Flow_05ef0ej</bpmn:incoming>
<bpmn:outgoing>Flow_16oi0x8</bpmn:outgoing>
<bpmn:outgoing>Flow_1yq2gkb</bpmn:outgoing>
</bpmn:exclusiveGateway>
<bpmn:sequenceFlow id="Flow_16oi0x8" name="小于等于7天" sourceRef="Gateway_1l7w1cq" targetRef="Activity_0kms1lj">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${ts <= 7}</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:sequenceFlow id="Flow_1yq2gkb" name="大于七天" sourceRef="Gateway_1l7w1cq" targetRef="Activity_0uw0hfc">
<bpmn:conditionExpression xsi:type="bpmn:tFormalExpression">${ts > 7}</bpmn:conditionExpression>
</bpmn:sequenceFlow>
<bpmn:exclusiveGateway id="Gateway_1sx3v29">
<bpmn:incoming>Flow_0808fzz</bpmn:incoming>
<bpmn:incoming>Flow_0y6yarx</bpmn:incoming>
<bpmn:outgoing>Flow_122u7os</bpmn:outgoing>
</bpmn:exclusiveGateway>
<bpmn:sequenceFlow id="Flow_0808fzz" sourceRef="Activity_0kms1lj" targetRef="Gateway_1sx3v29" />
<bpmn:sequenceFlow id="Flow_0y6yarx" sourceRef="Activity_0uw0hfc" targetRef="Gateway_1sx3v29" />
<bpmn:userTask id="Activity_1almw37" name="申請人" camunda:assignee="sqr">
<bpmn:incoming>Flow_1l7o4ts</bpmn:incoming>
<bpmn:outgoing>Flow_15p6u4g</bpmn:outgoing>
</bpmn:userTask>
<bpmn:sequenceFlow id="Flow_15p6u4g" sourceRef="Activity_1almw37" targetRef="Activity_1wm4vx9" />
<bpmn:sequenceFlow id="Flow_05ef0ej" sourceRef="Activity_1wm4vx9" targetRef="Gateway_1l7w1cq" />
<bpmn:sequenceFlow id="Flow_122u7os" sourceRef="Gateway_1sx3v29" targetRef="Activity_01orex7" />
<bpmn:endEvent id="Event_0cuwhfb">
<bpmn:incoming>Flow_0zfntyp</bpmn:incoming>
</bpmn:endEvent>
<bpmn:sequenceFlow id="Flow_0zfntyp" sourceRef="Activity_01orex7" targetRef="Event_0cuwhfb" />
<bpmn:userTask id="Activity_1wm4vx9" name="申請人上級" camunda:assignee="sjld">
<bpmn:incoming>Flow_15p6u4g</bpmn:incoming>
<bpmn:outgoing>Flow_05ef0ej</bpmn:outgoing>
</bpmn:userTask>
<bpmn:userTask id="Activity_0kms1lj" name="人事經(jīng)理" camunda:assignee="rsjl">
<bpmn:incoming>Flow_16oi0x8</bpmn:incoming>
<bpmn:outgoing>Flow_0808fzz</bpmn:outgoing>
</bpmn:userTask>
<bpmn:userTask id="Activity_0uw0hfc" name="人事總監(jiān)" camunda:assignee="rszj">
<bpmn:incoming>Flow_1yq2gkb</bpmn:incoming>
<bpmn:outgoing>Flow_0y6yarx</bpmn:outgoing>
</bpmn:userTask>
<bpmn:userTask id="Activity_01orex7" name="歸檔" camunda:assignee="sqr">
<bpmn:incoming>Flow_122u7os</bpmn:incoming>
<bpmn:outgoing>Flow_0zfntyp</bpmn:outgoing>
</bpmn:userTask>
</bpmn:process>
</bpmn:definitions>
流程的部署
- 復(fù)制如上的流程圖xml信息到classpath目錄下
- 部署流程
/**
* 部署bpmn旁理。xml資源文件
*/
public void bpmnImport() {
Deployment deploy = repositoryService
.createDeployment()
// .name("請假申請")
.addClasspathResource("diagram_1.bpmn")
.deploy();
log.info(deploy.getName());
log.info(deploy.getId());
}
運(yùn)行后如下表會一些基本信息
流程的運(yùn)行
/**
* 開始一個流程實(shí)例
*/
public void startBpm(String proInsId) {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(proInsId);
String id = processInstance.getId();
log.info(id);
}
流程的審批
/**
* 完成一個任務(wù) 不帶參數(shù)
* @param taskId
*/
public void comTask(String taskId) {
taskService.complete(taskId);
}
/**
* 完成一個任務(wù)先誉, 并且可以設(shè)置變量參數(shù)
* @param taskId
*/
public void comTask(String taskId, Map map) {
taskService.complete(taskId, map);
}
結(jié)束語
至此一個簡單固定的流程就可以成功完成!
示例代碼
服務(wù)代碼
@Slf4j
@Service
public class ResService {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
/**
* 部署bpmn匿值。xml資源文件
*/
public void bpmnImport() {
Deployment deploy = repositoryService
.createDeployment()
.name("請假申請")
.addClasspathResource("diagram_1.bpmn")
.deploy();
log.info(deploy.getName());
log.info(deploy.getId());
}
/**
* 開始一個流程實(shí)例
*/
public void startBpm(String proInsId) {
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(proInsId);
String id = processInstance.getId();
log.info(id);
}
/**
* 完成一個任務(wù) 不帶參數(shù)
* @param taskId
*/
public void comTask(String taskId) {
taskService.complete(taskId);
}
/**
* 完成一個任務(wù), 并且可以設(shè)置變量參數(shù)
* @param taskId
*/
public void comTask(String taskId, Map map) {
taskService.complete(taskId, map);
}
}
測試類代碼
@RunWith(SpringRunner.class)
@SpringBootTest
class ResServiceTest {
@Autowired
private ResService resService;
/**
* 測試導(dǎo)入classpath下的bpmn資源
*/
@Test
void bpmnImport() {
resService.bpmnImport();
}
/**
* 開始一個流程實(shí)例, xml標(biāo)簽內(nèi)的bpmn:process id="Process_007jh58"
*/
@Test
void startBpm() {
resService.startBpm("Process_007jh58");
}
/**
* 完成一個任務(wù)
*/
@Test
void comTask() {
resService.comTask("f4bc7a2a-0a0c-11eb-9b55-c81f66132ec7");
}
/**
* 完成一個任務(wù)髓窜, 帶參數(shù)比如此流程的分叉需要判斷請假天數(shù), 我們需要傳入當(dāng)前的變量天數(shù)
*/
@Test
void comTask1() {
Map map = new HashMap();
map.put("ts", 8);
resService.comTask("5cbe9b2b-0a0d-11eb-9439-c81f66132ec7", map);
}
}