springboot集成activiti全過(guò)程
springboot集成activiti
pom.xml
<!--activiti-->
<activiti.version>5.22.0</activiti.version>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>${activiti.version}</version>
</dependency>/**使用代碼創(chuàng)建工作流需要的23張表*/
@Test
public void createTable(){
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
//連接數(shù)據(jù)庫(kù)的配置
processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
processEngineConfiguration.setJdbcUrl("jdbc:mysql://localhost:3306/itcast0711activiti?useUnicode=true&characterEncoding=utf8");
processEngineConfiguration.setJdbcUsername("root");
processEngineConfiguration.setJdbcPassword("root");
/**
public static final String DB_SCHEMA_UPDATE_FALSE = "false";不能自動(dòng)創(chuàng)建表,需要表存在
public static final String DB_SCHEMA_UPDATE_CREATE_DROP = "create-drop";先刪除表再創(chuàng)建表
public static final String DB_SCHEMA_UPDATE_TRUE = "true";如果表不存在,自動(dòng)創(chuàng)建表
*/
processEngineConfiguration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
//工作流的核心對(duì)象,ProcessEnginee對(duì)象
ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
System.out.println("processEngine:"+processEngine);
}
注解 需要用到activiti.xfg.xml 吧activiti.xfg.xml放到resources目錄下 不然processEngine 會(huì)報(bào)null
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
activiti.xfg.xml內(nèi)容
<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
updateCheck="false">
<diskStore path="java.io.tmpdir/Tmp_EhCache"/>
<defaultCache eternal="false" maxElementsInMemory="1000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="600" memoryStoreEvictionPolicy="LRU"/>
<cache name="user" eternal="false" maxElementsInMemory="10000"
overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0"
timeToLiveSeconds="0" memoryStoreEvictionPolicy="LFU"/>
</ehcache>
package com.bootdo.activiti.config;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
public class ExclusiveGateWayTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**部署流程定義(從inputStream)*/
@Test
public void deploymentProcessDefinition_inputStream(){
InputStream inputStreamBpmn = this.getClass().getResourceAsStream("exclusiveGateWay.bpmn");
InputStream inputStreamjpg = this.getClass().getResourceAsStream("exclusiveGateWay.jpg");
Deployment deployment = processEngine.getRepositoryService()//與流程定義和部署對(duì)象相關(guān)的Service
.createDeployment()//創(chuàng)建一個(gè)部署對(duì)象
.name("排他網(wǎng)關(guān)")//添加部署的名稱(chēng)
.addInputStream("exclusiveGateWay.bpmn", inputStreamBpmn)//
.addInputStream("exclusiveGateWay.jpg", inputStreamjpg)//
.deploy();//完成部署
System.out.println("部署ID:"+deployment.getId());//
System.out.println("部署名稱(chēng):"+deployment.getName());//
}
/**啟動(dòng)流程實(shí)例*/
@Test
public void startProcessInstance(){
//流程定義的key
String processDefinitionKey = "exclusiveGateWay";
ProcessInstance pi = processEngine.getRuntimeService()//與正在執(zhí)行的流程實(shí)例和執(zhí)行對(duì)象相關(guān)的Service
.startProcessInstanceByKey(processDefinitionKey);//使用流程定義的key啟動(dòng)流程實(shí)例,key對(duì)應(yīng)helloworld.bpmn文件中id的屬性值,使用key值啟動(dòng),默認(rèn)是按照最新版本的流程定義啟動(dòng)
System.out.println("流程實(shí)例ID:"+pi.getId());//流程實(shí)例ID 101
System.out.println("流程定義ID:"+pi.getProcessDefinitionId());//流程定義ID helloworld:1:4
}
/**查詢(xún)當(dāng)前人的個(gè)人任務(wù)*/
@Test
public void findMyPersonalTask(){
String assignee = "王小五";
List<Task> list = processEngine.getTaskService()//與正在執(zhí)行的任務(wù)管理相關(guān)的Service
.createTaskQuery()//創(chuàng)建任務(wù)查詢(xún)對(duì)象
/**查詢(xún)條件(where部分)*/
.taskAssignee(assignee)//指定個(gè)人任務(wù)查詢(xún),指定辦理人
// .taskCandidateUser(candidateUser)//組任務(wù)的辦理人查詢(xún)
// .processDefinitionId(processDefinitionId)//使用流程定義ID查詢(xún)
// .processInstanceId(processInstanceId)//使用流程實(shí)例ID查詢(xún)
// .executionId(executionId)//使用執(zhí)行對(duì)象ID查詢(xún)
/**排序*/
.orderByTaskCreateTime().asc()//使用創(chuàng)建時(shí)間的升序排列
/**返回結(jié)果集*/
// .singleResult()//返回惟一結(jié)果集
// .count()//返回結(jié)果集的數(shù)量
// .listPage(firstResult, maxResults);//分頁(yè)查詢(xún)
.list();//返回列表
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任務(wù)ID:"+task.getId());
System.out.println("任務(wù)名稱(chēng):"+task.getName());
System.out.println("任務(wù)的創(chuàng)建時(shí)間:"+task.getCreateTime());
System.out.println("任務(wù)的辦理人:"+task.getAssignee());
System.out.println("流程實(shí)例ID:"+task.getProcessInstanceId());
System.out.println("執(zhí)行對(duì)象ID:"+task.getExecutionId());
System.out.println("流程定義ID:"+task.getProcessDefinitionId());
System.out.println("########################################################");
}
}
}
/**完成我的任務(wù)*/
@Test
public void completeMyPersonalTask(){
//任務(wù)ID
//select * from act_ru_task; --運(yùn)行時(shí)任務(wù)節(jié)點(diǎn)表id
String taskId = "304";
//完成任務(wù)的同時(shí),設(shè)置流程變量,使用流程變量用來(lái)指定完成任務(wù)后,下一個(gè)連線(xiàn),對(duì)應(yīng)exclusiveGateWay.bpmn文件中${money>1000}
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("money", 200);
processEngine.getTaskService()//與正在執(zhí)行的任務(wù)管理相關(guān)的Service
.complete(taskId,variables);
System.out.println("完成任務(wù):任務(wù)ID:"+taskId);
}
}exclusiveGateWay.bpmn


<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="exclusiveGateWay" name="exclusiveGateWayProcess" isExecutable="true">
<extensionElements>
<activiti:executionListener event="start" class="com.bootdo.activiti.config.ZengStartListener"></activiti:executionListener>
<activiti:executionListener event="end" class="com.bootdo.activiti.config.ZengEndListener"></activiti:executionListener>
</extensionElements>
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="費(fèi)用報(bào)銷(xiāo)申請(qǐng)" activiti:assignee="王小五"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="審批【部門(mén)經(jīng)理】" activiti:assignee="趙小六"></userTask>
<userTask id="usertask3" name="財(cái)務(wù)" activiti:assignee="胡小八"></userTask>
<userTask id="usertask4" name="審批【總經(jīng)理】" activiti:assignee="田小七"></userTask>
<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway" default="默認(rèn)執(zhí)行財(cái)務(wù)"></exclusiveGateway>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
<sequenceFlow id="flow3" name="金額小于等于1000,大于等于500" sourceRef="exclusivegateway1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money>=500 && money<=1000}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="默認(rèn)執(zhí)行財(cái)務(wù)" name="默認(rèn)執(zhí)行財(cái)務(wù)" sourceRef="exclusivegateway1" targetRef="usertask3"></sequenceFlow>
<sequenceFlow id="flow5" name="金額大于1000元" sourceRef="exclusivegateway1" targetRef="usertask4">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money>1000}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow6" sourceRef="usertask2" targetRef="servicetask2"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask3" targetRef="servicetask3"></sequenceFlow>
<sequenceFlow id="flow8" sourceRef="usertask4" targetRef="servicetask1"></sequenceFlow>
<serviceTask id="servicetask1" name="總經(jīng)理Service Task" activiti:class="com.bootdo.activiti.config.ZengServiceListener3"></serviceTask>
<serviceTask id="servicetask2" name="部門(mén)經(jīng)理Service Task" activiti:class="com.bootdo.activiti.config.ZengServiceListener1"></serviceTask>
<serviceTask id="servicetask3" name="財(cái)務(wù)Service Task" activiti:class="com.bootdo.activiti.config.ZengServiceListener2"></serviceTask>
<sequenceFlow id="flow9" sourceRef="servicetask3" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow10" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow11" sourceRef="servicetask2" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_exclusiveGateWay">
<bpmndi:BPMNPlane bpmnElement="exclusiveGateWay" id="BPMNPlane_exclusiveGateWay">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="61.0" width="141.0" x="80.0" y="390.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="300.0" y="393.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
<omgdc:Bounds height="55.0" width="121.0" x="533.0" y="390.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
<omgdc:Bounds height="40.0" width="40.0" x="337.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="335.0" y="650.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
<omgdc:Bounds height="55.0" width="130.0" x="529.0" y="520.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
<omgdc:Bounds height="55.0" width="151.0" x="75.0" y="520.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
<omgdc:Bounds height="55.0" width="141.0" x="282.0" y="522.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="85.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="140.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="357.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="260.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="150.0" y="390.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="64.0" width="100.0" x="181.0" y="292.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="默認(rèn)執(zhí)行財(cái)務(wù)" id="BPMNEdge_默認(rèn)執(zhí)行財(cái)務(wù)">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="352.0" y="393.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="48.0" width="72.0" x="360.0" y="332.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="593.0" y="390.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="48.0" width="84.0" x="430.0" y="310.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="150.0" y="451.0"></omgdi:waypoint>
<omgdi:waypoint x="150.0" y="520.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="352.0" y="448.0"></omgdi:waypoint>
<omgdi:waypoint x="352.0" y="522.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="593.0" y="445.0"></omgdi:waypoint>
<omgdi:waypoint x="594.0" y="520.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
<omgdi:waypoint x="352.0" y="577.0"></omgdi:waypoint>
<omgdi:waypoint x="352.0" y="650.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
<omgdi:waypoint x="594.0" y="575.0"></omgdi:waypoint>
<omgdi:waypoint x="352.0" y="650.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow11" id="BPMNEdge_flow11">
<omgdi:waypoint x="150.0" y="575.0"></omgdi:waypoint>
<omgdi:waypoint x="352.0" y="650.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>監(jiān)聽(tīng)器
開(kāi)始結(jié)束流程監(jiān)聽(tīng)器

開(kāi)始流程監(jiān)聽(tīng)器(啟動(dòng)流程的時(shí)候就會(huì)進(jìn)入這個(gè)類(lèi))
package com.bootdo.activiti.config;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Service;
@Service("zengStartListener")
public class ZengStartListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) throws Exception {
System.err.println("ZengStartListener =====================>");
System.err.println("ZengStartListener =====================>");
System.err.println("ZengStartListener =====================>");
}
}結(jié)束流程監(jiān)聽(tīng)器(結(jié)束流程的時(shí)候就會(huì)進(jìn)入這個(gè)類(lèi))
package com.bootdo.activiti.config;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.springframework.stereotype.Service;
@Service("zengEndListener")
public class ZengEndListener implements ExecutionListener {
@Override
public void notify(DelegateExecution delegateExecution) throws Exception {
System.err.println("ZengEndListener =====================>");
System.err.println("ZengEndListener =====================>");
System.err.println("ZengEndListener =====================>");
}
}
Service Task監(jiān)聽(tīng)器(上一個(gè)流程結(jié)束會(huì)自動(dòng)進(jìn)入service task監(jiān)聽(tīng)器類(lèi))

package com.bootdo.activiti.config;
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.JavaDelegate;
import org.springframework.stereotype.Service;
@Service("zengServiceListener3")
public class ZengServiceListener3 implements JavaDelegate{
//成員變量名稱(chēng)必須和組件中設(shè)置的字段名稱(chēng)保持一致
private Expression expression;
@Override
public void execute(DelegateExecution execution) {
Object o=expression.getValue(execution);
String text=expression.getExpressionText();
System.err.println("ZengServiceListener3執(zhí)行了服務(wù)任務(wù)===");
System.out.println(o);
System.out.println(text);
}
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java使用hutool實(shí)現(xiàn)文件大小的友好輸出
這篇文章主要為大家詳細(xì)介紹了Java如何使用hutool實(shí)現(xiàn)文件大小的友好輸出,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解下2023-11-11
Java+Eclipse+Selenium環(huán)境搭建的方法步驟
這篇文章主要介紹了Java+Eclipse+Selenium環(huán)境搭建的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-06-06
Docker?Compose+Jenkins自動(dòng)化部署流程圖文教程
Jenkins是一款非常流行的開(kāi)源持續(xù)集成工具,廣泛用于項(xiàng)目開(kāi)發(fā),具有自動(dòng)化構(gòu)建、測(cè)試和部署等功能,這篇文章主要介紹了Docker?Compose+Jenkins自動(dòng)化部署流程的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2026-04-04
Gradle的SpringBoot項(xiàng)目構(gòu)建圖解
這篇文章主要介紹了Gradle的SpringBoot項(xiàng)目構(gòu)建圖解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
一篇文章徹底拆解Java?HashMap擴(kuò)容機(jī)制
在Java中HashMap是一個(gè)非常常用的數(shù)據(jù)結(jié)構(gòu),基于哈希表實(shí)現(xiàn),它通過(guò)鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù),這篇文章主要介紹了Java HashMap擴(kuò)容機(jī)制的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下2026-04-04
Java中的HashMap和Hashtable區(qū)別解析
這篇文章主要介紹了Java中的HashMap和Hashtable區(qū)別解析,HashMap和Hashtable都實(shí)現(xiàn)了Map接口,但決定用哪一個(gè)之前先要弄清楚它們之間的區(qū)別,主要的區(qū)別有線(xiàn)程安全性、同步和速度,需要的朋友可以參考下2023-11-11

