springboot集成activiti全過程
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ù)庫的配置
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);
//工作流的核心對象,ProcessEnginee對象
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()//與流程定義和部署對象相關(guān)的Service
.createDeployment()//創(chuàng)建一個(gè)部署對象
.name("排他網(wǎng)關(guān)")//添加部署的名稱
.addInputStream("exclusiveGateWay.bpmn", inputStreamBpmn)//
.addInputStream("exclusiveGateWay.jpg", inputStreamjpg)//
.deploy();//完成部署
System.out.println("部署ID:"+deployment.getId());//
System.out.println("部署名稱:"+deployment.getName());//
}
/**啟動(dòng)流程實(shí)例*/
@Test
public void startProcessInstance(){
//流程定義的key
String processDefinitionKey = "exclusiveGateWay";
ProcessInstance pi = processEngine.getRuntimeService()//與正在執(zhí)行的流程實(shí)例和執(zhí)行對象相關(guān)的Service
.startProcessInstanceByKey(processDefinitionKey);//使用流程定義的key啟動(dòng)流程實(shí)例,key對應(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
}
/**查詢當(dāng)前人的個(gè)人任務(wù)*/
@Test
public void findMyPersonalTask(){
String assignee = "王小五";
List<Task> list = processEngine.getTaskService()//與正在執(zhí)行的任務(wù)管理相關(guān)的Service
.createTaskQuery()//創(chuàng)建任務(wù)查詢對象
/**查詢條件(where部分)*/
.taskAssignee(assignee)//指定個(gè)人任務(wù)查詢,指定辦理人
// .taskCandidateUser(candidateUser)//組任務(wù)的辦理人查詢
// .processDefinitionId(processDefinitionId)//使用流程定義ID查詢
// .processInstanceId(processInstanceId)//使用流程實(shí)例ID查詢
// .executionId(executionId)//使用執(zhí)行對象ID查詢
/**排序*/
.orderByTaskCreateTime().asc()//使用創(chuàng)建時(shí)間的升序排列
/**返回結(jié)果集*/
// .singleResult()//返回惟一結(jié)果集
// .count()//返回結(jié)果集的數(shù)量
// .listPage(firstResult, maxResults);//分頁查詢
.list();//返回列表
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任務(wù)ID:"+task.getId());
System.out.println("任務(wù)名稱:"+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í)行對象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è)置流程變量,使用流程變量用來指定完成任務(wù)后,下一個(gè)連線,對應(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)銷申請" activiti:assignee="王小五"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="審批【部門經(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="部門經(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)聽器
開始結(jié)束流程監(jiān)聽器

開始流程監(jiān)聽器(啟動(dòng)流程的時(shí)候就會(huì)進(jìn)入這個(gè)類)
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)聽器(結(jié)束流程的時(shí)候就會(huì)進(jìn)入這個(gè)類)
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)聽器(上一個(gè)流程結(jié)束會(huì)自動(dòng)進(jìn)入service task監(jiān)聽器類)

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{
//成員變量名稱必須和組件中設(shè)置的字段名稱保持一致
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)文章
Spring?cloud負(fù)載均衡@LoadBalanced?&?LoadBalancerClient
由于Spring?cloud2020之后移除了Ribbon,直接使用Spring?Cloud?LoadBalancer作為客戶端負(fù)載均衡組件,我們討論Spring負(fù)載均衡以Spring?Cloud2020之后版本為主,學(xué)習(xí)Spring?Cloud?LoadBalance2023-11-11
IDEA 集成log4j將SQL語句打印在控制臺(tái)上的實(shí)現(xiàn)操作
這篇文章主要介紹了IDEA 集成log4j將SQL語句打印在控制臺(tái)上的實(shí)現(xiàn)操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題
這篇文章主要介紹了springboot啟動(dòng)時(shí)候報(bào)錯(cuò)mongodb問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問題
這篇文章主要介紹了解決springboot使用logback日志出現(xiàn)LOG_PATH_IS_UNDEFINED文件夾的問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
Springboot 中使用 Aop代碼實(shí)戰(zhàn)教程
AOP的編程思想是把對類對象的橫切問題點(diǎn),從業(yè)務(wù)邏輯中分離出來,從而達(dá)到解耦的目的,增加代碼的復(fù)用性,提高開發(fā)效率,這篇文章主要介紹了Springboot中使用Aop代碼實(shí)戰(zhàn)教程,需要的朋友可以參考下2023-07-07

