SpringBoot - 工作流Activiti开发

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: 1.工作流 1.1 开发前奏 介绍 当前系统activit开发以springboot+mybatis开发为准, 1.1.1 IDEA安装actiBPM 通过File -> Settings -> Plugins 找到actiBPM插件进行安装 1.1.2 核心API介绍 ProcessEngineConfiguration:流程引擎配置。

1.工作流

1.1 开发前奏

介绍

当前系统activit开发以springboot+mybatis开发为准,

1.1.1 IDEA安装actiBPM

通过File -> Settings -> Plugins 找到actiBPM插件进行安装
actiBPM_

1.1.2 核心API介绍
API

ProcessEngineConfiguration:流程引擎配置。
ProcessEngine:流程引擎
核心七大接口
RepositoryService:提供一系列管理流程部署和流程定义的API。
RuntimeService:在流程运行时对流程实例进行管理与控制。
TaskService:对流程任务进行管理,例如任务提醒、任务完成和创建任务等。
IdentityService:提供对流程角色数据进行管理的API,这些角色数据包括用户组、用户及它们之间的关系。
ManagementService:提供对流程引擎进行管理和维护的服务,提供对activiti数据库的直接访问【一般不用】
HistoryService:对流程的历史数据进行操作,包括查询、删除这些历史数据。FormService:表单服务

1.1.3 添加依赖 pom.xml

<dependency>
   <groupId>org.activiti</groupId>
   <artifactId>activiti-spring-boot-starter-basic</artifactId>
   <version>6.0.0</version>
</dependency>

1.1.4 yml配置

spring:
  activiti:
    check-process-definitions: true #自动检查、部署流程定义文件
    database-schema-update: true #自动更新数据库结构
    history-level: full #保存历史数据级别设置为full最高级别,便于历史数据的追溯
    # process-definition-location-prefix: classpath:/processes/ #流程定义文件存放目录
    #process-definition-location-suffixes: #流程文件格式
    #  - **.bpmn20.xml
    #  - **.bpmn
    

springboot环境下不再以activiti.cfg.xml文件的形式配置,activiti使用starter配置后属于spring下,所以在yml里配置。

  • check-process-definitions【检查Activiti数据表是否存在及版本号是否匹配】默认为true,自动创建好表之后设为false。设为false会取消自动部署功能。
  • database-schema-update【在流程引擎启动和关闭时处理数据库模式】如下四个值:

    • false (默认值):在创建流程引擎时检查库模式的版本,如果版本不匹配则抛出异常。
    • true:在创建流程引擎时,执行检查并在必要时对数据库中所有的表进行更新,如果表不存在,则自动创建。 、
    • create-drop:在创建流程引擎时,会创建数据库的表,并在关闭流程引擎时删除数据库的表。
    • drop-create:Activiti启动时,执行数据库表的删除操作,在Activiti关闭时,会执行数据库表的创建操作。
  • history-level 【历史数据保存级别】

    • none:不保存任何的历史数据,因此,在流程执行过程中,这是最高效的。
    • activity:级别高于none,保存流程实例与流程行为,其他数据不保存。
    • audit:除activity级别会保存的数据外,还会保存全部的流程任务及其属性。audit为history的默认值。
    • full:保存历史数据的最高级别,除了会保存audit级别的数据外,还会保存其他全部流程相关的细节数据,包括一些流程参数等。

1.1.5 添加processes目录

SpringBoot集成activiti默认会从classpath下的processes目录下读取流程定义文件,所以需要在src/main/resources目录下添加processes目录,并在目录中创建流程文件.

如果没有processes目录,则需要修改配置spring.activiti.process-definition-location-prefix,指定流程文件存放目录。
processes

1.1.6 其他相关配置

  • 现在系统的启动类排除org.activiti.spring.boot.SecurityAutoConfiguration即可

    @SpringBootApplication(exclude = SecurityAutoConfiguration.class,scanBasePackages="com.poly")
    public class AvtivityApplication {

    public static void main(String[] args) {
        SpringApplication.run(AvtivityApplication.class, args);
    }
    

    }

  • 官方文档给出的启动类配置

    @Configuration
    @ComponentScan
    @EnableAutoConfiguration
    public class MyApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class, args);
    }
    

    }

1.1.7 配置bpmn

  • 通过插件配置
  • 官方文档测试bpmn文件 命名为one-task-process.bpmn20.xml 放入processes目录

    <?xml version="1.0" encoding="UTF-8"?>
    <definitions
            xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
            xmlns:activiti="http://activiti.org/bpmn"
            targetNamespace="Examples">
    
        <process id="oneTaskProcess" name="The One Task Process">
            <startEvent id="theStart" />
            <sequenceFlow id="flow1" sourceRef="theStart" targetRef="theTask" />
            <userTask id="theTask" name="my task" />
            <sequenceFlow id="flow2" sourceRef="theTask" targetRef="theEnd" />
            <endEvent id="theEnd" />
        </process>
    
    </definitions>
    

1.1.8 配置数据源

注意mysql连接池版本

# com.mysql.cj.jdbc.Driver
spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/activiti_test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
    username: root
    password: 12345678

1.2 表介绍

在1.1.2~1.1.8完成后 启动项目会生成如下表
_1

  1. act_ge_ 通用数据表,ge是general的缩写
  2. act_hi_ 历史数据表,hi是history的缩写,对应HistoryService接口
  3. act_id_ 身份数据表,id是identity的缩写,对应IdentityService接口
  4. act_re_ 流程存储表,re是repository的缩写,对应RepositoryService接口,存储流程部署和流程定义等静态数据
  5. act_ru_ 运行时数据表,ru是runtime的缩写,对应RuntimeService接口和TaskService接口,存储流程实例和用户任务等动态数据

    资源库流程规则表
    
    1) act_re_deployment 部署信息表
    
    2) act_re_model  流程设计模型部署表
    
    3) act_re_procdef  流程定义数据表
    
    运行时数据库表
    
    1) act_ru_execution运行时流程执行实例表
    
    2) act_ru_identitylink运行时流程人员表,主要存储任务节点与参与者的相关信息
    
    3) act_ru_task运行时任务节点表
    
    4) act_ru_variable运行时流程变量数据表
    
    历史数据库表
    
    1) act_hi_actinst 历史节点表
    
    2) act_hi_attachment历史附件表
    
    3) act_hi_comment历史意见表
    
    4) act_hi_identitylink历史流程人员表
    
    5) act_hi_detail历史详情表,提供历史变量的查询
    
    6) act_hi_procinst历史流程实例表
    
    7) act_hi_taskinst历史任务实例表
    
    8) act_hi_varinst历史变量表
    
    组织机构表
    
    1) act_id_group用户组信息表
    
    2) act_id_info用户扩展信息表
    
    3) act_id_membership用户与用户组对应信息表
    
    4) act_id_user用户信息表
    
    

bpmn_

1.3 开发实例

1.3.1 项目开发案例

  1. 通过actiBPM设置流程
    在processes下建立bpmn流程,之后更改为bpmn20.xml格式(注:具体xml内容请看项目中processes文件)

1.3.2 个人实例

思路

  1. 员工请假,先创建请假流程
  2. 员工填写请假申请,也可以不填写,直接结束流程
  3. 提交给直接主管审批,如果直接主管拒绝,则重新填写,如果直接主管同意,再到部门主管审批,
  4. 部门主管审批部门主管同意,则请假流程结束,请假成功,如果部门主管不同意,返回员工重新提交申请。
  • 创建流程文件
    _1

代码

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/testm1539766523202" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1539766523202" name="" targetNamespace="http://www.activiti.org/testm1539766523202" typeLanguage="http://www.w3.org/2001/XMLSchema">
  <process id="leave1" isClosed="false" isExecutable="true" processType="None">
    <startEvent id="_2" name="start"/>
    <userTask activiti:assignee="${leave.userId}" activiti:exclusive="true" id="_3" name="submit"/>
    <exclusiveGateway gatewayDirection="Unspecified" id="_4" name="result"/>
    <userTask activiti:assignee="${leave.approver1}" activiti:exclusive="true" id="_5" name="approve1"/>
    <exclusiveGateway gatewayDirection="Unspecified" id="_6" name="result"/>
    <userTask activiti:assignee="${leave.approver2}" activiti:exclusive="true" id="_7" name="approve2"/>
    <exclusiveGateway gatewayDirection="Unspecified" id="_8" name="result"/>
    <endEvent id="_9" name="end"/>
    <sequenceFlow id="_10" sourceRef="_2" targetRef="_3"/>
    <sequenceFlow id="_11" sourceRef="_3" targetRef="_4"/>
    <sequenceFlow id="_12" name="y" sourceRef="_4" targetRef="_5">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==true}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_13" name="n" sourceRef="_4" targetRef="_9">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.submit==false}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_14" sourceRef="_5" targetRef="_6"/>
    <sequenceFlow id="_15" name="n" sourceRef="_6" targetRef="_7">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==true}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_16" sourceRef="_7" targetRef="_8"/>
    <sequenceFlow id="_17" name="y" sourceRef="_6" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree1==false}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_18" name="n" sourceRef="_8" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==false}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_19" name="y" sourceRef="_8" targetRef="_9">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${leave.agree2==true}]]></conditionExpression>
    </sequenceFlow>
  </process>
</definitions>
  • 编写接口

    @RestController
    @RequestMapping("/level/v1")
    public class LeaveController {

    public static final Logger log = LoggerFactory.getLogger(LeaveController.class);
    
    @Autowired
    private RuntimeService runtimeService;
    
    @Autowired
    private TaskService taskService;
    
    @Autowired
    private ProcessEngine processEngine;
    
    /**
     * 启动流程
     * @param userId
     * @return
     */
    @RequestMapping(value = "/start", method = RequestMethod.GET)
    public Map<String, Object> start(@RequestParam String userId){
        Map<String, Object> vars = new HashMap<>();
        Leave leave = new Leave();
        leave.setUserId(userId);
        vars.put("leave",leave);
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave1",vars);
        Map<String, Object> resultMap = new HashMap<>();
        return resultMap;
    }
    
    /**
     * 填写请假单
     * @param leave
     * @return
     */
    @RequestMapping(value="/apply", method = RequestMethod.POST)
    public Map<String, Object> apply(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map<String, Object> vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setDesc(leave.getDesc());
        origin.setStartDate(leave.getStartDate());
        origin.setEndDate(leave.getEndDate());
        origin.setTotalDay(leave.getTotalDay());
        origin.setApprover1(leave.getApprover1());
        origin.setApprover2(leave.getApprover2());
        origin.setSubmit(leave.getSubmit());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(), vars);
        Map<String, Object> resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }
    
    /**
     * 查询用户流程
     * @param userId
     * @return
     */
    @RequestMapping(value = "/find", method = RequestMethod.GET)
    public Map<String, Object> find(@RequestParam("userId")String userId){
        List<Task> taskList = taskService.createTaskQuery().taskAssignee(userId).list();
        List<Leave> resultList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(taskList)){
            for(Task task : taskList){
                Leave leave = (Leave) taskService.getVariable(task.getId(),"leave");
                leave.setTaskId(task.getId());
                leave.setTaskName(task.getName());
                resultList.add(leave);
            }
        }
        Map<String, Object> resultMap = ResultMapHelper.getSuccessMap();
        resultMap.put("datas", resultList);
        return resultMap;
    }
    
    /**
     * 直接主管审批
     * @param leave
     * @return
     */
    @RequestMapping(value = "/approve1", method = RequestMethod.POST)
    public Map<String, Object> approve1(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map<String, Object> vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setApproveDesc1(leave.getApproveDesc1());
        origin.setAgree1(leave.getAgree1());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(),vars);
        Map<String, Object> resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }
    
    /**
     * 部门主管审批
     * @param leave
     * @return
     */
    @RequestMapping(value = "/approve2", method = RequestMethod.POST)
    public Map<String, Object> approve2(@RequestBody Leave leave){
        Task task = taskService.createTaskQuery().taskId(leave.getTaskId()).singleResult();
        Map<String, Object> vars = new HashMap<>();
        Leave origin = (Leave) taskService.getVariable(leave.getTaskId(), "leave");
        origin.setApproveDesc2(leave.getApproveDesc2());
        origin.setAgree2(leave.getAgree2());
        vars.put("leave", origin);
        taskService.complete(leave.getTaskId(),vars);
        Map<String, Object> resultMap = ResultMapHelper.getSuccessMap();
        return resultMap;
    }
    
    /**
     * 查看历史记录
     * @param userId
     * @return
     */
    @RequestMapping(value="/findClosed", method = RequestMethod.GET)
    public Map<String, Object> findClosed(String userId){
        HistoryService historyService = processEngine.getHistoryService();
    
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave1").variableValueEquals("leave.userId",userId).list();
        List<Leave> leaves = new ArrayList<>();
        for(HistoricProcessInstance pi : list){
            leaves.add((Leave) pi.getProcessVariables().get("leave"));
        }
        Map<String, Object> resultMap = ResultMapHelper.getSuccessMap();
        resultMap.put("datas", leaves);
        return resultMap;
    }
    

    }

public class Leave implements Serializable {
    private static final long serialVersionUID = 2248469053125414262L;
 
    private String userId;
 
    private Boolean submit;
 
    private Date startDate;
 
    private Date endDate;
 
    private float totalDay;
 
    private String desc;
 
    private String taskId;
 
    private String taskName;
 
    private String approver1;
 
    private Boolean agree1;
 
    private String approveDesc1;
 
    private String approver2;
 
    private Boolean agree2;
 
    private String approveDesc2;
}
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
2月前
|
前端开发 JavaScript Java
基于Springboot+SpringSecurity+Activiti7实现的工作流系统可方便二次开发(附完整源码)
基于Springboot+SpringSecurity+Activiti7实现的工作流系统可方便二次开发(附完整源码)
55 0
|
2月前
|
监控 Java API
|
7月前
|
Oracle Java 关系型数据库
Java SpringBoot集成Activiti7工作流
Java SpringBoot集成Activiti7工作流
|
5月前
|
SQL Java 关系型数据库
模仿Activiti工作流自动建表机制,实现Springboot项目启动后自动创建多表关联的数据库与表的方案
在一些本地化项目开发当中,存在这样一种需求,即开发完成的项目,在第一次部署启动时,需能自行构建系统需要的数据库及其对应的数据库表。
36 1
|
8月前
|
SQL Java 关系型数据库
Activiti工作流框架学习笔记(二)之springboot2.0整合工作流Activiti6.0
Activiti工作流框架学习笔记(二)之springboot2.0整合工作流Activiti6.0
129 0
|
网络协议 前端开发 Java
SpringBoot+Netty开发IM即时通讯系列(一)
简单来讲,Netty是一个提供了易于使用的API的客户端/服务端框架。Netty并发非常高,一个非阻塞的IO,Netty传输速度也非常快,因为他是0拷贝,什么是零拷贝?NIO中的特性之一就是零拷贝,在Java中,内存分为堆和栈以及字符串常量值等等,如果有一些数据从IO中读取并且放到堆里面,中间会经过一些缓冲区。
936 0
SpringBoot+Netty开发IM即时通讯系列(一)
|
11月前
|
存储 消息中间件 JavaScript
Spring Boot + flowable 快速实现工作流
Spring Boot + flowable 快速实现工作流
Spring Boot + flowable 快速实现工作流
|
JSON Java fastjson
使用spring boot开发时java对象和Json对象转换
使用spring boot开发时java对象和Json对象转换
1287 1
使用spring boot开发时java对象和Json对象转换
|
XML NoSQL Java
SpringBoot 开发总结思考(二)
模块装配:假设要注入MongoDB,那么就加上@Configuration注解,有可能一个配置类没办法解决某个方向的问题,往往是很多@Configuration的类组合在一起SpringBoot是使用Enable注解,然后再通过@import导入Selector,通过Selector读取 .factories 文件,最终加载的Configuration
153 0
SpringBoot 开发总结思考(二)
|
XML Java 关系型数据库
SpringBoot 开发总结思考(一)
从面向对象的角度理解XML,是以类和对象作为XML的配置SpringBoot 使用的是配置类加上普通/常规配置的形式,参数不是直接固定在配置类中,而可以写在配置文件中,例如application.properties
106 0
SpringBoot 开发总结思考(一)