前言
Flowable 是一个使用 Java 编写的轻量级业务流程引擎。Flowable 流程引擎可用于部署 BPMN2.0 流程定义(用于定义流程的行业 XML 标准),创建这些流程定义的流程实例,进行查询,访问运行中或历史的流程实例与相关数据,等等。
代码实现
- 流程部署、查询流程定义
- 启动流程、查询流程
- 待办任务、完成任务
- 已结束流程、已完成任务
引入依赖
<!-- flowable -->
<dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>6.7.2</version>
</dependency>
配置文件
# 服务配置
# 服务配置
server:port: 8024# spring配置
spring:# 数据源配置datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://127.0.0.1:3306/boot_flowable?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF8&nullCatalogMeansCurrent=trueusername: rootpassword: root# flowable配置
flowable:# 更新表策略,可选值:true,false,create,create-drop,drop-createdatabase-schema-update: true# 使用历史表db-history-used: true# 禁用自动部署/processes下的BPMN XML流程文件check-process-definitions: false# 保存历史数据的最高级别history-level: full
流程定义
package com.qiangesoft.flowable.controller;import com.qiangesoft.flowable.constant.ProcessConstant;
import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.repository.Deployment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** 流程定义 控制器** @author qiangesoft* @date 2024-04-30*/
@RestController
@RequestMapping("/definition")
public class ProcessDefinitionController {@Autowiredprivate RepositoryService repositoryService;@GetMapping("/deploy")public R deploy() {// 部署流程Deployment deployment = repositoryService.createDeployment().addClasspathResource("bpmn/test.bpmn20.xml").deploy();return R.ok(deployment);}@GetMapping("/list")public R list() {// 查询部署流程List<Deployment> deploymentList = repositoryService.createDeploymentQuery().processDefinitionKey(ProcessConstant.PROCESS_KEY)
// .deploymentName("").orderByDeploymentTime().desc().list();return R.ok(deploymentList);}@GetMapping("/get")public R get() {Deployment deployment = repositoryService.createDeploymentQuery().processDefinitionKey(ProcessConstant.PROCESS_KEY)
// .deploymentName("").latest().singleResult();return R.ok(deployment);}}
流程实例
package com.qiangesoft.flowable.controller;import com.qiangesoft.flowable.constant.ProcessConstant;
import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.runtime.ProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** 流程实例 控制器** @author qiangesoft* @date 2024-04-30*/
@RestController
@RequestMapping("/instance")
public class ProcessInstanceController {@Autowiredprivate RuntimeService runtimeService;@GetMapping("/start")public R start() {// 启动流程:提供流程key,业务key,主题String businessId = "101";ProcessInstance processInstance = runtimeService.createProcessInstanceBuilder().processDefinitionKey(ProcessConstant.PROCESS_KEY).businessKey(ProcessConstant.BUSINESS_KEY_PREFIX + businessId).name("请假流程").start();return R.ok(processInstance.toString());}@GetMapping("/list")public R list() {// 查询进行中的流程实例List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery().processDefinitionKey(ProcessConstant.PROCESS_KEY)
// .deploymentId("")
// .processInstanceId("")
// .processInstanceBusinessKey("")
// .processInstanceNameLike("请假流程").active().orderByProcessInstanceId().desc().list();return R.ok(processInstanceList.toString());}@GetMapping("/get")public R get(String instanceId) {// 某个实例ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(instanceId).singleResult();return R.ok(processInstance == null ? null : processInstance.toString());}}
流程任务
package com.qiangesoft.flowable.controller;import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** 流程待办 控制器** @author qiangesoft* @date 2024-04-30*/
@RestController
@RequestMapping("/task")
public class TaskController {@Autowiredprivate TaskService taskService;@GetMapping("/mine")public R mine() {// 某人的待办任务:按照时间倒序String userId = "zhangsan";List<Task> taskList = taskService.createTaskQuery()
// .processDefinitionKey("")
// .taskCandidateOrAssigned("").taskAssignee(userId).active().orderByTaskCreateTime().desc().list();return R.ok(taskList.toString());}@GetMapping("/handle")public R handle(String taskId) {// 完成任务taskService.complete(taskId);return R.ok();}}
流程历史
package com.qiangesoft.flowable.controller;import com.qiangesoft.flowable.constant.ProcessConstant;
import com.qiangesoft.flowable.constant.R;
import org.flowable.engine.HistoryService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import java.util.List;/*** 历史 控制器** @author qiangesoft* @date 2024-04-30*/
@RestController
@RequestMapping("/history")
public class HistoryController {@Autowiredprivate HistoryService historyService;@GetMapping("/finished")public R finished() {// 已结束的流程:按照结束时间倒序List<HistoricProcessInstance> processInstanceList = historyService.createHistoricProcessInstanceQuery()
// .processInstanceId("")
// .deploymentId("")
// .processDefinitionName("")
// .processInstanceBusinessKey("").processDefinitionKey(ProcessConstant.PROCESS_KEY).finished().orderByProcessInstanceEndTime().desc().list();return R.ok(processInstanceList.toString());}@GetMapping("/completed")public R completed() {// 某人已完成的任务:按照完成时间倒序String userId = "zhangsan";List<HistoricTaskInstance> processInstanceList = historyService.createHistoricTaskInstanceQuery()
// .processInstanceId("")
// .deploymentId("")
// .processDefinitionName("")
// .processInstanceBusinessKey("").taskAssignee(userId).processDefinitionKey(ProcessConstant.PROCESS_KEY).finished().orderByHistoricTaskInstanceEndTime().desc().list();return R.ok(processInstanceList.toString());}}