01-activiti工作流

2022-07-18

01-activiti工作流

Activiti深入研究

一. 表结构详解

1.1 表概述

activiti一共25张表

  • Activiti的表都是以ACT_开头。第二部分是表示表的用途的两个字母标识。用途也和服务的API对应。
  • ACT_RE_*:'RE'表示Repository。这个前缀的表包含了流程定义和流程静态资源(图片、规则等等)。
  • ACT_RU_*:'RU'表示Runtime。这些运行时的表,包含流程实例,任务、变量,异步任务等运行中的数据。Activiti只在流程实例执行过程中保存这些数据,在流程结束时就会删除这些记录。这些运行时表可以一直很小并且速度很快。
  • ACT_HI_*:'HI'表示History。这些表包含历史数据,比如历史流程实例,变量,任务等等。
  • ACT_GE_*:'GE'表示General。通用数据,用于不同场景下。
表分类表名解释
一般数据
act_ge_bytearray通用的流程定义和流程资源
act_ge_property系统相关属性
流程历史记录act_hi_actinst历史的流程实例
act_hi_attachment历史的流程附件
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_identitylink运行时用户关系信息,存储任务节点和参与者的相关信息
act_ru_job运行时作业
act_ru_task运行时任务
act_ru_variable运行时变量表

1.2 核心字段分析

PROC_DEF_ID_:流程定义id,是ACT_RE_PROCDEF的主键,例如claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7,由3部分组成,第一部分为流程名称,第二部分是一个自增的数字,第三部分是一个唯一标

二. 类关系图

核心类为ProcessEngine,通过这个类可以访问到各种操作表的service

@Internal
public interface ProcessEngine {

  /** the version of the activiti library */
  public static String VERSION = "7.1.0-M6"; // Note the extra -x at the end. To cater for snapshot releases with different database changes

  /**
   * The name as specified in 'process-engine-name' in the activiti.cfg.xml configuration file. The default name for a process engine is 'default
   */
  String getName();

  void close();

  RepositoryService getRepositoryService();

  RuntimeService getRuntimeService();

  TaskService getTaskService();

  HistoryService getHistoryService();

  ManagementService getManagementService();

  DynamicBpmnService getDynamicBpmnService();

  ProcessEngineConfiguration getProcessEngineConfiguration();

}

ProcessEngineAutoConfiguration

在springboot中已经自动注入了ProcessEngineConfiguration,是关于流程引擎的配置类

三. 操作解析

3.1 部署

@Resource
private RepositoryService repositoryService;
public void deploy() {
  DeploymentBuilder builder = repositoryService.createDeployment();
  Deployment deploy = builder
    .name("test")
    .addClasspathResource("bpmn/test.bpmn")
    .deploy();
  System.out.println("流程部署的id=" + deploy.getId());
  System.out.println("流程部署的name=" + deploy.getName());
}

中间所相关的表

  1. ACT_RE_DEPLOYMENT

    流程定义部署表,例如这里插入了部署的名称,部署时间,版本等信息

    每部署一次,都会增加一条记录

     insert into ACT_RE_DEPLOYMENT(ID_, NAME_, CATEGORY_, KEY_, TENANT_ID_, DEPLOY_TIME_, ENGINE_VERSION_, VERSION_, PROJECT_RELEASE_VERSION_) values('36d84411-a847-11ec-8a4a-02fcbc36ad1d', 'test', null, null, '', '2022-03-20 20:13:55.792', null, 1, null);
    
  2. ACT_RE_PROCDEF

    已部署的流程定义。key是bpmn文件中定义的流程id,用来区分不同的流程,是流程的唯一标识

    insert into ACT_RE_PROCDEF(ID_, REV_, CATEGORY_, NAME_, KEY_, VERSION_, DEPLOYMENT_ID_, RESOURCE_NAME_, DGRM_RESOURCE_NAME_, DESCRIPTION_, HAS_START_FORM_KEY_, HAS_GRAPHICAL_NOTATION_ , SUSPENSION_STATE_, TENANT_ID_, ENGINE_VERSION_, APP_VERSION_)
     VALUES ('claimExpenseProcess:3:36f4f3d3-a847-11ec-8a4a-02fcbc36ad1d', 1, 'http://www.activiti.org/test', '报销审批流程', 'claimExpenseProcess', 3, '36d84411-a847-11ec-8a4a-02fcbc36ad1d', 'bpmn/test.bpmn', null, null, false, true, 1, '', null, null);
    
  3. ACT_GE_BYTEARRAY

    资源文件的二进制存储

    insert into ACT_GE_BYTEARRAY(ID_, REV_, NAME_, BYTES_, DEPLOYMENT_ID_, GENERATED_)
     VALUES ('33a96a73-a8cf-11ec-91ff-e288fc887ee7', 1, 'bpmn/test.bpmn', 'java.io.ByteArrayInputStream@7b8cb283(ByteArrayInputStream), 33a96a72-a8cf-11ec-91ff-e288fc887ee7', false, error);
    

3.2 启动流程

public void start() {
  ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("claimExpenseProcess");
  System.out.println("流程定义id=" + processInstance.getProcessDefinitionId());
  System.out.println("流程实例id=" + processInstance.getProcessInstanceId());
  System.out.println("当前活动id=" + processInstance.getActivityId());
}

通过流程key启动流程

  1. ACT_RU_EXECUTION

    运行时流程执行实例。

    insert into ACT_RU_EXECUTION (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_, IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, IS_MI_ROOT_, PARENT_ID_, SUPER_EXEC_, ROOT_PROC_INST_ID_, SUSPENSION_STATE_, TENANT_ID_, NAME_, START_TIME_, START_USER_ID_, IS_COUNT_ENABLED_, EVT_SUBSCR_COUNT_, TASK_COUNT_, JOB_COUNT_, TIMER_JOB_COUNT_, SUSP_JOB_COUNT_, DEADLETTER_JOB_COUNT_, VAR_COUNT_, ID_LINK_COUNT_, APP_VERSION_)
     VALUES ('bde1f26e-a928-11ec-bd78-de5927eace7d', 1, 'bde1f26e-a928-11ec-bd78-de5927eace7d', null, 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', null, true, false, true, false, false, null, null, 'bde1f26e-a928-11ec-bd78-de5927eace7d', 1, '', null, '2022-03-21 23:08:19.113', null, false, 0, 0, 0, 0, 0, 0, 0, 0, null) , ('bde2408f-a928-11ec-bd78-de5927eace7d', 1, 'bde1f26e-a928-11ec-bd78-de5927eace7d', null, 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', 'usertask1', true, false, false, false, false, 'bde1f26e-a928-11ec-bd78-de5927eace7d', null, 'bde1f26e-a928-11ec-bd78-de5927eace7d', 1, '', null, '2022-03-21 23:08:19.115', null, false, 0, 0, 0, 0, 0, 0, 0, 0, null);
    
  2. ACT_RU_TASK

    运行时任务表。当前正在执行的任务

    操作数据库的act_ru_execution表,如果是用户任务节点,同时也会在act_ru_task添加一条记录

    对于执行完的任务,activiti将从act_ru_task表中删除该任务,下一个任务会被插入进来。

    insert into ACT_RU_TASK (ID_, REV_, NAME_, BUSINESS_KEY_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_, CLAIM_TIME_, APP_VERSION_)
     VALUES ('bde43c62-a928-11ec-bd78-de5927eace7d', 1, '报销申请', null, null, null, 50, '2022-03-21 23:08:19.119', null, 'jellyleo', null, 'bde2408f-a928-11ec-bd78-de5927eace7d', 'bde1f26e-a928-11ec-bd78-de5927eace7d', 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', 'usertask1', null, null, 1, '', null, null, null );
    
  3. ACT_RU_IDENTITYLINK

    任务参与者数据表。主要存储当前节点参与者的信息。

     insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_)
     VALUES ('bde46373-a928-11ec-bd78-de5927eace7d', 1, 'participant', 'jellyleo', null, null, 'bde1f26e-a928-11ec-bd78-de5927eace7d', null);
    

3.3 完成任务

 select *
 FROM ACT_RU_IDENTITYLINK
 WHERE PROC_INST_ID_ = 'bde1f26e-a928-11ec-bd78-de5927eace7d';
-- ==>      Total: 1
--  ------------------------------------------------------------------------------------------------
--  371  2022-03-21 23:51:04.746 -- [main] DEBUG o.a.e.i.p.e.H.insertHistoricTaskInstance - ==>
 insert into ACT_HI_TASKINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, NAME_, PARENT_TASK_ID_, DESCRIPTION_, OWNER_, ASSIGNEE_, START_TIME_, CLAIM_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TASK_DEF_KEY_, FORM_KEY_, PRIORITY_, DUE_DATE_, CATEGORY_, TENANT_ID_ )
 VALUES ( 'b70d61a6-a92e-11ec-8e95-de5927eace7d', 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', 'bde1f26e-a928-11ec-bd78-de5927eace7d', 'bde2408f-a928-11ec-bd78-de5927eace7d', '部门经理审批', null, null, null, '赵经理', '2022-03-21 23:51:04.634', null, null, null, null, 'usertask2', null, 50, null, null, '' );
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  372  2022-03-21 23:51:04.819 -- [main] DEBUG o.a.e.i.p.e.H.insertHistoricActivityInstance - ==>
 insert into ACT_HI_ACTINST ( ID_, PROC_DEF_ID_, PROC_INST_ID_, EXECUTION_ID_, ACT_ID_, TASK_ID_, CALL_PROC_INST_ID_, ACT_NAME_, ACT_TYPE_, ASSIGNEE_, START_TIME_, END_TIME_, DURATION_, DELETE_REASON_, TENANT_ID_ )
 VALUES ( 'b70c0215-a92e-11ec-8e95-de5927eace7d', 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', 'bde1f26e-a928-11ec-bd78-de5927eace7d', 'bde2408f-a928-11ec-bd78-de5927eace7d', 'usertask2', 'b70d61a6-a92e-11ec-8e95-de5927eace7d', null, '部门经理审批', 'userTask', '赵经理', '2022-03-21 23:51:04.625', null, null, null, '' );
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  373  2022-03-21 23:51:04.893 -- [main] DEBUG o.a.e.i.p.e.H.insertHistoricIdentityLink - ==>
 insert into ACT_HI_IDENTITYLINK (ID_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_)
 VALUES ('b71e2a87-a92e-11ec-8e95-de5927eace7d', 'participant', '赵经理', null, null, 'bde1f26e-a928-11ec-bd78-de5927eace7d');
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  374  2022-03-21 23:51:05.104 -- [main] DEBUG o.a.e.i.p.entity.TaskEntityImpl.insertTask - ==>
 insert into ACT_RU_TASK (ID_, REV_, NAME_, BUSINESS_KEY_, PARENT_TASK_ID_, DESCRIPTION_, PRIORITY_, CREATE_TIME_, OWNER_, ASSIGNEE_, DELEGATION_, EXECUTION_ID_, PROC_INST_ID_, PROC_DEF_ID_, TASK_DEF_KEY_, DUE_DATE_, CATEGORY_, SUSPENSION_STATE_, TENANT_ID_, FORM_KEY_, CLAIM_TIME_, APP_VERSION_)
 VALUES ('b70d61a6-a92e-11ec-8e95-de5927eace7d', 1, '部门经理审批', null, null, null, 50, '2022-03-21 23:51:04.626', null, '赵经理', null, 'bde2408f-a928-11ec-bd78-de5927eace7d', 'bde1f26e-a928-11ec-bd78-de5927eace7d', 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', 'usertask2', null, null, 1, '', null, null, null );
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  375  2022-03-21 23:51:05.176 -- [main] DEBUG o.a.e.i.p.e.I.insertIdentityLink - ==>
 insert into ACT_RU_IDENTITYLINK (ID_, REV_, TYPE_, USER_ID_, GROUP_ID_, TASK_ID_, PROC_INST_ID_, PROC_DEF_ID_)
 VALUES ('b71e2a87-a92e-11ec-8e95-de5927eace7d', 1, 'participant', '赵经理', null, null, 'bde1f26e-a928-11ec-bd78-de5927eace7d', null);
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  376  2022-03-21 23:51:05.248 -- [main] DEBUG o.a.e.i.p.e.H.updateHistoricActivityInstance - ==>
 update ACT_HI_ACTINST set EXECUTION_ID_ = 'bde2408f-a928-11ec-bd78-de5927eace7d', ASSIGNEE_ = 'jellyleo', END_TIME_ = '2022-03-21 23:51:04.621', DURATION_ = 2565502, DELETE_REASON_ = null
 WHERE ID_ = 'bde2dcd1-a928-11ec-bd78-de5927eace7d';
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  377  2022-03-21 23:51:05.322 -- [main] DEBUG o.a.e.i.p.e.H.updateHistoricTaskInstance - ==>
 update ACT_HI_TASKINST set PROC_DEF_ID_ = 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', EXECUTION_ID_ = 'bde2408f-a928-11ec-bd78-de5927eace7d', NAME_ = '报销申请', PARENT_TASK_ID_ = null, DESCRIPTION_ = null, OWNER_ = null, ASSIGNEE_ = 'jellyleo', CLAIM_TIME_ = null, END_TIME_ = '2022-03-21 23:51:04.278', DURATION_ = 2565150, DELETE_REASON_ = null, TASK_DEF_KEY_ = 'usertask1', FORM_KEY_ = null, PRIORITY_ = 50, DUE_DATE_ = null, CATEGORY_ = null
 WHERE ID_ = 'bde43c62-a928-11ec-bd78-de5927eace7d';
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  378  2022-03-21 23:51:05.395 -- [main] DEBUG o.a.e.i.p.e.ExecutionEntityImpl.updateExecution - ==>
 update ACT_RU_EXECUTION set REV_ = 2, BUSINESS_KEY_ = null, PROC_DEF_ID_ = 'claimExpenseProcess:4:33d24f34-a8cf-11ec-91ff-e288fc887ee7', ACT_ID_ = 'usertask2', IS_ACTIVE_ = true, IS_CONCURRENT_ = false, IS_SCOPE_ = false, IS_EVENT_SCOPE_ = false, IS_MI_ROOT_ = false, PARENT_ID_ = 'bde1f26e-a928-11ec-bd78-de5927eace7d', SUPER_EXEC_ = null, ROOT_PROC_INST_ID_ = 'bde1f26e-a928-11ec-bd78-de5927eace7d', SUSPENSION_STATE_ = 1, NAME_ = null, IS_COUNT_ENABLED_ = false, EVT_SUBSCR_COUNT_ = 0, TASK_COUNT_ = 0, JOB_COUNT_ = 0, TIMER_JOB_COUNT_ = 0, SUSP_JOB_COUNT_ = 0, DEADLETTER_JOB_COUNT_ = 0, VAR_COUNT_ = 0, ID_LINK_COUNT_ = 0, APP_VERSION_ = null
 WHERE ID_ = 'bde2408f-a928-11ec-bd78-de5927eace7d' and REV_ = 1;
-- ==>    Updates: 1
--  ------------------------------------------------------------------------------------------------
--  379  2022-03-21 23:51:05.470 -- [main] DEBUG o.a.e.i.p.entity.TaskEntityImpl.deleteTask - ==>
 delete
 FROM ACT_RU_TASK
 WHERE ID_ = 'bde43c62-a928-11ec-bd78-de5927eace7d' and REV_ = 1;
-- ==>    Updates: 1

3.4 总结

按照个人理解

部署主要就是给数据库插入部署信息,主要涉及到ACT_RE_DEPLOYMENT和ACT_RE_PROCDEF这两张表,前者是部署信息,每部署一次加一条记录。

启动流程就是解析bpmn文件,从开始节点开始,依次往后启动,遇到任务节点时会停住,等待完成

完成任务,就是通过任务id指定任务完成,当完成任务后,会继续解析bpmn文件,将下一个节点的信息插入到ACT_RU_EXECUTION和ACT_RU_TASK,并且删除上一步已经完成的节点。

流程就相当于一个完整的任务,中间每一个节点是任务的一部分,依次根据bpmn往下执行,当某个节点任务完成时,会自动操作相关表删除完成任务,并插入下一步节点的内容,直到流程结束。

四. 启动分析

SpringTransactionInterceptor

CommandContextInterceptor

TransactionContextInterceptor

CommandInvoker

StartProcessInstanceCmd

image.png


标题:01-activiti工作流
作者:mahaonan
地址:https://mahaonan.fun/articles/2022/07/18/1658147025290.html