一、Activiti使用步骤
通常使用Activiti时包含以下几个步骤:
1、部署activiti:Activiti包含一堆jar包,因此需要把业务系统和Activiti的环境集成在一起进行部署;
2、定义流程:使用Activiti的建模工具定义业务流程.bpmn文件;
3、部署流程定义:使用Activiti提供的API把流程定义内容存储起来,在Activiti执行过程汇总可以查询定义的内容。Activiti是通过数据库来存储业务流程的;
4、启动流程实例:流程实例也叫ProcessInstance,启动一个流程实例表示开始一次业务流程的运作。例如员工提交请假申请后,就可以开启一个流程实例,从而推动后续的审批等操作;
5、用户查询代办任务(task):因为现在系统的业务流程都交给了activiti管理,通过activiti就可以查询当前流程执行到哪个步骤了。当前用户需要办理哪些任务也就同样可以由activiti帮我们管理,开发人员不需要自己编写sql语句进行查询了;
6、用户办理任务:用户查询到自己的代办任务后,就可以办理某个业务,如果这个业务办理完成还需要其他用户办理,就可以由activiti帮我们把工作流程往后面的步骤推动;
7、流程结束:当任务办理完成没有下一个任务节点后,这个流程实例就执行完成了。
二、准备工作
1、、使用Activiti需要的基本环境包括:JDK8或以上版本,然后需要一个数据库用来保存流程定义数据,建议使用MySQL5或以上版本
2、安装插件
开发工具IDEA,在IDEA中需要安装Activiti的流程定义工具插件actiBPM。目前该插件从2014年11月后就没有再更新,对于IDEA版本只支持到2019.1。新版本的IDEA已经无法从插件市场搜索到该插件。安装时,可以到jetBrain的插件市场(https://plugins.jetbrains.com/)搜索actiBPM插件,下载到本地后,从本地安装该插件。
三、Activiti环境搭建
(一)、新建Maven工程
工程结构:
pom.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <modelVersion>4.0.0</modelVersion> 6 7 <groupId>com.jerry</groupId> 8 <artifactId>activiti</artifactId> 9 <version>1.0-SNAPSHOT</version> 10 11 <properties> 12 <activiti.version>7-201802-EA</activiti.version> 13 <mysql.version>8.0.29</mysql.version> 14 <mybatis.version>3.5.7</mybatis.version> 15 <commons-dbcp.version>1.4</commons-dbcp.version> 16 <commons-io.version>2.6</commons-io.version> 17 <junit.version>4.12</junit.version> 18 <log4j.version>1.2.17</log4j.version> 19 <slf4j.version>1.7.30</slf4j.version> 20 </properties> 21 22 <dependencies> 23 <dependency> 24 <groupId>org.activiti</groupId> 25 <artifactId>activiti-engine</artifactId> 26 <version>${activiti.version}</version> 27 </dependency> 28 <dependency> 29 <groupId>org.activiti</groupId> 30 <artifactId>activiti-spring</artifactId> 31 <version>${activiti.version}</version> 32 </dependency> 33 <!-- bpmn模型处理 --> 34 <dependency> 35 <groupId>org.activiti</groupId> 36 <artifactId>activiti-bpmn-model</artifactId> 37 <version>${activiti.version}</version> 38 </dependency> 39 <!-- bpmn转换 --> 40 <dependency> 41 <groupId>org.activiti</groupId> 42 <artifactId>activiti-bpmn-converter</artifactId> 43 <version>${activiti.version}</version> 44 </dependency> 45 <!-- bpmn json数据转换 --> 46 <dependency> 47 <groupId>org.activiti</groupId> 48 <artifactId>activiti-json-converter</artifactId> 49 <version>${activiti.version}</version> 50 </dependency> 51 <!-- bpmn布局 --> 52 <dependency> 53 <groupId>org.activiti</groupId> 54 <artifactId>activiti-bpmn-layout</artifactId> 55 <version>${activiti.version}</version> 56 </dependency> 57 <!-- mysql驱动 --> 58 <dependency> 59 <groupId>mysql</groupId> 60 <artifactId>mysql-connector-java</artifactId> 61 <version>${mysql.version}</version> 62 </dependency> 63 <!-- mybatis --> 64 <dependency> 65 <groupId>org.mybatis</groupId> 66 <artifactId>mybatis</artifactId> 67 <version>${mybatis.version}</version> 68 </dependency> 69 <!-- 连接池 --> 70 <dependency> 71 <groupId>commons-dbcp</groupId> 72 <artifactId>commons-dbcp</artifactId> 73 <version>${commons-dbcp.version}</version> 74 </dependency> 75 <dependency> 76 <groupId>commons-io</groupId> 77 <artifactId>commons-io</artifactId> 78 <version>${commons-io.version}</version> 79 </dependency> 80 <dependency> 81 <groupId>junit</groupId> 82 <artifactId>junit</artifactId> 83 <version>${junit.version}</version> 84 </dependency> 85 <!-- log start --> 86 <dependency> 87 <groupId>log4j</groupId> 88 <artifactId>log4j</artifactId> 89 <version>${log4j.version}</version> 90 </dependency> 91 <dependency> 92 <groupId>org.slf4j</groupId> 93 <artifactId>slf4j-api</artifactId> 94 <version>${slf4j.version}</version> 95 </dependency> 96 <dependency> 97 <groupId>org.slf4j</groupId> 98 <artifactId>slf4j-log4j12</artifactId> 99 <version>${slf4j.version}</version> 100 </dependency> 101 </dependencies> 102 103 </project>View Code
log4j.properties
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 log4j.rootLogger=debug, console, logfile 2 3 log4j.logger.org.apache.axis.enterprise=fatal, console 4 5 log4j.appender.console=org.apache.log4j.ConsoleAppender 6 log4j.appender.console.layout=org.apache.log4j.PatternLayout 7 log4j.appender.console.layout.ConversionPattern=%d %p [%c]:%L - %m%n 8 9 log4j.appender.logfile=org.apache.log4j.FileAppender 10 log4j.appender.logfile.File=activiti.log 11 log4j.appender.logfile.Append=true 12 log4j.appender.logfile.layout=org.apache.log4j.PatternLayout 13 log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%nView Code
activiti.cfg.xml
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <beans xmlns="http://www.springframework.org/schema/beans" 2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://www.springframework.org/schema/beans 4 http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 6 <!-- 连接池 dbcp --> 7 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 8 <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/> 9 <property name="url" value="jdbc:mysql://localhost:3306/activiti?serverTimezone=GMT%2B8"/> 10 <property name="username" value="root"/> 11 <property name="password" value="root"/> 12 <property name="maxActive" value="3"/> 13 <property name="maxIdle" value="1"/> 14 </bean> 15 16 <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> 17 <!-- 应用数据源 --> 18 <property name="dataSource" ref="dataSource"/> 19 <!-- activiti数据库表处理策略 --> 20 <property name="databaseSchemaUpdate" value="true"/> 21 </bean> 22 23 </beans>View Code
(二)、新建leave.bpmn
流程配置:
将提交请假申请节点的Assignee配置为worker,部门经理审批的节点的Assignee配置为manager,财务审批的Assignee配置为financer
注意:
1、IDEA2020以后版本默认不显示:BPMN Editor,演示使用IDEA版本2019.3
2、bpmn文件编辑后关掉,再次打开,如果有中文乱码问题,解决:
Help->Edit Custom VM Options...
最后一行加上:-Dfile.encoding=UTF-8即可。
(三)、转换png图片
将leave.bpmn复制并重命名leave.bpmn.xml,右键:Diagrams->Show BPMN 2.0 Designer...打开,点击上方导出按钮,将图片导出到同目录下并重命名为leave.png。
(四)、生成activiti的数据库表
在mysql中新建数据库:activiti
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Test 2 public void testCreateTable() { 3 // 默认创建方式 4 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 5 System.out.println(processEngine); 6 }View Code
成为25张表:
(五)测试
1、部署流程定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 部署流程定义 文件上传方式 3 */ 4 @Test 5 public void testDeployment() { 6 // 1、创建ProcessEngine 7 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 8 // 2、得到RepositoryService实例 9 RepositoryService repositoryService = processEngine.getRepositoryService(); 10 // 3、使用RepositoryService进行部署 11 Deployment deployment = repositoryService.createDeployment() 12 .addClasspathResource("bpmn/leave.bpmn") // 添加bpmn资源 13 .addClasspathResource("bpmn/leave.png") // 添加png资源 14 .name("员工请假审批流程") 15 .deploy(); 16 // 4、输出部署信息 17 System.out.println("流程部署id:" + deployment.getId()); 18 System.out.println("流程部署名称:" + deployment.getName()); 19 } 20 21 /** 22 * zip压缩文件上传方式(需要将leave.bpmn和leave.png一起压缩为leave.zip包) 23 */ 24 @Test 25 public void testDeploymentByZip() { 26 // 定义zip输入流 27 InputStream is = this.getClass().getClassLoader().getResourceAsStream("bpmn/leave.zip"); 28 ZipInputStream zis = new ZipInputStream(is); 29 // 获取repositoryService 30 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 31 RepositoryService repositoryService = processEngine.getRepositoryService(); 32 // 流程部署 33 Deployment deployment = repositoryService.createDeployment().addZipInputStream(zis).name("员工请假审批流程").deploy(); 34 System.out.println("流程部署id:" + deployment.getId()); 35 System.out.println("流程部署名称:" + deployment.getName()); 36 }View Code
注意:部署报错
发现了以元素 'process' 开头的无效内容。应以 '{
"http://www.omg.org/spec/BPMN/20100524/MODEL":import,
"http://www.omg.org/spec/BPMN/20100524/MODEL":extension,
"http://www.omg.org/spec/BPMN/20100524/MODEL":rootElement,
"http://www.omg.org/spec/BPMN/20100524/DI":BPMNDiagram,
"http://www.omg.org/spec/BPMN/20100524/MODEL":relationship
}' 之一开头。
解决:
记事本打开leave.bpmn,删除<process/>标签中无效的xmlns=””即可。
2、启动流程实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 启动流程实例 3 */ 4 @Test 5 public void testStartProcess() { 6 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 7 RuntimeService runtimeService = processEngine.getRuntimeService(); 8 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leaveId"); 9 System.out.println("流程定义id:" + processInstance.getProcessDefinitionId()); 10 System.out.println("流程实例id:" + processInstance.getId()); 11 System.out.println("当前活动id:" + processInstance.getActivityId()); 12 }View Code
3、查询当前个人待执行的任务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 查询当前个人待执行的任务 3 */ 4 @Test 5 public void testFindPersonalTaskList() { 6 String assignee = "worker"; 7 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 8 TaskService taskService = processEngine.getTaskService(); 9 List<Task> list = taskService.createTaskQuery().processDefinitionKey("leaveId").taskAssignee(assignee).list(); 10 for (Task task : list) { 11 System.out.println("流程实例id:" + task.getProcessInstanceId()); 12 System.out.println("任务id:" + task.getId()); 13 System.out.println("任务负责人:" + task.getAssignee()); 14 System.out.println("任务名称:" + task.getName()); 15 } 16 }View Code
4、完成任务
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 完成任务 3 */ 4 @Test 5 public void testCompleteTask() { 6 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 7 TaskService taskService = processEngine.getTaskService(); 8 Task task = taskService.createTaskQuery().processDefinitionKey("leaveId").taskAssignee("worker").singleResult(); 9 taskService.complete(task.getId()); 10 }View Code
5、查询流程定义
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 查询流程定义 3 */ 4 @Test 5 public void testQueryProcessDefinition() { 6 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 7 RepositoryService repositoryService = processEngine.getRepositoryService(); 8 ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); 9 List<ProcessDefinition> definitionList = processDefinitionQuery.processDefinitionKey("leaveId").orderByProcessDefinitionVersion().desc().list(); 10 for (ProcessDefinition processDefinition : definitionList) { 11 System.out.println("流程定义:" + processDefinition.getId()); 12 System.out.println("流程定义name:" + processDefinition.getName()); 13 System.out.println("流程定义key:" + processDefinition.getKey()); 14 System.out.println("流程定义version:" + processDefinition.getVersion()); 15 System.out.println("流程部署id:" + processDefinition.getDeploymentId()); 16 } 17 }View Code
6、查询流程实例
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 查询流程实例 3 */ 4 @Test 5 public void testQueryProcessInstance() { 6 String processDefinitionKey = "leaveId"; 7 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 8 RuntimeService runtimeService = processEngine.getRuntimeService(); 9 List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().processDefinitionKey(processDefinitionKey).list(); 10 for (ProcessInstance processInstance : list) { 11 System.out.println("---------------------"); 12 System.out.println("流程实例id:" + processInstance.getProcessInstanceId()); 13 System.out.println("所属流程定义id:" + processInstance.getProcessDefinitionId()); 14 System.out.println("是否执行完成:" + processInstance.isEnded()); 15 System.out.println("是否暂停:" + processInstance.isSuspended()); 16 System.out.println("当前活动标识:" + processInstance.getActivityId()); 17 System.out.println("业务关键字:" + processInstance.getBusinessKey()); 18 } 19 }View Code
7、删除流程
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 删除流程 3 */ 4 @Test 5 public void testDeleteDeployment() { 6 String deploymentId = "1"; 7 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 8 RepositoryService repositoryService = processEngine.getRepositoryService(); 9 // 有流程实例启动不可以删除 10 repositoryService.deleteDeployment(deploymentId); 11 // 设置true,级联删除流程定义,即使该流程有流程实例启动也可以删除,设置为false,非级联删除方式 12 // repositoryService.deleteDeployment(deploymentId, true); 13 }View Code
8、下载文件
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @Test 2 public void testQueryBpmnFile() throws Exception { 3 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 4 RepositoryService repositoryService = processEngine.getRepositoryService(); 5 ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leaveId").singleResult(); 6 String deploymentId = processDefinition.getDeploymentId(); 7 InputStream pngInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getDiagramResourceName()); 8 InputStream bpmnInput = repositoryService.getResourceAsStream(deploymentId, processDefinition.getResourceName()); 9 File filePng = new File("g:/leave.png"); 10 File fileBpmn = new File("g:/leave.bpmn"); 11 FileOutputStream pngOutput = new FileOutputStream(filePng); 12 FileOutputStream bpmnOutput = new FileOutputStream(fileBpmn); 13 IOUtils.copy(pngInput, pngOutput); 14 IOUtils.copy(bpmnInput, bpmnOutput); 15 bpmnOutput.close(); 16 pngOutput.close(); 17 bpmnInput.close(); 18 pngInput.close(); 19 }View Code
9、查看历史信息
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 查看历史信息 3 */ 4 @Test 5 public void testFindHistoryInfo() { 6 ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); 7 HistoryService historyService = processEngine.getHistoryService(); 8 HistoricActivityInstanceQuery instanceQuery = historyService.createHistoricActivityInstanceQuery(); 9 instanceQuery.processInstanceId("2501"); 10 instanceQuery.orderByHistoricActivityInstanceStartTime().asc(); 11 List<HistoricActivityInstance> activityInstanceList = instanceQuery.list(); 12 for (HistoricActivityInstance instance : activityInstanceList) { 13 System.out.println(instance.getActivityId()); 14 System.out.println(instance.getActivityName()); 15 System.out.println(instance.getProcessDefinitionId()); 16 System.out.println(instance.getProcessInstanceId()); 17 System.out.println("================"); 18 } 19 }View Code
标签:activiti,流程,基础,System,工作,Activiti7,println,processEngine,out From: https://www.cnblogs.com/plusjerry/p/17035794.html