文章目录
前言
写一个简单的小案例,来分析Activiti7的执行过程,以某公司请假审批的流程为案例进行如下分析
开始—>员工提出请假申请—>项目经理审批—>老板审批—>财务审批—>结束
一、环境准备
开发工具:IIntelliJ IDEA 2023.2.1
开发语言:Java
构建架构:基于Maven构建
数据库:MySQL8.0.25版本
二、搭建开发环境
1、导入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>activiti</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<activiti.version>7.0.0.Beta1</activiti.version>
</properties>
<dependencies>
<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>
<!-- bpmn 模型处理 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn json数据转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 布局 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- activiti 云支持 -->
<dependency>
<groupId>org.activiti.cloud</groupId>
<artifactId>activiti-cloud-services-api</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.27</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<!-- 链接池 -->
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!-- log start -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>1.4</version>
</dependency>
</dependencies>
</project>
2、编写activiti.cfg.xml配置文件
注意:如果是第一次操作的小伙伴,建议“activiti.cfg.xml”这个文件名字不要随便改,就按照这个名字命名放在Resource文件夹下就好,因为activiti7源码中默认加载的配置文件就是以这个名字命名的,如果名字对不上,可能会造成后面的程序启动报错。
activiti.cfg.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contex
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 默认id对应的值 为processEngineConfiguration -->
<!-- processEngine Activiti的流程引擎 -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcDriver" value="com.mysql.cj.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/activiti?useSSL=false&characterSet=utf8&nullCatalogMeansCurrent=true"/>
<property name="jdbcUsername" value="root"/>
<property name="jdbcPassword" value="123456"/>
<!-- activiti数据库表处理策略 -->
<property name="databaseSchemaUpdate" value="true"/>
</bean>
</beans>
文件中的databaseSchemaUpdate属性,true表示如果项目启动后发现数据库中没有对应activiti的表结构,那么就会自动创建,有则不创建,false表示每次都会创建新的,这里建议用true就好。
3、编写log4j.properties配置文件
# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=INFO, CONSOLE debug info warn error fatal
log4j.rootCategory=debug, CONSOLE, LOGFILE
# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=FATAL, CONSOLE
# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m\n
# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.FileAppender
log4j.appender.LOGFILE.File=D:\logs\activitt.log
log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%d{ISO8601} %-6r[%15.15t] %-5p %30.30c %x - %m\n
4、画流程图(请假流程)
这里建议大家在Reource文件下创建一个新的文件夹用来存储bpmn文件,方便管理,对于找不到创建bpmn文件的同志可以看下我之前的博客,里面有详细介绍。
最后将这几个节点连接起来,导出PNG图片即可,文件和图片我放下边了,需要的通知可以自行拷贝
askForLeave.bpmn20.xml
<?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/processdef">
<process id="askForLeave" name="askForLeave" isExecutable="true">
<startEvent id="sid-83f92cf4-58cd-4a9e-b717-53f0a10c1aa5" name="开始请假流程"/>
<userTask id="sid-aeef678e-f52e-45f3-8b29-35d940219c6b" name="提交请假材料" activiti:assignee="员工"/>
<userTask id="sid-103ef116-72e4-4ae9-982e-ff0bb6b29693" name="项目经理审批" activiti:assignee="项目经理"/>
<userTask id="sid-5ce4e251-5218-4171-8503-d847edf9df67" name="老板审批" activiti:assignee="老板"/>
<userTask id="sid-0fc37565-ebb1-47d0-85f8-28907c856749" name="财务审批" activiti:assignee="财务"/>
<endEvent id="sid-9ecbbb09-d1e1-455c-ab02-565bc9481bb1" name="请假流程结束"/>
<sequenceFlow id="sid-158b648f-1edf-4934-bc8a-696e4e7e2d82" sourceRef="sid-aeef678e-f52e-45f3-8b29-35d940219c6b" targetRef="sid-103ef116-72e4-4ae9-982e-ff0bb6b29693"/>
<sequenceFlow id="sid-dab466fa-441f-4a3e-8806-02fef1911e70" sourceRef="sid-103ef116-72e4-4ae9-982e-ff0bb6b29693" targetRef="sid-5ce4e251-5218-4171-8503-d847edf9df67"/>
<sequenceFlow id="sid-1325f4f3-421f-438a-b92e-048f8eb36374" sourceRef="sid-5ce4e251-5218-4171-8503-d847edf9df67" targetRef="sid-0fc37565-ebb1-47d0-85f8-28907c856749"/>
<sequenceFlow id="sid-b68c6714-13db-4cdd-ba03-cb3452499a8d" sourceRef="sid-0fc37565-ebb1-47d0-85f8-28907c856749" targetRef="sid-9ecbbb09-d1e1-455c-ab02-565bc9481bb1"/>
<sequenceFlow id="sid-8a994d16-0ced-4bec-aed4-057044049898" sourceRef="sid-83f92cf4-58cd-4a9e-b717-53f0a10c1aa5" targetRef="sid-aeef678e-f52e-45f3-8b29-35d940219c6b"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_askForLeave">
<bpmndi:BPMNPlane bpmnElement="askForLeave" id="BPMNPlane_askForLeave">
<bpmndi:BPMNShape id="shape-1ac71483-618d-4dd2-a0b8-9f9b793947c7" bpmnElement="sid-83f92cf4-58cd-4a9e-b717-53f0a10c1aa5">
<omgdc:Bounds x="-60.0" y="-155.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-e76a8b93-463e-484e-9510-26e9056d9061" bpmnElement="sid-aeef678e-f52e-45f3-8b29-35d940219c6b">
<omgdc:Bounds x="-95.0" y="-70.0" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-859e0e0b-0e86-4d06-b7c7-31a9b7a64625" bpmnElement="sid-103ef116-72e4-4ae9-982e-ff0bb6b29693">
<omgdc:Bounds x="-95.0" y="40.0" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-852a1f8b-73b0-4fb2-a08c-10d9c5522b0a" bpmnElement="sid-5ce4e251-5218-4171-8503-d847edf9df67">
<omgdc:Bounds x="-95.0" y="145.0" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-6979b08e-0eeb-4774-99ed-28a5fc7999c9" bpmnElement="sid-0fc37565-ebb1-47d0-85f8-28907c856749">
<omgdc:Bounds x="-95.0" y="250.00002" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-6d69b3b7-5e23-4048-8b9e-517ea5a0bdff" bpmnElement="sid-9ecbbb09-d1e1-455c-ab02-565bc9481bb1">
<omgdc:Bounds x="-60.0" y="355.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-1885e7e5-6184-4a9b-b36e-e86df12b5e68" bpmnElement="sid-158b648f-1edf-4934-bc8a-696e4e7e2d82">
<omgdi:waypoint x="-45.0" y="10.0"/>
<omgdi:waypoint x="-45.0" y="40.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-83751a62-454e-480a-bfcb-5bc18dec348a" bpmnElement="sid-dab466fa-441f-4a3e-8806-02fef1911e70">
<omgdi:waypoint x="-45.0" y="120.0"/>
<omgdi:waypoint x="-45.0" y="145.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-cdcaec3d-4a9f-4b96-9e9f-47235fec6634" bpmnElement="sid-1325f4f3-421f-438a-b92e-048f8eb36374">
<omgdi:waypoint x="-45.0" y="225.0"/>
<omgdi:waypoint x="-45.0" y="250.00002"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-2a720687-763f-40c9-9554-65b0cb1434a0" bpmnElement="sid-b68c6714-13db-4cdd-ba03-cb3452499a8d">
<omgdi:waypoint x="-45.0" y="330.0"/>
<omgdi:waypoint x="-45.0" y="355.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-161be1bd-2ed7-4326-b51b-9fcdb36215ae" bpmnElement="sid-8a994d16-0ced-4bec-aed4-057044049898">
<omgdi:waypoint x="-45.0" y="-125.0"/>
<omgdi:waypoint x="-45.0" y="-70.0"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
三、创建activiti7的表结构
Activiti7有一个核心类ProcessEngine,获取其类对象的方式有很多种,下面介绍本人常用的两种方式
// 这种方式要求配置文件必须放在resource下,且必须以activiti.cfg.xml命名
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 使用自定义方式
// 配置文件的名字可以自定义,bean的名字也可以自定义
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.
createProcessEngineConfigurationFromResource("activiti.cfg.xml",
"processEngineConfiguration");