一、项目结构
二、maven配置
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.9.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.mk</groupId><artifactId>flowable</artifactId><version>1.0-SNAPSHOT</version><name>flowable</name><description>flowable</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.20</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.1.0</version></dependency><dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>6.5.0</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>com.h2database</groupId><artifactId>h2</artifactId><version>1.3.168</version><scope>test</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
application.yml
spring:application:name: flowableprofiles:active: devjackson:time-zone: GMT+8date-format: yyyy-MM-dd HH:mm:ssdefault-property-inclusion: ALWAYSdatasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: "jdbc:mysql://localhost:3306/flowable?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&useSSL=false"username: "root"password: "admin"druid:initial-size: 10max-active: 100min-idle: 10max-wait: 60000pool-prepared-statements: truemax-pool-prepared-statement-per-connection-size: 20time-between-eviction-runs-millis: 60000min-evictable-idle-time-millis: 300000validation-query: SELECT 1 FROM DUALtest-while-idle: truetest-on-borrow: falsetest-on-return: falsefilter:stat:log-slow-sql: trueslow-sql-millis: 1000merge-sql: trueenabled: truewall:config:multi-statement-allow: truestat-view-servlet:enabled: trueservlet:multipart:max-file-size: 10MBmax-request-size: 10MBmvc:date-format: yyyy-MM-dd HH:mm:ssserver:port: 8008mybatis:mapper-locations: classpath*:mapper/*.xmltype-aliases-package: com.mk.flowable.entityconfiguration:map-underscore-to-camel-case: trueconfiguration-properties:prefix:blobType: BLOBboolValue: TRUE
三、常用API
package com.mk.flowable.service;import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.bpmn.model.EndEvent;
import org.flowable.bpmn.model.FlowElement;
import org.flowable.common.engine.impl.identity.Authentication;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.identitylink.api.IdentityLinkInfo;
import org.flowable.task.api.Task;
import org.springframework.stereotype.Service;import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;@Service
public class FlowableService {@Resourceprivate TaskService taskService;@Resourceprivate RepositoryService repositoryService;@Resourceprivate RuntimeService runtimeService;@Resourceprivate HistoryService historyService;public ProcessInstance start(String processDefinitionKey, String userId) {Map<String, Object> variables = new HashMap<>();variables.put("userName", userId);variables.put("_ACTIVITI_SKIP_EXPRESSION_ENABLED", true);//允许执行跳过表达式Authentication.setAuthenticatedUserId(userId);ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, variables);Authentication.setAuthenticatedUserId(null);return processInstance;}public ProcessInstance get(String processInstanceId){ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();return processInstance;}public List<Task> listTask(String processInstanceId) {List<org.flowable.task.api.Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();return tasks;}public Task getTask(String taskId) {Task task = taskService.createTaskQuery().taskId(taskId).singleResult();return task;}public void complete(String taskId, Map<String, Object> variables) {taskService.complete(taskId, variables);}public void move(Task task, String activityName) {runtimeService.createChangeActivityStateBuilder().processInstanceId(task.getProcessInstanceId()).moveActivityIdsToSingleActivityId(Arrays.asList(task.getTaskDefinitionKey()),activityName).changeState();}public List<EndEvent> getEnd(String processDefinitionId) {BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);List<EndEvent> endEvents = bpmnModel.getProcesses().stream().flatMap(v->v.findFlowElementsOfType(EndEvent.class).stream()).collect(Collectors.toList());return endEvents;}public FlowElement getFlowElement(Task task) {BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());FlowElement flowElement = bpmnModel.getFlowElement(task.getTaskDefinitionId());return flowElement;}public Map<String, Object> getVariables(String taskId) {Map<String, Object> variables = taskService.getVariables(taskId);return variables;}public List<String> getCandidateUsers(String taskId) {Task task = taskService.createTaskQuery().taskId(taskId).includeIdentityLinks().singleResult();List<? extends IdentityLinkInfo> linkInfos = task.getIdentityLinks();List<String> candidateUsers = linkInfos.stream().filter(v -> "candidate".equals(v.getType())).map(IdentityLinkInfo::getUserId).filter(StringUtils::isNotBlank).collect(Collectors.toList());return candidateUsers;}public List<String> getCandidatGroups(String taskId) {Task task = taskService.createTaskQuery().taskId(taskId).includeIdentityLinks().singleResult();List<? extends IdentityLinkInfo> linkInfos = task.getIdentityLinks();List<String> candidateUsers = linkInfos.stream().filter(v -> "candidate".equals(v.getType())).map(IdentityLinkInfo::getGroupId).filter(StringUtils::isNotBlank).collect(Collectors.toList());return candidateUsers;}}
四、事件和委托
结束事件监听
package com.mk.flowable.listener;import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;@Slf4j
public class EndListener implements ExecutionListener {@Overridepublic void notify(DelegateExecution execution) {}
}
任务监听
package com.mk.flowable.listener;import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;public class FirstTaskListener implements TaskListener {@Overridepublic void notify(DelegateTask delegateTask) {}}
自动节点委托
package com.mk.flowable.service;import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;public class AutoExeService implements JavaDelegate {public void execute(DelegateExecution execution) {}
}
testFlowable.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="testFlowable" name="testFlowable" isExecutable="true"><startEvent id="startevent1" name="Start"></startEvent><endEvent id="endevent1" name="End"><extensionElements><activiti:executionListener event="end" class="com.mk.flowable.listener.EndListener"></activiti:executionListener></extensionElements></endEvent><serviceTask id="servicetask1" name="Service Task1" activiti:class="com.mk.flowable.service.AutoExeService"><documentation>{"data":"es"}</documentation></serviceTask><serviceTask id="servicetask2" name="Service Task2" activiti:class="com.mk.flowable.service.AutoExeService"></serviceTask><serviceTask id="servicetask3" name="Service Task3" activiti:class="com.mk.flowable.service.AutoExeService"></serviceTask><sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask0"></sequenceFlow><sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow><sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="servicetask3"></sequenceFlow><sequenceFlow id="flow4" sourceRef="servicetask3" targetRef="endevent1"></sequenceFlow><userTask id="usertask0" name="User Task0" activiti:assignee="${userName}"></userTask><sequenceFlow id="flow5" sourceRef="usertask0" targetRef="servicetask1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_testFlowable"><bpmndi:BPMNPlane bpmnElement="testFlowable" id="BPMNPlane_testFlowable"><bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"><omgdc:Bounds height="35.0" width="35.0" x="30.0" y="180.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"><omgdc:Bounds height="35.0" width="35.0" x="730.0" y="180.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1"><omgdc:Bounds height="55.0" width="105.0" x="280.0" y="170.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2"><omgdc:Bounds height="55.0" width="105.0" x="430.0" y="170.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3"><omgdc:Bounds height="55.0" width="105.0" x="580.0" y="170.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="usertask0" id="BPMNShape_usertask0"><omgdc:Bounds height="55.0" width="105.0" x="120.0" y="170.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="65.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="120.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="385.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="430.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"><omgdi:waypoint x="535.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="580.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"><omgdi:waypoint x="685.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="730.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5"><omgdi:waypoint x="225.0" y="197.0"></omgdi:waypoint><omgdi:waypoint x="280.0" y="197.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>