当前位置:   article > 正文

SpringBoot集成Flowable

SpringBoot集成Flowable

一、项目结构

 

二、maven配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.9.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.mk</groupId>
  12. <artifactId>flowable</artifactId>
  13. <version>1.0-SNAPSHOT</version>
  14. <name>flowable</name>
  15. <description>flowable</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>com.alibaba</groupId>
  22. <artifactId>druid-spring-boot-starter</artifactId>
  23. <version>1.1.20</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. <scope>runtime</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.mybatis.spring.boot</groupId>
  32. <artifactId>mybatis-spring-boot-starter</artifactId>
  33. <version>2.1.0</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.flowable</groupId>
  37. <artifactId>flowable-spring-boot-starter</artifactId>
  38. <version>6.5.0</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-devtools</artifactId>
  43. <scope>runtime</scope>
  44. <optional>true</optional>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-test</artifactId>
  49. <scope>test</scope>
  50. </dependency>
  51. <dependency>
  52. <groupId>com.h2database</groupId>
  53. <artifactId>h2</artifactId>
  54. <version>1.3.168</version>
  55. <scope>test</scope>
  56. </dependency>
  57. <dependency>
  58. <groupId>org.projectlombok</groupId>
  59. <artifactId>lombok</artifactId>
  60. </dependency>
  61. </dependencies>
  62. <build>
  63. <plugins>
  64. <plugin>
  65. <groupId>org.springframework.boot</groupId>
  66. <artifactId>spring-boot-maven-plugin</artifactId>
  67. </plugin>
  68. </plugins>
  69. </build>
  70. </project>

application.yml 

  1. spring:
  2. application:
  3. name: flowable
  4. profiles:
  5. active: dev
  6. jackson:
  7. time-zone: GMT+8
  8. date-format: yyyy-MM-dd HH:mm:ss
  9. default-property-inclusion: ALWAYS
  10. datasource:
  11. type: com.alibaba.druid.pool.DruidDataSource
  12. driver-class-name: com.mysql.jdbc.Driver
  13. url: "jdbc:mysql://localhost:3306/flowable?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"
  14. username: "root"
  15. password: "admin"
  16. druid:
  17. initial-size: 10
  18. max-active: 100
  19. min-idle: 10
  20. max-wait: 60000
  21. pool-prepared-statements: true
  22. max-pool-prepared-statement-per-connection-size: 20
  23. time-between-eviction-runs-millis: 60000
  24. min-evictable-idle-time-millis: 300000
  25. validation-query: SELECT 1 FROM DUAL
  26. test-while-idle: true
  27. test-on-borrow: false
  28. test-on-return: false
  29. filter:
  30. stat:
  31. log-slow-sql: true
  32. slow-sql-millis: 1000
  33. merge-sql: true
  34. enabled: true
  35. wall:
  36. config:
  37. multi-statement-allow: true
  38. stat-view-servlet:
  39. enabled: true
  40. servlet:
  41. multipart:
  42. max-file-size: 10MB
  43. max-request-size: 10MB
  44. mvc:
  45. date-format: yyyy-MM-dd HH:mm:ss
  46. server:
  47. port: 8008
  48. mybatis:
  49. mapper-locations: classpath*:mapper/*.xml
  50. type-aliases-package: com.mk.flowable.entity
  51. configuration:
  52. map-underscore-to-camel-case: true
  53. configuration-properties:
  54. prefix:
  55. blobType: BLOB
  56. boolValue: TRUE

 

 

三、常用API

  1. package com.mk.flowable.service;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.flowable.bpmn.model.BpmnModel;
  4. import org.flowable.bpmn.model.EndEvent;
  5. import org.flowable.bpmn.model.FlowElement;
  6. import org.flowable.common.engine.impl.identity.Authentication;
  7. import org.flowable.engine.HistoryService;
  8. import org.flowable.engine.RepositoryService;
  9. import org.flowable.engine.RuntimeService;
  10. import org.flowable.engine.TaskService;
  11. import org.flowable.engine.runtime.ProcessInstance;
  12. import org.flowable.identitylink.api.IdentityLinkInfo;
  13. import org.flowable.task.api.Task;
  14. import org.springframework.stereotype.Service;
  15. import javax.annotation.Resource;
  16. import java.util.*;
  17. import java.util.stream.Collectors;
  18. @Service
  19. public class FlowableService {
  20. @Resource
  21. private TaskService taskService;
  22. @Resource
  23. private RepositoryService repositoryService;
  24. @Resource
  25. private RuntimeService runtimeService;
  26. @Resource
  27. private HistoryService historyService;
  28. public ProcessInstance start(String processDefinitionKey, String userId) {
  29. Map<String, Object> variables = new HashMap<>();
  30. variables.put("userName", userId);
  31. variables.put("_ACTIVITI_SKIP_EXPRESSION_ENABLED", true);//允许执行跳过表达式
  32. Authentication.setAuthenticatedUserId(userId);
  33. ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);
  34. Authentication.setAuthenticatedUserId(null);
  35. return processInstance;
  36. }
  37. public ProcessInstance get(String processInstanceId){
  38. ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
  39. return processInstance;
  40. }
  41. public List<Task> listTask(String processInstanceId) {
  42. List<org.flowable.task.api.Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
  43. return tasks;
  44. }
  45. public Task getTask(String taskId) {
  46. Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
  47. return task;
  48. }
  49. public void complete(String taskId, Map<String, Object> variables) {
  50. taskService.complete(taskId, variables);
  51. }
  52. public void move(Task task, String activityName) {
  53. runtimeService.createChangeActivityStateBuilder()
  54. .processInstanceId(task.getProcessInstanceId())
  55. .moveActivityIdsToSingleActivityId(
  56. Arrays.asList(task.getTaskDefinitionKey()),
  57. activityName)
  58. .changeState();
  59. }
  60. public List<EndEvent> getEnd(String processDefinitionId) {
  61. BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
  62. List<EndEvent> endEvents = bpmnModel.getProcesses().stream().flatMap(v->v.findFlowElementsOfType(EndEvent.class).stream()).collect(Collectors.toList());
  63. return endEvents;
  64. }
  65. public FlowElement getFlowElement(Task task) {
  66. BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());
  67. FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionId());
  68. return flowElement;
  69. }
  70. public Map<String, Object> getVariables(String taskId) {
  71. Map<String, Object> variables = taskService.getVariables(taskId);
  72. return variables;
  73. }
  74. public List<String> getCandidateUsers(String taskId) {
  75. Task task = taskService.createTaskQuery().taskId(taskId).includeIdentityLinks().singleResult();
  76. List<? extends IdentityLinkInfo> linkInfos = task.getIdentityLinks();
  77. List<String> candidateUsers = linkInfos.stream().filter(v -> "candidate".equals(v.getType())).map(IdentityLinkInfo::getUserId)
  78. .filter(StringUtils::isNotBlank).collect(Collectors.toList());
  79. return candidateUsers;
  80. }
  81. public List<String> getCandidatGroups(String taskId) {
  82. Task task = taskService.createTaskQuery().taskId(taskId).includeIdentityLinks().singleResult();
  83. List<? extends IdentityLinkInfo> linkInfos = task.getIdentityLinks();
  84. List<String> candidateUsers = linkInfos.stream().filter(v -> "candidate".equals(v.getType())).map(IdentityLinkInfo::getGroupId)
  85. .filter(StringUtils::isNotBlank).collect(Collectors.toList());
  86. return candidateUsers;
  87. }
  88. }

 

四、事件和委托

结束事件监听

  1. package com.mk.flowable.listener;
  2. import lombok.extern.slf4j.Slf4j;
  3. import org.flowable.engine.delegate.DelegateExecution;
  4. import org.flowable.engine.delegate.ExecutionListener;
  5. @Slf4j
  6. public class EndListener implements ExecutionListener {
  7. @Override
  8. public void notify(DelegateExecution execution) {
  9. }
  10. }

任务监听

  1. package com.mk.flowable.listener;
  2. import org.flowable.engine.delegate.TaskListener;
  3. import org.flowable.task.service.delegate.DelegateTask;
  4. public class FirstTaskListener implements TaskListener {
  5. @Override
  6. public void notify(DelegateTask delegateTask) {
  7. }
  8. }

自动节点委托

  1. package com.mk.flowable.service;
  2. import org.flowable.engine.delegate.DelegateExecution;
  3. import org.flowable.engine.delegate.JavaDelegate;
  4. public class AutoExeService implements JavaDelegate {
  5. public void execute(DelegateExecution execution) {
  6. }
  7. }

 

testFlowable.bpmn

  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/test">
  3. <process id="testFlowable" name="testFlowable" isExecutable="true">
  4. <startEvent id="startevent1" name="Start"></startEvent>
  5. <endEvent id="endevent1" name="End">
  6. <extensionElements>
  7. <activiti:executionListener event="end" class="com.mk.flowable.listener.EndListener"></activiti:executionListener>
  8. </extensionElements>
  9. </endEvent>
  10. <serviceTask id="servicetask1" name="Service Task1" activiti:class="com.mk.flowable.service.AutoExeService">
  11. <documentation>{"data":"es"}</documentation>
  12. </serviceTask>
  13. <serviceTask id="servicetask2" name="Service Task2" activiti:class="com.mk.flowable.service.AutoExeService"></serviceTask>
  14. <serviceTask id="servicetask3" name="Service Task3" activiti:class="com.mk.flowable.service.AutoExeService"></serviceTask>
  15. <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask0"></sequenceFlow>
  16. <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
  17. <sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="servicetask3"></sequenceFlow>
  18. <sequenceFlow id="flow4" sourceRef="servicetask3" targetRef="endevent1"></sequenceFlow>
  19. <userTask id="usertask0" name="User Task0" activiti:assignee="${userName}"></userTask>
  20. <sequenceFlow id="flow5" sourceRef="usertask0" targetRef="servicetask1"></sequenceFlow>
  21. </process>
  22. <bpmndi:BPMNDiagram id="BPMNDiagram_testFlowable">
  23. <bpmndi:BPMNPlane bpmnElement="testFlowable" id="BPMNPlane_testFlowable">
  24. <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
  25. <omgdc:Bounds height="35.0" width="35.0" x="30.0" y="180.0"></omgdc:Bounds>
  26. </bpmndi:BPMNShape>
  27. <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
  28. <omgdc:Bounds height="35.0" width="35.0" x="730.0" y="180.0"></omgdc:Bounds>
  29. </bpmndi:BPMNShape>
  30. <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
  31. <omgdc:Bounds height="55.0" width="105.0" x="280.0" y="170.0"></omgdc:Bounds>
  32. </bpmndi:BPMNShape>
  33. <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
  34. <omgdc:Bounds height="55.0" width="105.0" x="430.0" y="170.0"></omgdc:Bounds>
  35. </bpmndi:BPMNShape>
  36. <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
  37. <omgdc:Bounds height="55.0" width="105.0" x="580.0" y="170.0"></omgdc:Bounds>
  38. </bpmndi:BPMNShape>
  39. <bpmndi:BPMNShape bpmnElement="usertask0" id="BPMNShape_usertask0">
  40. <omgdc:Bounds height="55.0" width="105.0" x="120.0" y="170.0"></omgdc:Bounds>
  41. </bpmndi:BPMNShape>
  42. <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
  43. <omgdi:waypoint x="65.0" y="197.0"></omgdi:waypoint>
  44. <omgdi:waypoint x="120.0" y="197.0"></omgdi:waypoint>
  45. </bpmndi:BPMNEdge>
  46. <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
  47. <omgdi:waypoint x="385.0" y="197.0"></omgdi:waypoint>
  48. <omgdi:waypoint x="430.0" y="197.0"></omgdi:waypoint>
  49. </bpmndi:BPMNEdge>
  50. <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
  51. <omgdi:waypoint x="535.0" y="197.0"></omgdi:waypoint>
  52. <omgdi:waypoint x="580.0" y="197.0"></omgdi:waypoint>
  53. </bpmndi:BPMNEdge>
  54. <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
  55. <omgdi:waypoint x="685.0" y="197.0"></omgdi:waypoint>
  56. <omgdi:waypoint x="730.0" y="197.0"></omgdi:waypoint>
  57. </bpmndi:BPMNEdge>
  58. <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
  59. <omgdi:waypoint x="225.0" y="197.0"></omgdi:waypoint>
  60. <omgdi:waypoint x="280.0" y="197.0"></omgdi:waypoint>
  61. </bpmndi:BPMNEdge>
  62. </bpmndi:BPMNPlane>
  63. </bpmndi:BPMNDiagram>
  64. </definitions>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/982814
推荐阅读
相关标签