原创

Spring Boot集成activiti快速入门Demo

1.什么事activiti?

Activiti是一个工作流引擎,可以将业务系统中复杂的业务流程抽取出来,使用专门的建模语言BPMN2.0进行定义,业务流程按照预先定义的流程进行执行,实现了系统的流程流activiti进行管理,减少业务系统由于流程变更进行系统升级改造的工作量,从而提高系统的健壮性,同时也减少了系统开发维护成本.

Activiti 的核心服务组件

  1. RepositoryService:提供一系列管理流程部署和流程定义的API
  2. RuntimeService:在流程运行时对流程实例进行管理与控制。
  3. TaskService:对流程任务进行管理,例如任务提醒、任务完成和创建任务等。
  4. IdentityService:提供对流程角色数据进行管理的API,这些角色数据包括用户组、用户及它们之间的关系。
  5. ManagementService:提供对流程引擎进行管理和维护的服务。
  6. HistoryService:对流程的历史数据进行操作,包括查询、删除这些历史数据。
  7. FormService:表单服务。

业务流程模型 BPMN xml 配置文件

一个xml文件,activiti去解析这个文件,了解我们到底想干什么事。可以使用ideal插件【Activiti BPMN visualizer】来编辑这个文件,插件具体使用方法可以Google搜索一下,这里就不具体描述了 2

2.mysql数据库环境搭建

为什么需要mysql?

因为activiti会生成很多流程执行的表。表都是 ACT 开头,用以记录工作流产生的记录数据!如下图所示

表结构介绍

表分类 表名 解释
一般数据
ACT_GE_BYTEARRAY 通用的流程定义和流程资源
ACT_GE_PROPERTY 系统相关属性
流程历史记录
ACT_HI_ACTINST 历史的流程实例
ACT_HI_ATACHMENT 历史的流程附件
ACT_HI_COMMENT 历史的说明性信息
ACT_HI_DETAIL 历史的流程运行中的细节信息
ACT_HI_IDENTITYLINK 历史的流程运行过程中用户关系
ACT_HI_PROCINST 历史的流程实例
ACT_HI_TASKINST 历史的任务实例
ACT_HI_VARINST 历史的流程运行中的变量信息
流程定义表
ACT_RE_DEPLOYMENT 部署单元信息
ACT_RE_MODEL 模型信息
ACT_RE_PROCDEF 已部署的流程定义
运行实例表
ACT_RU_EVENT_SUBSCR 运行时事件
ACT_RU_EXECUTION 运行时流程执行实例
ACT_RU_IDNTITYLINK 运行时用户关系信息,存储任务节点与参与者的相关信息
ACT_RU_JOB 运行时作业
ACT_RU_TASK 运行时任务
ACT_RU_VARIABLE 运行时变量表

安装

docker run --name docker-mysql-5.7 -e MYSQL_ROOT_PASSWORD=123456 -p 3306:3306 -d mysql:5.7

初始化

create database activity;

默认用户和密码

msyql username:root
mysql password:123456

3.代码工程

实现目的:实现流程驱动

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springboot-demo</artifactId>
        <groupId>com.et</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>activiti</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-basic</artifactId>
            <version>6.0.0</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
    </dependencies>
</project>

application.properties

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/activity?characterEncoding=utf8&useSSL=true
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.show-sql=true
server.session.timeout=10
server.tomcat.uri-encoding=UTF-8
server.port=8088

test.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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.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" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="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.activiti.org/testm1510735932336" id="m1510735932336" name="">
  <process id="leave" isExecutable="true" isClosed="false" processType="None">
    <startEvent id="_2" name="StartEvent"/>
    <endEvent id="_3" name="EndEvent"/>
    <userTask id="approve" name="manager approve" activiti:assignee="${approve}"/>
    <exclusiveGateway id="_5" name="ExclusiveGateway"/>
    <sequenceFlow id="_6" sourceRef="approve" targetRef="_5"/>
    <sequenceFlow id="_7" name="pass" sourceRef="_5" targetRef="_3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${pass}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="application" name="apply submit" activiti:assignee="${apply}"/>
    <sequenceFlow id="_9" sourceRef="_2" targetRef="application"/>
    <sequenceFlow id="_10" sourceRef="application" targetRef="approve"/>
    <userTask id="modify" name="modify apply" activiti:assignee="${apply}"/>
    <sequenceFlow id="_12" name="fail " sourceRef="_5" targetRef="modify">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!pass}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="_13" sourceRef="modify" targetRef="approve"/>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave">
    <bpmndi:BPMNPlane bpmnElement="leave" id="BPMNPlane_leave">
      <bpmndi:BPMNShape bpmnElement="_2" id="BPMNShape__2">
        <omgdc:Bounds height="35.0" width="35.0" x="15.0" y="60.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_3" id="BPMNShape__3">
        <omgdc:Bounds height="35.0" width="35.0" x="630.0" y="63.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="approve" id="BPMNShape_approve">
        <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="50.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="_5" id="BPMNShape__5">
        <omgdc:Bounds height="40.0" width="40.0" x="505.0" y="60.5"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="application" id="BPMNShape_application">
        <omgdc:Bounds height="55.0" width="85.0" x="135.0" y="50.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="modify" id="BPMNShape_modify">
        <omgdc:Bounds height="55.0" width="85.0" x="315.0" y="150.0"/>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="_6" id="BPMNEdge__6">
        <omgdi:waypoint x="400.0" y="77.0"/>
        <omgdi:waypoint x="505.0" y="80.5"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_7" id="BPMNEdge__7">
        <omgdi:waypoint x="545.0" y="80.5"/>
        <omgdi:waypoint x="630.0" y="80.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_9" id="BPMNEdge__9">
        <omgdi:waypoint x="50.0" y="77.0"/>
        <omgdi:waypoint x="135.0" y="77.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_10" id="BPMNEdge__10">
        <omgdi:waypoint x="220.0" y="77.0"/>
        <omgdi:waypoint x="315.0" y="77.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_12" id="BPMNEdge__12">
        <omgdi:waypoint x="525.0" y="100.5"/>
        <omgdi:waypoint x="525.0" y="177.0"/>
        <omgdi:waypoint x="400.0" y="177.0"/>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="_13" id="BPMNEdge__13">
        <omgdi:waypoint x="357.0" y="150.0"/>
        <omgdi:waypoint x="357.0" y="105.0"/>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

controller

package com.et.activiti.controller;

import com.et.activiti.service.ActivityConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
public class HelloWorldController {
    @Autowired
    ActivityConsumerService activityConsumerService;

    @RequestMapping(value="/startActivityDemo",method= RequestMethod.GET)
    public boolean startActivityDemo(){
        return activityConsumerService.startActivityDemo();
    }
}

service

package com.et.activiti.service;

import org.activiti.engine.task.TaskQuery;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
 

public interface ActivityConsumerService {

 public boolean startActivityDemo();
 
}
package com.et.activiti.service;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ActivityConsumerServiceImpl implements ActivityConsumerService {

    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    @Override
    public boolean startActivityDemo() {
        System.out.println("method startActivityDemo begin....");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("apply", "zhangsan");
        map.put("approve", "lisi");
        //flow start
        ExecutionEntity pi1 = (ExecutionEntity) runtimeService.startProcessInstanceByKey("leave", map);
        String processId = pi1.getId();
        pi1.getExecutions().forEach(row->{
            String taskId =  row.getTasks().get(0).getId();
            taskService.complete(taskId, map);//complete first step
            Task task = taskService.createTaskQuery().processInstanceId(processId).singleResult();
            String taskId2 = task.getId();
            map.put("pass", false);
            taskService.complete(taskId2, map);//refuse apply
            System.out.println("method startActivityDemo end....");
        });

        return false;
    }
}

4.测试

  • 启动Spring Boot应用
  • 访问http://127.0.0.1:8088/startActivityDemo
可能需要你先登录一下,因为activiti默认集成Spring security,默认的用户名user,密码是程序启动会自动生成一个,如下所示
Using generated security password: 86f027ef-93c2-46ee-a054-c1ada840e64d

5.引用参考

正文到此结束
Loading...