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

}
相关推荐
向前看-7 小时前
验证码机制
前端·后端
黄油饼卷咖喱鸡就味增汤拌孜然羊肉炒饭8 小时前
SpringBoot如何实现缓存预热?
java·spring boot·spring·缓存·程序员
超爱吃士力架9 小时前
邀请逻辑
java·linux·后端
AskHarries11 小时前
Spring Cloud OpenFeign快速入门demo
spring boot·后端
isolusion12 小时前
Springboot的创建方式
java·spring boot·后端
Yvemil712 小时前
《开启微服务之旅:Spring Boot Web开发举例》(一)
前端·spring boot·微服务
zjw_rp12 小时前
Spring-AOP
java·后端·spring·spring-aop
TodoCoder12 小时前
【编程思想】CopyOnWrite是如何解决高并发场景中的读写瓶颈?
java·后端·面试
凌虚13 小时前
Kubernetes APF(API 优先级和公平调度)简介
后端·程序员·kubernetes
星河梦瑾13 小时前
SpringBoot相关漏洞学习资料
java·经验分享·spring boot·安全