SpringBoot集成Flowable工作流

news/2024/10/18 5:59:20/

SpringBoot集成Flowable工作流

  • Flowable是什么?
  • 一、添加依赖
  • 二、flowable配置
  • 三、定义流程文件
    • 1.使用流程文件定义工作流
    • 2.idea使用插件来定义流程图
      • 1.安装插件
      • 2.创建bpmn文件并画流程图
      • 3.右击流程用模型设计器打开文件
  • 四、测试controller


Flowable是什么?

官方文档:https://tkjohn.github.io/flowable-userguide/#_introduction

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

一、添加依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>6.4.1</version></dependency>
</dependencies>

二、flowable配置

spring:datasource:url: jdbc:mysql://127.0.0.1:3306/flowableDemo?useUnicode=true&characterEncoding=utf-8&&serverTimezone=UTC&nullCatalogMeansCurrent=trueusername: rootpassword: root
flowable:#关闭定时任务JOBasync-executor-activate: false

初次运行时flowable会将自动执行flowable中的初始化脚本完成工作流所需要的数据表的建立

如果出现不自动创建表的情况:
1.数据库是mysql8需要在连接字符串中添加&nullCatalogMeansCurrent=true
2.去除mysql引入依赖中runtime

三、定义流程文件

flowable建议采用业界标准BPMN2.0的XML来描述需要定义的工作流。

1.使用流程文件定义工作流

为了方便测试,这里采用一个开源项目中的流程文件,其描述如下:
ExpenseProcess.bpmn20.xml

<?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: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="Expense" name="ExpenseProcess" isExecutable="true"><documentation>报销流程</documentation><startEvent id="start" name="开始"></startEvent><userTask id="fillTask" name="出差报销" flowable:assignee="${taskUser}"><extensionElements><modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete></extensionElements></userTask><exclusiveGateway id="judgeTask"></exclusiveGateway><userTask id="directorTak" name="经理审批"><extensionElements><flowable:taskListener event="create"class="com.erfou.flowabledemo.listener.ManagerTaskHandler"></flowable:taskListener></extensionElements></userTask><userTask id="bossTask" name="老板审批"><extensionElements><flowable:taskListener event="create"class="com.erfou.flowabledemo.listener.BossTaskHandler"></flowable:taskListener></extensionElements></userTask><endEvent id="end" name="结束"></endEvent><sequenceFlow id="directorNotPassFlow" name="驳回" sourceRef="directorTak" targetRef="fillTask"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='驳回'}]]></conditionExpression></sequenceFlow><sequenceFlow id="bossNotPassFlow" name="驳回" sourceRef="bossTask" targetRef="fillTask"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='驳回'}]]></conditionExpression></sequenceFlow><sequenceFlow id="flow1" sourceRef="start" targetRef="fillTask"></sequenceFlow><sequenceFlow id="flow2" sourceRef="fillTask" targetRef="judgeTask"></sequenceFlow><sequenceFlow id="judgeMore" name="大于500元" sourceRef="judgeTask" targetRef="bossTask"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${money > 500}]]></conditionExpression></sequenceFlow><sequenceFlow id="bossPassFlow" name="通过" sourceRef="bossTask" targetRef="end"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通过'}]]></conditionExpression></sequenceFlow><sequenceFlow id="directorPassFlow" name="通过" sourceRef="directorTak" targetRef="end"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${outcome=='通过'}]]></conditionExpression></sequenceFlow><sequenceFlow id="judgeLess" name="小于500元" sourceRef="judgeTask" targetRef="directorTak"><conditionExpression xsi:type="tFormalExpression"><![CDATA[${money <= 500}]]></conditionExpression></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_Expense"><bpmndi:BPMNPlane bpmnElement="Expense" id="BPMNPlane_Expense"><bpmndi:BPMNShape bpmnElement="start" id="BPMNShape_start"><omgdc:Bounds height="30.0" width="30.0" x="285.0" y="135.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="fillTask" id="BPMNShape_fillTask"><omgdc:Bounds height="80.0" width="100.0" x="405.0" y="110.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="judgeTask" id="BPMNShape_judgeTask"><omgdc:Bounds height="40.0" width="40.0" x="585.0" y="130.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="directorTak" id="BPMNShape_directorTak"><omgdc:Bounds height="80.0" width="100.0" x="735.0" y="110.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="bossTask" id="BPMNShape_bossTask"><omgdc:Bounds height="80.0" width="100.0" x="555.0" y="255.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="end" id="BPMNShape_end"><omgdc:Bounds height="28.0" width="28.0" x="771.0" y="281.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="315.0" y="150.0"></omgdi:waypoint><omgdi:waypoint x="405.0" y="150.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="505.0" y="150.16611295681062"></omgdi:waypoint><omgdi:waypoint x="585.4333333333333" y="150.43333333333334"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="judgeLess" id="BPMNEdge_judgeLess"><omgdi:waypoint x="624.5530726256983" y="150.44692737430168"></omgdi:waypoint><omgdi:waypoint x="735.0" y="150.1392757660167"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="directorNotPassFlow" id="BPMNEdge_directorNotPassFlow"><omgdi:waypoint x="785.0" y="110.0"></omgdi:waypoint><omgdi:waypoint x="785.0" y="37.0"></omgdi:waypoint><omgdi:waypoint x="455.0" y="37.0"></omgdi:waypoint><omgdi:waypoint x="455.0" y="110.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="bossPassFlow" id="BPMNEdge_bossPassFlow"><omgdi:waypoint x="655.0" y="295.0"></omgdi:waypoint><omgdi:waypoint x="771.0" y="295.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="judgeMore" id="BPMNEdge_judgeMore"><omgdi:waypoint x="605.4340277777778" y="169.56597222222223"></omgdi:waypoint><omgdi:waypoint x="605.1384083044983" y="255.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="directorPassFlow" id="BPMNEdge_directorPassFlow"><omgdi:waypoint x="785.0" y="190.0"></omgdi:waypoint><omgdi:waypoint x="785.0" y="281.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="bossNotPassFlow" id="BPMNEdge_bossNotPassFlow"><omgdi:waypoint x="555.0" y="295.0"></omgdi:waypoint><omgdi:waypoint x="455.0" y="295.0"></omgdi:waypoint><omgdi:waypoint x="455.0" y="190.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

其中的两个代理类为:

import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;public class ManagerTaskHandler implements TaskListener {@Overridepublic void notify(DelegateTask delegateTask) {delegateTask.setAssignee("经理");}}
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;public class BossTaskHandler implements TaskListener {@Overridepublic void notify(DelegateTask delegateTask) {delegateTask.setAssignee("老板");}}

2.idea使用插件来定义流程图

1.安装插件

在这里插入图片描述

2.创建bpmn文件并画流程图

在resource目录下新建文件夹process,在process文件夹上右键新建,创建出来的文件是xml,项目在启动的时候会自动部署流程,无需手动部署
在这里插入图片描述

3.右击流程用模型设计器打开文件

在这里插入图片描述

四、测试controller

package com.erfou.flowabledemo.controller;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.stereotype.Controller;
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;@Controller
@RequestMapping(value = "expense")
public class ExpenseController {@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate TaskService taskService;@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate ProcessEngine processEngine;/***************此处为业务代码******************//*** 添加报销** @param userId    用户Id* @param money     报销金额* @param description 描述*/@RequestMapping(value = "add")@ResponseBodypublic String addExpense(String userId, Integer money, String description) {//启动流程HashMap<String, Object> map = new HashMap<>();map.put("taskUser", userId);map.put("money", money);ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("Expense", map);return "提交成功.流程Id为:" + processInstance.getId();}/*** 获取审批管理列表*/@RequestMapping(value = "/list")@ResponseBodypublic Object list(String userId) {List<Task> tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();for (Task task : tasks) {System.out.println(task.toString());}return tasks.toString();}/*** 批准** @param taskId 任务ID*/@RequestMapping(value = "apply")@ResponseBodypublic String apply(String taskId) {Task task = taskService.createTaskQuery().taskId(taskId).singleResult();if (task == null) {throw new RuntimeException("流程不存在");}//通过审核HashMap<String, Object> map = new HashMap<>();map.put("outcome", "通过");taskService.complete(taskId, map);return "processed ok!";}/*** 拒绝*/@ResponseBody@RequestMapping(value = "reject")public String reject(String taskId) {HashMap<String, Object> map = new HashMap<>();map.put("outcome", "驳回");taskService.complete(taskId, map);return "reject";}/*** 生成流程图** @param processId 任务ID*/@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的IdList<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,true);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();}}}}

http://www.ppmy.cn/news/836898.html

相关文章

星际战甲计算机硬盘不够,win10星际战甲游戏出现无法更新的三种解决方法

星际战甲一款科幻题材的第三人称射击网游&#xff0c;为得到更好体验&#xff0c;一般要定时对星际战甲游戏升级更新。可能win10系统在更新星际战甲游戏时出现无法更新&#xff0c;怎么办&#xff1f;可能是因为电脑本地网络不通畅活&#xff0c;或者游戏客户端本身服务器出现了…

本质安全设备标准(IEC60079-11)的理解(五)绝对可靠器件infallible components

在前面的章节中我们简单提及到绝对可靠器件&#xff0c; 这里重新摘抄如下&#xff1a; “这里顺便说一下可靠元器件&#xff08;infallible component&#xff09;的理解。它在标准里面占有不少的章节&#xff0c;而且开始理解他也有一些费劲。本人从两个方面理解它 &#x…

Linux 学习记录50(QT篇)

Linux 学习记录50(QT篇) 本文目录 Linux 学习记录50(QT篇)一、基于QT的TCP客户端连接1. 代码实现2. 自制的客户端/服务器 二、QT数据库SQL1. QT将数据库分为三个层次2. 实现数据库操作的相关方法 一、基于QT的TCP客户端连接 所需头文件 <QTcpSocket> 1. 代码实现 QStr…

设计模式——命令模式

命令模式 定义 将一个请求封装成一个对象&#xff0c;从而让你使用不同的请求吧客户端参数化&#xff0c;对请求排队或者记录请求日志&#xff0c;可以提供命令的撤销和恢复功能。 命令模式是一个高内聚的模式。 优缺点、应用场景 优点 类间解耦。调用者与接收者之间没有任…

西门子1200四轴程序,FB284伺服v90profinet 程序西门子触摸屏程 序,新能源行业自动排列机,真实项目

西门子1200四轴程序&#xff0c;FB284伺服v90profinet 程序西门子触摸屏程 序&#xff0c;新能源行业自动排列机&#xff0c;真实项目&#xff0c;V90 走PN 口控制4轴&#xff0c;自己写的RFID读写FB SCL和西门子运动控制FB块开放&#xff0c;直接复制可用&#xff0c;没有密码…

Z世代新消费主义:如何把握体验经济的增量新机遇

点击上方蓝字关注我们 如果说被称为“The Net Generation”&#xff08;网络一代&#xff09;的“千禧一代”伴随着互联网的诞生和发展&#xff0c;那么 Z 世代早已能够自然地沐浴在移动互联网的“空气”中。如今&#xff0c;最早出生的 Z 世代已经步入社会、成为青年白领的主…

德国西门子在LED可见光通讯技术方面有了新突破

采用LED可见光作为通讯传输的技术又有了新的突破。德国西门子&#xff08;Siemens&#xff09;近日表示&#xff0c;该公司和德国Heinrich Rudolf 研究所的科学家成功合作&#xff0c;在LED可见光通讯技术上有了更新进一步的突破&#xff0c;让LED可见光通讯传输速率最高可提升…

西门子PLC200smart学习浅谈

针对于PLC200smart的学习我也写了很多文章&#xff0c;发现很多人还是遇到了大量的问题&#xff0c;所以我个人会写一些实时的文章和经验方法分享给大家&#xff0c;方法如下&#xff0c;有需要的可以关注一下。 为了能更高效的沟通我也想过使用群之类的&#xff0c;后来想了想…