七、springboot整合flowable(工作流)

七、springboot整合flowable(工作流)springboot整合flowable(工作流)简介Flowable适用于开发人员,系统管理员和业务用户的紧凑且高效的工作流程和业务流程管理(BPM)平台。Flowable的发布包里包含了大部分源码,以JAR文件方式提供。Flowable的源码也可以通过以下链接获得:https://github.com/flowable/flowable-engine准备工作pom….

大家好,又见面了,我是你们的朋友全栈君。

springboot整合flowable(工作流)

简介

  • Flowable 适用于开发人员,系统管理员和业务用户的紧凑且高效的工作流程和业务流程管理(BPM)平台。

  • Flowable的发布包里包含了大部分源码,以JAR文件方式提供。Flowable的源码也可以通过以下链接获得: https://github.com/flowable/flowable-engine

准备工作

  • pom.xml jar引入: <flowable.version>6.3.0</flowable.version>
<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>${flowable.version}</version>
</dependency>

项目要点

  • 1.application.yml配置 添加以下属性
flowable:
  #关闭定时任务JOB
  async-executor-activate: false
#  将databaseSchemaUpdate设置为true。当Flowable发现库与数据库表结构不一致时,会自动将数据库表结构升级至新版本。
  database-schema-update: true
  • 2.创建OrderApproval.bpmn20.xml文件。( 之前使用activiti的时候eclipse集成的Activiti Diagram工具挺好用的,idea对bpmn画图工具集成不是很友好,activiti中也有自己的BPMN可视化工具,也还算友好)
    将其放于项目中的resource目录下的processe,当此框架启动的时候它会默认加载resource目录下的processes时就可以将此流程配置加载到数据库进行持久化。
<?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:flowable="http://flowable.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.flowable.org/processdef">
  <process id="OrderApproval" name="OrderApproval" isExecutable="true">
    <startEvent id="startEvent" name="成功提交采购订单审批"></startEvent>
    <sequenceFlow id="sequenceFlow-31f46bc9-22c4-460f-b2a5-1426bb106d31" sourceRef="startEvent" targetRef="approveTask"></sequenceFlow>
    <userTask id="approveTask" name="Order approval" flowable:assignee="${userId}"></userTask>
    <exclusiveGateway id="decision"></exclusiveGateway>
    <sequenceFlow id="sequenceFlow-90e6eadb-3a9e-4955-bad4-ec337a360fac" sourceRef="decision" targetRef="false">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!approved}]]></conditionExpression>
    </sequenceFlow>
    <serviceTask id="success" name="success" flowable:class="com.honghh.bootfirst.delegate.ReviewApprove"></serviceTask>
    <endEvent id="approveEnd"></endEvent>
    <endEvent id="rejectEnd"></endEvent>
    <sequenceFlow id="sequenceFlow-e94e21dd-7d79-4f42-9f59-6c21baa8ce29" sourceRef="success" targetRef="approveEnd"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-195d996c-ccbe-4834-881f-bafaf1393643" sourceRef="false" targetRef="rejectEnd"></sequenceFlow>
    <serviceTask id="false" name="false" flowable:class="com.honghh.bootfirst.delegate.ReviewNoApprove"></serviceTask>
    <sequenceFlow id="sequenceFlow-5587fdbe-fb00-420d-9711-1b1a81c086db" sourceRef="approveTask" targetRef="decision"></sequenceFlow>
    <sequenceFlow id="sequenceFlow-c1d4f277-e5a7-492a-9768-a9161f18c116" sourceRef="decision" targetRef="success">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${approved}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_OrderApproval">
    <bpmndi:BPMNPlane bpmnElement="OrderApproval" id="BPMNPlane_OrderApproval">
      <bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
        <omgdc:Bounds height="30.0" width="30.0" x="0.0" y="95.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="approveTask" id="BPMNShape_approveTask">
        <omgdc:Bounds height="60.0" width="100.0" x="75.0" y="75.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="decision" id="BPMNShape_decision">
        <omgdc:Bounds height="40.0" width="40.0" x="230.0" y="90.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="success" id="BPMNShape_success">
        <omgdc:Bounds height="60.0" width="100.0" x="320.0" y="0.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="approveEnd" id="BPMNShape_approveEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="620.0" y="16.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="rejectEnd" id="BPMNShape_rejectEnd">
        <omgdc:Bounds height="28.0" width="28.0" x="570.0" y="175.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="false" id="BPMNShape_false">
        <omgdc:Bounds height="60.0" width="100.0" x="315.0" y="150.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-5587fdbe-fb00-420d-9711-1b1a81c086db" id="BPMNEdge_sequenceFlow-5587fdbe-fb00-420d-9711-1b1a81c086db">
        <omgdi:waypoint x="174.95" y="105.0"></omgdi:waypoint>
        <omgdi:waypoint x="202.5" y="105.0"></omgdi:waypoint>
        <omgdi:waypoint x="202.5" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="230.0" y="110.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-c1d4f277-e5a7-492a-9768-a9161f18c116" id="BPMNEdge_sequenceFlow-c1d4f277-e5a7-492a-9768-a9161f18c116">
        <omgdi:waypoint x="269.9189252336448" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="30.000000000000004"></omgdi:waypoint>
        <omgdi:waypoint x="319.999999999994" y="30.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-195d996c-ccbe-4834-881f-bafaf1393643" id="BPMNEdge_sequenceFlow-195d996c-ccbe-4834-881f-bafaf1393643">
        <omgdi:waypoint x="414.95000000000005" y="180.0"></omgdi:waypoint>
        <omgdi:waypoint x="460.0" y="180.0"></omgdi:waypoint>
        <omgdi:waypoint x="460.0" y="189.0"></omgdi:waypoint>
        <omgdi:waypoint x="570.0" y="189.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-e94e21dd-7d79-4f42-9f59-6c21baa8ce29" id="BPMNEdge_sequenceFlow-e94e21dd-7d79-4f42-9f59-6c21baa8ce29">
        <omgdi:waypoint x="419.94999999998697" y="30.0"></omgdi:waypoint>
        <omgdi:waypoint x="620.0" y="30.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-31f46bc9-22c4-460f-b2a5-1426bb106d31" id="BPMNEdge_sequenceFlow-31f46bc9-22c4-460f-b2a5-1426bb106d31">
        <omgdi:waypoint x="29.949987029268733" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="52.5" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="52.5" y="105.0"></omgdi:waypoint>
        <omgdi:waypoint x="74.99999999999241" y="105.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sequenceFlow-90e6eadb-3a9e-4955-bad4-ec337a360fac" id="BPMNEdge_sequenceFlow-90e6eadb-3a9e-4955-bad4-ec337a360fac">
        <omgdi:waypoint x="269.9189252336448" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="110.0"></omgdi:waypoint>
        <omgdi:waypoint x="282.0" y="180.0"></omgdi:waypoint>
        <omgdi:waypoint x="314.9999999999916" y="180.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

3.创建FlowableController类

package com.honghh.bootfirst.controller;

import com.honghh.bootfirst.utils.R;
import org.flowable.bpmn.model.BpmnModel;
import org.flowable.engine.*;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.image.ProcessDiagramGenerator;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * ClassName: FlowableController
 * Description:
 *
 * @author honghh
 * @date 2019/02/20 18:12
 */
@RestController
@RequestMapping("/flowable")
public class FlowableController {
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    @Autowired
    private HistoryService historyService;

    @Autowired
    private ProcessEngine processEngine;

    /**
     * .提交采购订单的审批请求
     *
     * @param userId 用户id
     */
    @PostMapping("/start/{userId}/{purchaseOrderId}")
    public R startFlow(@PathVariable String userId, @PathVariable String purchaseOrderId) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("purchaseOrderId", purchaseOrderId);
        ProcessInstance processInstance =
                runtimeService.startProcessInstanceByKey("OrderApproval", map);
        String processId = processInstance.getId();
        String name = processInstance.getName();
        System.out.println(processId + ":" + name);
        return R.ok(processId + ":" + name);
    }

    /**
     * .获取用户的任务
     *
     * @param userId 用户id
     */
    @GetMapping("/getTasks/{userId}")
    public R getTasks(@PathVariable String userId) {
        List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
        return R.ok(tasks.toString());
    }

    /**
     * .审批通过
     */
    @PostMapping("/success/{taskId}")
    public R success(@PathVariable String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            return R.error("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("approved", true);
        taskService.complete(taskId, map);
        return R.ok("流程审核通过!");
    }

    /**
     * .审批不通过
     */
    @PostMapping("/faile/{taskId}")
    public R faile(@PathVariable String taskId) {
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
        if (task == null) {
            return R.error("流程不存在");
        }
        //通过审核
        HashMap<String, Object> map = new HashMap<>();
        map.put("approved", false);
        taskService.complete(taskId, map);
        return R.ok();
    }
    @RequestMapping(value = "processDiagram")
    public void genProcessDiagram(HttpServletResponse httpServletResponse, String processId) throws Exception {
        ProcessInstance pi = runtimeService.createProcessInstanceQuery().processInstanceId(processId).singleResult();

        //流程走完的不显示图
        if (pi == null) {
            return;
        }
        Task task = taskService.createTaskQuery().processInstanceId(pi.getId()).singleResult();
        //使用流程实例ID,查询正在执行的执行对象表,返回流程实例对象
        String InstanceId = task.getProcessInstanceId();
        List<Execution> executions = runtimeService
                .createExecutionQuery()
                .processInstanceId(InstanceId)
                .list();

        //得到正在执行的Activity的Id
        List<String> activityIds = new ArrayList<>();
        List<String> flows = new ArrayList<>();
        for (Execution exe : executions) {
            List<String> ids = runtimeService.getActiveActivityIds(exe.getId());
            activityIds.addAll(ids);
        }

        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(pi.getProcessDefinitionId());
        ProcessEngineConfiguration engconf = processEngine.getProcessEngineConfiguration();
        ProcessDiagramGenerator diagramGenerator = engconf.getProcessDiagramGenerator();
        InputStream in = diagramGenerator.generateDiagram(bpmnModel, "png", activityIds, flows, engconf.getActivityFontName(), engconf.getLabelFontName(), engconf.getAnnotationFontName(), engconf.getClassLoader(), 1.0);
        OutputStream out = null;
        byte[] buf = new byte[1024];
        int legth = 0;
        try {
            out = httpServletResponse.getOutputStream();
            while ((legth = in.read(buf)) != -1) {
                out.write(buf, 0, legth);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}
  • 4.配置FlowableConfig 为防止生成的流程图中中文乱码
package com.honghh.bootfirst.config;

import org.flowable.spring.SpringProcessEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.context.annotation.Configuration;

/**
 * ClassName: FlowableConfig
 * Description:
 *
 * @author honghh
 * @date 2019/02/20 18:43
 */
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {


    @Override
    public void configure(SpringProcessEngineConfiguration engineConfiguration) {
        engineConfiguration.setActivityFontName("宋体");
        engineConfiguration.setLabelFontName("宋体");
        engineConfiguration.setAnnotationFontName("宋体");
    }

}

演示效果

  • 1.提交采购订单的审批请求
    http://localhost:8080/flowable/start/1/1

image

  • 2.获取用户的任务
    http://localhost:8080/flowable/getTasks/1
    image

  • 3.审批通过 http://localhost:8080/flowable/success/11
    image

  • 4.生成当前流程图表 http://localhost:8080/flowable/processDiagram?processId=5
    image

代码获取

https://gitee.com/honghh/boot-demo.git

参考文献

  • Flowable BPMN 用户手册 (v 6.3.0)
    https://tkjohn.github.io/flowable-userguide/

  • 采用springboot+flowable快速实现工作流
    https://blog.csdn.net/puhaiyang/article/details/79845248

  • Activiti学习文档(三)之画流程图并部署流程
    https://blog.csdn.net/samile6899/article/details/52578347

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/143284.html原文链接:https://javaforall.net

(0)
上一篇 2022年5月21日 下午11:40
下一篇 2022年5月21日 下午11:40


相关推荐

  • objdump命令详解

    objdump命令详解objdump 命令是用查看目标文件或者可执行的目标文件的构成的 gcc 工具 选项 archive headers a 显示档案库的成员信息 类似 ls l 将 lib a 的信息列出 bbfdnametarg bfdname 指定目标码格式 这不是必须的 objdump 能自动识别许多格式 比如 objdump boasys mvax hfu o 显

    2025年11月27日
    7
  • matlab解析int8数据为double_matlab把double转成int

    matlab解析int8数据为double_matlab把double转成int最近写matlab又遇到一个坑,感觉是匪夷所思的bug,简直刷新我的人生观、世界观和价值观【手动笑哭】想解决的问题很简单,我就是想求一张图片中所有像素点的R、G、B三个颜色分量的平均值,然后我发现,每个颜色分量的和永远是255,这怎么可能啊,和肯定会很大啊,各种调试,调到我质疑人生。后来在Workspace中看了几眼,看到图片存储是以unit8数值类型存储的,成功引起了我的注意,以前真是没…

    2025年12月12日
    4
  • 查看linux 定时任务的执行记录

    查看linux 定时任务的执行记录1,查看有哪些任务[root@daping-6~]#crontab-lSHELL=/bin/bashPATH=/sbin:/bin:/usr/sbin:/usr/binMAILTO=root#Fordetailsseeman4crontabs#Exampleofjobdefinition:#.—————-minute(0-59)#|.————-hour(0-23)#||.——-.

    2025年7月11日
    4
  • BEAM188能做屈曲分析吗

    BEAM188能做屈曲分析吗请问高手 BEAM188 189 能做长杆的屈曲分析吗 nbsp 特别要注意的是 一个非收敛的解 并不意味着结构达到了其最大载荷 它也可能是由于数值不稳定引起的 这可以通过细化模型的方法来修正 跟踪结构响应的载荷 变形历程 可以确定一个非收敛的载荷步 到底是表示了一个实际的结构屈曲 还是反映了其它问题 用户可以先用弧长法 ARCLEN 命令来进行一个预分析 以预测屈曲载荷 近似值 将此近似值

    2026年3月19日
    2
  • 6.2 匿名类

    6.2 匿名类和类有关的匿名类 和接口有关的匿名类一 和类有关的匿名类 Java 允许用户直接使用一个类的子类的类体创建一个子类对象在创建子类对象时 除了使用父类的构造方法外还有类体 此类体被认为是一个子类去掉类声明后的类体 称为匿名类使用匿名类创建对象时 要直接使用父类的构造方法匿名类就是一个子类 由于无名可用 所以不可能用匿名类声明对象 但却可以直接用匿名类创建一个对象 不可声明 但

    2026年3月18日
    1
  • sigsuspend() 用法

    sigsuspend() 用法这个函数的用法困扰了我很久 虽然有部分原因是 apue 列子没看明白 但经过一番摸索也加深了我对 sigsuspend 的理解 1 关于 pause 和 sigsuspend 函数 但是不懂他们究竟有些什么区别 在什么时候可以相互替换 什么时候又不可以呢 简单的说 sigsuspend unblock pause nbsp nbsp nbsp nbsp sigsuspend 函数是用于需要先接触某个信号的阻塞状态

    2026年3月17日
    2

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号