SpringBoot集成Flowable案例

前言

Flowable 是一个使用 Java 编写的轻量级业务流程引擎。Flowable 流程引擎可用于部署 BPMN2.0 流程定义(用于定义流程的行业 XML 标准),创建这些流程定义的流程实例,进行查询,访问运行中或历史的流程实例与相关数据,等等。

代码实现

  • 流程部署、查询流程定义
  • 启动流程、查询流程
  • 待办任务、完成任务
  • 已结束流程、已完成任务

引入依赖

xml 复制代码
<!-- flowable -->
<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>6.7.2</version>
</dependency>

配置文件

yaml 复制代码
# 服务配置
# 服务配置
server:
  port: 8024

# spring配置
spring:
  # 数据源配置
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/boot_flowable?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF8&nullCatalogMeansCurrent=true
    username: root
    password: root

# flowable配置
flowable:
  # 更新表策略,可选值:true,false,create,create-drop,drop-create
  database-schema-update: true
  # 使用历史表
  db-history-used: true
  # 禁用自动部署/processes下的BPMN XML流程文件
  check-process-definitions: false
  # 保存历史数据的最高级别
  history-level: full

流程定义

java 复制代码
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 {

    @Autowired
    private 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);
    }

}

流程实例

java 复制代码
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 {

    @Autowired
    private 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());
    }

}

流程任务

java 复制代码
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 {

    @Autowired
    private 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();
    }

}

流程历史

java 复制代码
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 {

    @Autowired
    private 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());
    }

}
相关推荐
计算机学长felix8 分钟前
基于SpringBoot的“校园交友网站”的设计与实现(源码+数据库+文档+PPT)
数据库·spring boot·毕业设计·交友
码农派大星。20 分钟前
Spring Boot 配置文件
java·spring boot·后端
江深竹静,一苇以航30 分钟前
springboot3项目整合Mybatis-plus启动项目报错:Invalid bean definition with name ‘xxxMapper‘
java·spring boot
杜杜的man1 小时前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*1 小时前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
llllinuuu1 小时前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s1 小时前
Golang--协程和管道
开发语言·后端·golang
为什么这亚子1 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算
豪宇刘1 小时前
SpringBoot+Shiro权限管理
java·spring boot·spring
想进大厂的小王1 小时前
项目架构介绍以及Spring cloud、redis、mq 等组件的基本认识
redis·分布式·后端·spring cloud·微服务·架构