概念
核心组件
api介绍
springboot集成
具体业务集成
api使用
前言
项目中需要用到工作流引擎来设计部分业务流程,框架选型最终选择了 camunda7,关于 camunda以及 activity 等其他工作流 引擎的介绍及对比不再介绍,这里只介绍与现有springboot项目的集成以及具体使用及配置
基于 spring boot + mybatis plus + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://github.com/yunaiv/ruoyi-vue-pro
视频教程:https://doc.iocoder.cn/video/
概念
流程(process) : 通过工具建模最终生成的bpmn文件,里面有整个流程的定义
流程实例(instance) :流程启动后的实例
流程变量(variables) :流程任务之间传递的参数
任务(task) :流程中定义的每一个节点
流程部署 :将之前流程定义的.bpmn文件部署到工作流平台
基于 spring cloud alibaba + gateway + nacos + rocketmq + vue & element 实现的后台管理系统 + 用户小程序,支持 rbac 动态权限、多租户、数据权限、工作流、三方登录、支付、短信、商城等功能
项目地址:https://github.com/yunaiv/yudao-cloud
视频教程:https://doc.iocoder.cn/video/
核心组件
process engine -流程引擎
web applicatons - 基于web的管理页面
api介绍
官方文档
https://docs.camunda.org/manual/7.18/user-guide/process-engine/process-engine-api/
下面是官网的一些文档,有时间可以看看,下面说一些核心的东西。
processengine
为流程引擎,可以通过他获取相关service,里面集成了很多相关service,默认实现如下:
repositoryservice
此服务提供用于管理和操作部署和流程定义的操作,使用camunda的第一要务
runtimeservice
运行相关,启动流程实例、删除、搜索等
taskservice
所有围绕任务相关的操作,如完成、分发、认领等
historyservice
提供引擎搜集的历史数据服务
identityservice
用户相关,实际中用不太到
springboot集成
依赖集成
maven
https://mvnrepository.com/search?q=org.camunda.bpm.springboot
可以根据需要引用版本,我这边用的是 7.18
需要3个maven依赖,分别是对应 流程引擎、web管理平台、提供rest api操作接口包
org.camunda.bpm.springboot camunda-bpm-spring-boot-starter 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-rest 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-webapp 7.18.0
数据库
我这边使用的是mysql,建了个新库 camunda(可自定义),启动后会自动生成所需表结构
pom文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.3 com.example camunda-demo 0.0.1-snapshot camunda-demo camunda-demo 17 org.springframework.boot spring-boot-starter org.camunda.bpm.springboot camunda-bpm-spring-boot-starter 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-rest 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-webapp 7.18.0 mysql mysql-connector-java 8.0.32 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
application.yml
server: port: 8081 # camunda登录信息配置camunda.bpm: admin-user: id: admin #用户名 password: 123456 #密码 firstname: yu filter: create: all tasks # mysql连接信息spring: datasource: driver-class-name: com.mysql.cj.jdbc.driver url: jdbc//localhost:8101/camunda username: root password: 123456 type: com.mysql.cj.jdbc.mysqldatasource
启动效果
准备好前置工作,启动后效果如下:
数据库表结构
启动后自动生成的表结构如下
大概有这么几个表模块,重要的详细介绍下:
act_id_
这部分表示用户模块,配置文件里面的用户,信息就在此模块
act_hi_
表示流程历史记录
act_hi_actinst: 执行的活动历史
act_hi_taskinst:执行任务历史
act_hi_procinst:执行流程实例历史
act_hi_varinst:流程变量历史表
act_re_
表示流程资源存储
act_re_procdef:流程定义存储
act_re_deployment: 自动部署,springboot每次启动都会重新部署,生成记录
act_ru_
表示流程运行时表数据,流程结束后会删除
act_ru_execution:运行时流程实例
act_ru_task:运行时的任务
act_ru_variable:运行时的流程变量
act_ge_
流程通用数据
act_ge_bytearray:每次部署的文件2进制数据,所以如果文件修改后,重启也没用,因为重新生成了记录,需要清掉数据库,或者这个表记录
登录界面
登录地址为 http://localhost:8081/,输入用户名密码即为配置文件里面的 admin,123456
主控制台
登陆成功后,如下所示,具体的使用在下面介绍
具体业务集成
绘制流程图
下载
首先需要一个工具 camunda modeler 来画,下载地址:
https://camunda.com/download/modeler/
解压缩后打开如下:
绘制
新建一个
我这边稍微画了一个,具体怎么画,就不在细说了,最后效果如下,模拟了个oa的流程
任务分类
只介绍最常用的两种
用户任务 (user task)
具体来说就是需要手动执行的任务,即需要我们这变写完业务代码后,调用代码
taskservice.complete(taskid, variables);
才会完成的任务
系统任务(service task)
系统会自动帮我们完成的任务
网关
分为这么几类,会根据我们传入的流程变量及设定的条件走
排他网关(exclusive gateway)
这个网关只会走一个,我们走到这个网关时,会从上到下找第一个符合条件的任务往下走
并行网关(parallel gateway)
这个网关不需要设置条件,会走所有的任务
包含网关(inclusive gateway)
这个网关会走一个或者多个符合条件的任务
示例
如上图包含网关,需要在网关的连线初设置表达式 condition,参数来自于流程变量
两个参数:
switch2d 、 switch3d
如果 都为true,则走任务1,3
如果 switch2d 为true switch3d为false,则只走任务1
如果 switch3d 为true switch2d为false,则只走任务3
如果都为false,则直接走网关,然后结束
引入项目
将画好的流程图保存文件为 test_1.bpmn,在刚才的springboot项目中resources新建一个bpmn文件夹,放进去,
重启项目,发现web界面中已经被集成进来了
具体开发
写几个测试controller和service
controller
service
public void startprocess() { processinstance instance = runtimeservice.startprocessinstancebykey(key); system.out.println(instance.tostring());}public list findprocesses() { return repositoryservice.createprocessdefinitionquery().list();}public list findtasks() { return taskservice.createtaskquery().list();}
启动流程成功,说明问题不大,接下来详细业务改进。
下一篇介绍详细的业务集成及各种api(变量传递、自动任务)的使用
api使用
流程相关api
创建流程:
会同时创建第一个任务
processinstance instance = runtimeservice.startprocessinstancebykey(processkey, params);
暂停流程
流程暂停后,再执行相关任务会报错,需要先重新激活任务
runtimeservice.suspendprocessinstancebyid(instance.getid());
重新激活流程
runtimeservice.activateprocessinstancebyid(instance.getid());
删除流程
会同时删除任务
runtimeservice.deleteprocessinstance(instance.getid(), 手动删除);
以上都可以在流程历史表 act_hi_procinst 里查询
任务相关api
基于service的查询类,都可先构建一个 query,然后在附上查询条件,实例几个
list list = repositoryservice.createprocessdefinitionquery().list();list list = taskservice.createtaskquery().taskassignee(zhangsan).list();list instances = runtimeservice.createprocessinstancequery().listpage(1, 10);
查询历史任务
list list = historyservice.createhistoricprocessinstancequery().list();
查询当前任务/分页
list list = taskservice.createtaskquery().orderbytaskcreatetime().desc().list();
任务回退
大体思路是拿到当前的任务,及当前任务的上一个历史任务,然后重启
代码示例
task activetask = taskservice.createtaskquery() .taskid(taskid) .active() .singleresult(); list historictaskinstance = historyservice.createhistorictaskinstancequery() .processinstanceid(instanceid) .orderbyhistoricactivityinstancestarttime() .desc() .list(); list historictaskinstances = historictaskinstance.stream().filter(v -> !v.gettaskdefinitionkey().equals(activetask.gettaskdefinitionkey())).tolist(); assert.notempty(historictaskinstances, 当前已是初始任务!); historictaskinstance curr = historictaskinstances.get(0); runtimeservice.createprocessinstancemodification(instanceid) .cancelallforactivity(activetask.gettaskdefinitionkey()) .setannotation(重新执行) .startbeforeactivity(curr.gettaskdefinitionkey()) .execute();
流程变量
包括流程中产生的变量信息,包括控制流程流转的变量,网关、业务表单中填写的流程需要用到的变量等。很多地方都要用到
流程变量变量传递
变量最终会存在 act_ru_variable 这个表里面
在绘制流程图的时候,如果是用户任务(userservice) 可以设置变量,比如执行人,
写法有这么几种方式
写死,就比如 zhangsan
表达式,比如上面写的 ${user},这种需要传入参数,其实就是启动参数的时候传入,传入参数,可选值为一个map,之后的流程可查看次参数,上面写的是 user, 所以map里面的key需要带着user,不然会报错。
关于扩展变量,可在流程图绘制这么设定,传递方式还是一样,流程图里面在下面写:
代码:
processinstance instance = runtimeservice.startprocessinstancebykey(key, new hashmap());
变量设置
runtimeservice.setvariable(instance.getid(), constants.patient_id, relatedid);
变量查询
object variable = runtimeservice.getvariable(instance.getid(), constants.general_id);
历史变量查询
historicvariableinstance variableinstance = historyservice.createhistoricvariableinstancequery().processinstanceid(bo.getid().tostring()). variablename(constants.patient_id).singleresult();//变量值variableinstance.getvalue();//变量名称variableinstance.getname();
针对后端来说任务类型主要有两种。
用户任务-usertask
即需要用户参与的任务,因为工作流执行过程中需要涉及到审批、过审之类的需要用户参与的任务,这个时候需要用户参与,然后调用接口完成任务。
服务任务-servicetask
即自动执行的任务,比如用户提交后,系统自动存储、修改状态等自动完成的任务。
type
任务类型是关键,可根据配型配置实现调用 java的方法,spring 的bean方法,等等有这么几种类型
推荐使用 -- delegate expression !!!
在系统任务中,因为是自动执行,所以实际应用中需要嵌入各种业务逻辑,可以在流程图设计中,按照下面方式调用java代码执行,在spring中配置同名的bean
配置表达式,可以实现javadelegate接口使用类名配置,快捷写法如下,比较推荐下面这种,此种可灵活配置bean和spring结合使用,注入service等业务方法
@bean(t17)javadelegate t17() { return execution -> { map variables = execution.getvariables(); task task = taskservice.createtaskquery().processinstanceid(execution.getprocessinstanceid()).singleresult(); //业务逻辑 task.setowner(string.valueof(dentistid)); };}
java class :
配置java类名,需要实现javadelegate接口,注意是全路径名,不可以使用spring的bean配置!!!
@componentpublic class t17delegate implements javadelegate { @override public void execute(delegateexecution execution) throws exception { string taskid = execution.getid(); string instanceid = execution.getprocessinstanceid(); map variables = execution.getvariables(); }}
下面两种可使用spring的配置
expression:
el表达式,调用java类的方法 ,规范:
expression=“#{monitorexecution.execution(execution)}”@component(monitorexecution)public class monitorexecution { public void execution(delegateexecution execution){ string processinstanceid = execution.getprocessinstanceid(); }}
任务监听器 - task listener
任务监听器用于在某个与任务相关的事件发生时执行自定义java逻辑或表达式。它只能作为用户任务的子元素添加到流程定义中。
请注意,这也必须作为bpmn 2.0扩展元素的子级和camunda命名空间中发生,因为任务侦听器是专门为camunda引擎构建的。
适用场景:
@beantasklistener t21() { return delegatetask -> { string taskid = delegatetask.getid(); string instanceid = delegatetask.getprocessinstanceid(); map variables = delegatetask.getvariables(); // todo: 20log/3/22 delegatetask.setvariable(, ); };}
执行监听器 - execution listener
执行侦听器在流程执行过程中发生某些事件时执行外部java代码或计算表达式。可以用在任何任务中,可以捕获的事件有:
流程实例的开始和结束。
进行过渡。
活动的开始和结束。
网关的开始和结束。
中间事件的开始和结束。
结束开始事件或开始结束事件
适用场景:每个任务结束时设置任务进度
public class exampleexecutionlistenerone implements executionlistener { public void notify(delegateexecution execution) throws exception { execution.setvariable(variablesetinexecutionlistener, firstvalue); execution.setvariable(eventreceived, execution.geteventname()); } }
扩展属性- extension properties
扩展属性适用于很多自定义的业务属性,比如设置业务流程进度
流程权限及创建人设置
identityservice为鉴权相关服务,但是我们实际开发中,一般会用到我们自己的鉴权系统,所以可以使用camunda提供的api来设置,具体可以看identityserviceimpl这个类,其中也是使用了threadlocal来保存鉴权信息 ,代码在下面
private threadlocal currentauthentication = new threadlocal();
用户信息设置:
// userutil是我们自己封装的用户工具类identityservice.setauthenticateduserid(userutil.getuserid().tostring()); //获取authentication authentication = identityservice.getcurrentauthentication();
他内置很多比如开启流程时候,会默认找当前登录的人,这个类defaulthistoryeventproducer
// set super process instance id executionentity superexecution = executionentity.getsuperexecution(); if (superexecution != null) { evt.setsuperprocessinstanceid(superexecution.getprocessinstanceid()); } //state evt.setstate(historicprocessinstance.state_active); // set start user id evt.setstartuserid(context.getcommandcontext().getauthenticateduserid());
任务执行人及发起人设置
//根据任务id设置执行人taskservice.setassignee(task.getid(), userutil.getuserid().tostring());
消费者对无线充电技术的喜爱有所增加
苹果15芯片是多少纳米?苹果15芯片几纳米的?
半导体先进封装产业如何破局?听行业大咖纵论“芯”思路,助力中国“芯”!
全新英飞凌功率MOSFET系列使电动工具更紧凑耐用
微软宣布了五款新的支持 LTE 的合作伙伴的 Windows 10 笔记本设备
Springboot项目的集成以及具体使用及配置
影响网络质量的4个要素
电力电容器额定电压过大会怎样?
深度解析开关电源的LLC电路设计原理图
盐雾箱饱和桶和箱体的温度怎么会有这些区别?
高侧电流传感器显示正负电源-High-Side Positi
fireflyFace-RK3399主板I2C控制器介绍
车载芯片的发展趋势是什么?GPU和ASIC的发展方向与应用的概述
预计爱立信年底全球5G用户数达1.9亿,到2025年底将达到28亿
高频/射频信号及视频压缩编码的基本概念
有毒有害气体检测仪在环保监测领域的应用
TDK 负温度系数 (NTC) 热敏电阻: 坚固耐用的温度传感器,测量温度高达300℃
新思科技数字定制设计平台已获台积公司N3制程技术认证
华为mate9pro抢夺海外市场,已经攻下台湾 iPhone7泪奔
微信如何找回聊天记录