当前位置:   article > 正文

springboot项目集成activiti工作流引擎_springboot集成工作流

springboot集成工作流

1、目前我所了解到的activiti工作流想要在项目中使用分为两种:

一、一种较为简单,只需要使用idea的插件来画流程图。(我所使用的的插件是Activiti BPMN visualizer)

二、另一种就是可以使用(在线流程设计器)或者(页面设计器部署到自己项目中)来画流程图。

本文是将流程设计页面部署到了项目中

2、先来看看两种方式的效果吧,是否满足您所需要的结果。

一、使用idea的插件来画流程图。

 二、流程设计页面画流程图。 

3、那么现在开始实现上面两种流图以及部署启动流程

一、先来第一种方式(比较简单)好入手,对于第一次接触的可以很好的去了解到这个Activiti流程引擎的过程。

首先你需要了解activiti的 25张表(这里就不多说了,网上很好找到)

1)、先导入相关的activiti依赖(一定要注意版本号)

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.activiti</groupId>
  4. <artifactId>activiti-spring-boot-starter-basic</artifactId>
  5. <version>5.22.0</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.activiti</groupId>
  9. <artifactId>activiti-rest</artifactId>
  10. <version>5.22.0</version>
  11. </dependency>
  12. <dependency>
  13. <groupId>org.activiti</groupId>
  14. <artifactId>activiti-diagram-rest</artifactId>
  15. <version>5.22.0</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>org.activiti</groupId>
  19. <artifactId>activiti-spring</artifactId>
  20. <version>5.22.0</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.activiti</groupId>
  24. <artifactId>activiti-json-converter</artifactId>
  25. <version>5.22.0</version>
  26. </dependency>
  27. </dependencies>

2)、配置相关 activiti.xml 文件连接数据库,用来生成 25张表

        需要注意(xml文件名和xml中id="processEngineConfiguration"不要乱改)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/contex
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx.xsd">
  12. <!-- 这里可以使用 链接池 dbcp-->
  13. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  14. <property name="driverClassName" value="com.mysql.cj.jdbc.Driver" />
  15. <property name="url" value="jdbc:mysql://localhost:3306/activiti?serverTimezone=Asia/Shanghai"/>
  16. <property name="username" value="*****" />
  17. <property name="password" value="*****" />
  18. <property name="maxActive" value="3" />
  19. <property name="maxIdle" value="1" />
  20. </bean>
  21. <bean id="processEngineConfiguration"
  22. class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
  23. <property name="dataSource" ref="dataSource" />
  24. <property name="databaseSchemaUpdate" value="true"/>
  25. </bean>
  26. </beans>

3)、配置好需要测试类启动生成表

  1. package com.atxinxin;
  2. import org.activiti.engine.ProcessEngine;
  3. import org.activiti.engine.ProcessEngineConfiguration;
  4. import org.junit.jupiter.api.Test;
  5. /**
  6. * @author weixinxin 2023-06-28
  7. **/
  8. public class TestCreateTable {
  9. @Test
  10. public void testCreat(){
  11. ProcessEngineConfiguration processEngineConfiguration =
  12. ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.xml","processEngineConfiguration");
  13. ProcessEngine processEngine = processEngineConfiguration.buildProcessEngine();
  14. System.out.println("processEngine = " + processEngine);
  15. }
  16. }

 4)、结构目录

 5)、生成的数据表

 6)、接下来需要画流程图,插件开头已经提过,这里不说了,在resources包下创建一个bpmn包用来放流程图的xml文件和bpmn文件,右键新建一个文件例如(activiti.bpmn20.xml)

 7)、右键最下面打开Diagram界面

8)、再空白页面右键有各个事件(介绍三个必不可少的)启动、节点、结束

 

 

 

 10)、到这里会玩的大概也差不多了,怎么画就不详细说了,我这里把我的 evection.bpmn20.xml 放出来。对于png文件把后缀名改成png就可以

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <process id="evection" name="出差申请" isExecutable="true">
  4. <startEvent id="sid-918d4c76-a682-4a1d-8160-c853a2293fc0" name="myEvection"/>
  5. <userTask id="sid-efbbfc5f-d4af-4e5b-9f2c-6ac2be4fb0de" name="创建出差申请" activiti:assignee="zhangsan"/>
  6. <sequenceFlow id="sid-01be7034-9f59-4812-9d89-42603748a9f7" sourceRef="sid-918d4c76-a682-4a1d-8160-c853a2293fc0" targetRef="sid-efbbfc5f-d4af-4e5b-9f2c-6ac2be4fb0de"/>
  7. <endEvent id="sid-728c4461-5967-42d9-99dd-973e989dc6b9" name="endEvection"/>
  8. <sequenceFlow id="sid-6381cc34-a651-4d8e-aa9e-1639bd0c765a" sourceRef="sid-efbbfc5f-d4af-4e5b-9f2c-6ac2be4fb0de" targetRef="sid-34a543c6-7f56-4717-81e3-ac3e9f781f68"/>
  9. <userTask id="sid-34a543c6-7f56-4717-81e3-ac3e9f781f68" name="经理审批" activiti:assignee="lisi"/>
  10. <userTask id="sid-7bc0b739-807a-449f-9280-efad4650434d" name="总经理审批" activiti:assignee="wangwu"/>
  11. <userTask id="sid-8ac9a522-b178-4536-bf6c-3354a303301b" name="财务审批" activiti:assignee="zhaoliu"/>
  12. <sequenceFlow id="sid-6c096aae-97dd-4a4b-bac9-d14f540a960f" sourceRef="sid-34a543c6-7f56-4717-81e3-ac3e9f781f68" targetRef="sid-7bc0b739-807a-449f-9280-efad4650434d"/>
  13. <sequenceFlow id="sid-e7022864-0523-4dd4-a652-04a80dd83730" sourceRef="sid-7bc0b739-807a-449f-9280-efad4650434d" targetRef="sid-8ac9a522-b178-4536-bf6c-3354a303301b"/>
  14. <sequenceFlow id="sid-2e3106c8-c44e-436d-8873-20c1351f8041" sourceRef="sid-8ac9a522-b178-4536-bf6c-3354a303301b" targetRef="sid-728c4461-5967-42d9-99dd-973e989dc6b9"/>
  15. </process>
  16. <bpmndi:BPMNDiagram id="BPMNDiagram_evection">
  17. <bpmndi:BPMNPlane bpmnElement="evection" id="BPMNPlane_evection">
  18. <bpmndi:BPMNShape id="shape-0398c401-36bb-469a-8d7d-2f7cc9c4155e" bpmnElement="sid-918d4c76-a682-4a1d-8160-c853a2293fc0">
  19. <omgdc:Bounds x="-65.0" y="-55.0" width="30.0" height="30.0"/>
  20. </bpmndi:BPMNShape>
  21. <bpmndi:BPMNShape id="shape-759f1834-be0a-4ca0-bca5-8c4ca232b22a" bpmnElement="sid-efbbfc5f-d4af-4e5b-9f2c-6ac2be4fb0de">
  22. <omgdc:Bounds x="0.0" y="-80.0" width="100.0" height="80.0"/>
  23. </bpmndi:BPMNShape>
  24. <bpmndi:BPMNEdge id="edge-e9e3e744-8b73-4c26-8266-1534dd6f6e0e" bpmnElement="sid-01be7034-9f59-4812-9d89-42603748a9f7">
  25. <omgdi:waypoint x="-35.0" y="-40.0"/>
  26. <omgdi:waypoint x="0.0" y="-40.0"/>
  27. </bpmndi:BPMNEdge>
  28. <bpmndi:BPMNShape id="shape-81cdb314-1800-4251-bf37-c5905fa74294" bpmnElement="sid-728c4461-5967-42d9-99dd-973e989dc6b9">
  29. <omgdc:Bounds x="-75.0" y="45.0" width="30.0" height="30.0"/>
  30. </bpmndi:BPMNShape>
  31. <bpmndi:BPMNEdge id="edge-b3dae946-cff0-4424-85da-d5a1663bab38" bpmnElement="sid-6381cc34-a651-4d8e-aa9e-1639bd0c765a">
  32. <omgdi:waypoint x="100.0" y="-40.0"/>
  33. <omgdi:waypoint x="170.0" y="-40.0"/>
  34. </bpmndi:BPMNEdge>
  35. <bpmndi:BPMNShape id="shape-e5dcff26-8968-49b2-9ffb-e8af02dde13d" bpmnElement="sid-34a543c6-7f56-4717-81e3-ac3e9f781f68">
  36. <omgdc:Bounds x="170.0" y="-80.0" width="100.0" height="80.0"/>
  37. </bpmndi:BPMNShape>
  38. <bpmndi:BPMNShape id="shape-dc398d5e-7227-4a25-8787-51e4544d9c31" bpmnElement="sid-7bc0b739-807a-449f-9280-efad4650434d">
  39. <omgdc:Bounds x="170.0" y="20.0" width="100.0" height="80.0"/>
  40. </bpmndi:BPMNShape>
  41. <bpmndi:BPMNShape id="shape-54b39e0b-98ac-4d20-88dc-df942e84e52d" bpmnElement="sid-8ac9a522-b178-4536-bf6c-3354a303301b">
  42. <omgdc:Bounds x="0.0" y="20.0" width="100.0" height="80.0"/>
  43. </bpmndi:BPMNShape>
  44. <bpmndi:BPMNEdge id="edge-33a2f259-0113-4f01-adf2-bcc42e680591" bpmnElement="sid-6c096aae-97dd-4a4b-bac9-d14f540a960f">
  45. <omgdi:waypoint x="220.0" y="0.0"/>
  46. <omgdi:waypoint x="220.0" y="20.0"/>
  47. </bpmndi:BPMNEdge>
  48. <bpmndi:BPMNEdge id="edge-14615a3e-cad3-4f3d-a378-e8fff55ddc33" bpmnElement="sid-e7022864-0523-4dd4-a652-04a80dd83730">
  49. <omgdi:waypoint x="170.0" y="60.0"/>
  50. <omgdi:waypoint x="100.0" y="60.0"/>
  51. </bpmndi:BPMNEdge>
  52. <bpmndi:BPMNEdge id="edge-a275de64-4c05-4b3b-a719-70fa26145a5a" bpmnElement="sid-2e3106c8-c44e-436d-8873-20c1351f8041">
  53. <omgdi:waypoint x="0.0" y="60.0"/>
  54. <omgdi:waypoint x="-45.0" y="60.0"/>
  55. </bpmndi:BPMNEdge>
  56. </bpmndi:BPMNPlane>
  57. </bpmndi:BPMNDiagram>
  58. </definitions>

 这里 创建申请-zhansan  经理-lisi  总经理-wangwu  财务-zhaoliu

11)、部署流程图 部署完成查看act_re_deployment表

  1. @Test
  2. public void testDeployment(){
  3. // 1.创建processEngine
  4. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  5. // 2.获取RepositoryService
  6. RepositoryService repositoryService = processEngine.getRepositoryService();
  7. // 3.使用service进行流程部署 ,定义流程名,把 bpmn 和 png 部署到数据库
  8. Deployment deployment = repositoryService.createDeployment()
  9. .name("出差申请流程")
  10. .addClasspathResource("bpmn/evection.bpmn20.xml")
  11. .addClasspathResource("bpmn/evection.png")
  12. .deploy();
  13. System.out.println("流程部署id = " + deployment.getId());
  14. System.out.println("流程部署name = " + deployment.getName());
  15. }

 12)、启动流程实例

  1. /**
  2. *@Description 启动流程实例
  3. *@author weixinxin
  4. *@Date 17:17 2023/6/28
  5. **/
  6. @Test
  7. public void testStartProcess(){
  8. // 1. 创建ProcessEngine
  9. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  10. // 2. 获取RuntimeService
  11. RuntimeService runtimeService = processEngine.getRuntimeService();
  12. // 3. 根据流程定义的id启动流程
  13. ProcessInstance myEvection = runtimeService.startProcessInstanceByKey("evection");
  14. System.out.println("流程定义id = " + myEvection.getProcessDefinitionId());
  15. System.out.println("流程实例id = " + myEvection.getId());
  16. System.out.println("当前活动id = " + myEvection.getActivityId());
  17. }

 13)、查询个人待办任务

  1. /**
  2. *@Description 查询个人待执行的任务
  3. *@author weixinxin
  4. *@Date 17:33 2023/6/28
  5. **/
  6. @Test
  7. public void testFindPersonalTaskList(){
  8. // 1.获取流程引擎
  9. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  10. // 2.获取taskService
  11. TaskService taskService = processEngine.getTaskService();
  12. // 3.根据流程key 和 任务负责人 查询任务
  13. List<Task> list = taskService.createTaskQuery()
  14. .processDefinitionKey("evection")
  15. .taskAssignee("zhangsan")
  16. .list();
  17. for (Task task : list) {
  18. System.out.println("流程实例id = " + task.getProcessInstanceId());
  19. System.out.println("任务id = " + task.getId());
  20. System.out.println("任务负责人id = " + task.getAssignee());
  21. System.out.println("任务名称 = " + task.getName());
  22. }
  23. }

 14)、完成个人任务,依次完成zhangsan、lisi、wangwu、zhaoliu的任务

  1. /**
  2. *@Description 完成个人任务
  3. *@author weixinxin
  4. *@Date 17:45 2023/6/28
  5. **/
  6. @Test
  7. public void completTesk(){
  8. // 1.获取流程引擎
  9. ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
  10. // 2.获取taskService
  11. TaskService taskService = processEngine.getTaskService();
  12. /*Task task = taskService.createTaskQuery() //完成zhangsan任务
  13. .processDefinitionKey("evection")
  14. .taskAssignee("zhangsan")
  15. .singleResult();*/
  16. /*Task task = taskService.createTaskQuery() //完成lisi任务
  17. .processDefinitionKey("evection")
  18. .taskAssignee("lisi")
  19. .singleResult();*/
  20. /*Task task = taskService.createTaskQuery() //完成wangwu任务
  21. .processDefinitionKey("evection")
  22. .taskAssignee("wangwu")
  23. .singleResult();*/
  24. Task task = taskService.createTaskQuery() //完成zhaoliu任务
  25. .processDefinitionKey("evection")
  26. .taskAssignee("zhaoliu")
  27. .singleResult();
  28. System.out.println("流程实例id = " + task.getProcessInstanceId());
  29. System.out.println("任务id = " + task.getId());
  30. System.out.println("任务负责人id = " + task.getAssignee());
  31. System.out.println("任务名称 = " + task.getName());
  32. taskService.complete(task.getId());
  33. }

 以上是idea的插件简单的activiti工作引擎的使用过程

 (页面设计器部署到自己项目中)来画流程图。下篇文章http://t.csdnimg.cn/yuimK

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号