在activiti中有几个概念经常用,但文档也没有讲的很清楚,如果不理解,有可能会误用。本文就详细探讨以下几个概念
我们在前面章节讲过,bpmn流程模型文件需要通过 RepositoryService
进行发布,相同名称的流程模型重新部署后会升一个版本,但并不影响之前的流程,还未结束的流程还是会按照发起时候的版本走。因此deployment就表示一次资源的部署,资源类型不一定是bpmn文件,可以是任意文件。上传的资源元信息保存在 ACT_RE_DEPLOYMENT
中,资源内容以二进制形式保存在表 ACT_GE_BYTEARRAY
中,两张表通过 DEPLOYMENT_ID
字段关联
一个流程每发起一次就是一个实例,实例数也是流程统计的重要数据,一个流程如果有10000个实例就表示这个流程已经被发起了10000次,流程实例在activiti中是没有实体表存储的,在流程发起时系统分配一个实例id( PROC_INST_ID_
),在整个流程的流转中,该实例号都保持不变,因此可以根据PROC_INST_ID_查询整个流程运行时的数据。当调用api发起流程时,应用程序应需要把 PROC_INST_ID_
保存到自身的业务表中,后续对流程的管理和追踪都需要PROC_INST_ID_,如要查询指定流程目前审批人信息,执行以下api:
TaskService taskService = ProcessEngines.getDefaultProcessEngine().getTaskService(); List<Task> tasks = taskService.createTaskQuery() .processInstanceId(instanceId) .list();
如果要追踪流程运行时信息,可以以PROC_INST_ID_作为过滤条件,你可以根据PROC_INST_ID_查询以下表的信息
mysql> SELECT table_name,column_name FROM information_schema.COLUMNS where COLUMN_NAME='PROC_INST_ID_'; +-----------------------+---------------+ | TABLE_NAME | COLUMN_NAME | +-----------------------+---------------+ | ACT_EVT_LOG | PROC_INST_ID_ | | ACT_FO_SUBMITTED_FORM | PROC_INST_ID_ | | ACT_HI_ACTINST | PROC_INST_ID_ | | ACT_HI_ATTACHMENT | PROC_INST_ID_ | | ACT_HI_COMMENT | PROC_INST_ID_ | | ACT_HI_DETAIL | PROC_INST_ID_ | | ACT_HI_IDENTITYLINK | PROC_INST_ID_ | | ACT_HI_PROCINST | PROC_INST_ID_ | | ACT_HI_TASKINST | PROC_INST_ID_ | | ACT_HI_VARINST | PROC_INST_ID_ | | ACT_RU_EVENT_SUBSCR | PROC_INST_ID_ | | ACT_RU_EXECUTION | PROC_INST_ID_ | | ACT_RU_IDENTITYLINK | PROC_INST_ID_ | | ACT_RU_TASK | PROC_INST_ID_ | | ACT_RU_VARIABLE | PROC_INST_ID_ | +-----------------------+---------------+ 15 rows in set (0.01 sec)
execution是比较难理解的一个概念,官方的解释是 Represent a 'path of execution' in a process instance
,代表流程实例的执行路径,有点抽象,什么意思呢,当流程没有分之,start到end就是一条直线,那么就只有一条执行路径,就是一个execution,如果流程有分之,比如平行审批,那么到平行网关就会分出两条路径,这时候就会产生两个的execution,新的execution通过 PARENT_ID_
与父exection进行关联,之所以要这么设计,我觉得是为了隔离不同执行路径间数据的隔离,比如在程序分之A上定义了一个变量,程序是不知道流程是否在分之上,在分之B上又重新定义了相同的变量,如果没有execution的概念就会导致变量被覆盖。
有一个比较特别的地方需要注意下,流程实例(ProcessInstance)也是一个execution,实例id(PROC_INST_ID_)就是流程实例在execution表上的id,从源码上也可以看出:
下面我们通过两个具体例子来理解execution
以上就是一个简单的流程,流程发起后到001节点,001审批后到002,002审批后流程结束,通过代码进行发起,保存实例id,后续通过实例id进行观察
public String start() { ProcessInstance instance = runtimeService.startProcessInstanceByKey("execution_simple"); return instance.getId(); }
发起后表信息
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_id_=77508; +-------+---------------+------------+-------------------+--------------------+---------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------+--------------------+---------+ | 77508 | 77508 | NULL | myProcess:2:77507 | 77508 | NULL | | 77509 | 77508 | 77508 | myProcess:2:77507 | 77508 | task1 | +-------+---------------+------------+-------------------+--------------------+---------+ 2 rows in set (0.01 sec) mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,NAME_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=77508; +-------+---------------+---------------+-------------------+-------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------+-----------+---------------+ | 77512 | 77509 | 77508 | myProcess:2:77507 | 001 | task1 | +-------+---------------+---------------+-------------------+-----------+---------------+ 1 row in set (0.00 sec)
我们可以得到以下几个信息
001审批后表信息
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_id_=77508; +-------+---------------+------------+-------------------+--------------------+---------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------+--------------------+---------+ | 77508 | 77508 | NULL | myProcess:2:77507 | 77508 | NULL | | 77509 | 77508 | 77508 | myProcess:2:77507 | 77508 | task2 | +-------+---------------+------------+-------------------+--------------------+---------+ 2 rows in set (0.00 sec) mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,ASSIGNEE_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=77508; +-------+---------------+---------------+-------------------+-------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------+-----------+---------------+ | 77515 | 77509 | 77508 | myProcess:2:77507 | 002 | task2 | +-------+---------------+---------------+-------------------+-------------+---------------+ 1 row in set (0.00 sec)
execution信息不变,因为当前流程没有分之。
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_id_=80011; +-------+---------------+------------+-------------------------+--------------------+---------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------------+--------------------+---------+ | 80011 | 80011 | NULL | parallelProcess:1:80009 | 80011 | NULL | | 80012 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | task1 | +-------+---------------+------------+-------------------------+--------------------+---------+ 2 rows in set (0.00 sec) mysql> mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,ASSIGNEE_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=80011; +-------+---------------+---------------+-------------------------+-----------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------------+-----------+---------------+ | 80015 | 80012 | 80011 | parallelProcess:1:80009 | 001 | task1 | +-------+---------------+---------------+-------------------------+-----------+---------------+ 1 row in set (0.00 sec)
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_idINST_ID_=80011; +-------+---------------+------------+-------------------------+--------------------+---------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------------+--------------------+---------+ | 80011 | 80011 | NULL | parallelProcess:1:80009 | 80011 | NULL | | 80012 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | task3 | | 80018 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | task2 | +-------+---------------+------------+-------------------------+--------------------+---------+ 3 rows in set (0.00 sec) mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,ASSIGNEE_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=80011; +-------+---------------+---------------+-------------------------+-----------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------------+-----------+---------------+ | 80020 | 80012 | 80011 | parallelProcess:1:80009 | 003 | task3 | | 80023 | 80018 | 80011 | parallelProcess:1:80009 | 002 | task2 | +-------+---------------+---------------+-------------------------+-----------+---------------+ 2 rows in set (0.00 sec)
可以看出80018是新创建的execution,task3复用了原来的exection 80012,两个execution的父id都是80011,也就是流程实例execution。
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_id_=80011; +-------+---------------+------------+-------------------------+--------------------+------------------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------------+--------------------+------------------+ | 80011 | 80011 | NULL | parallelProcess:1:80009 | 80011 | NULL | | 80012 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | parallelgateway2 | | 80018 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | task2 | +-------+---------------+------------+-------------------------+--------------------+------------------+ 3 rows in set (0.00 sec) mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,ASSIGNEE_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=80011; +-------+---------------+---------------+-------------------------+-----------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------------+-----------+---------------+ | 80023 | 80018 | 80011 | parallelProcess:1:80009 | 002 | task2 | +-------+---------------+---------------+-------------------------+-----------+---------------+ 1 row in set (0.00 sec)
80012停留在 parallelgateway2
上等待80018结束,没有新的execution产生
mysql> SELECT id_,PROC_INST_ID_,PARENT_ID_,PROC_DEF_ID_,ROOT_PROC_INST_ID_,ACT_ID_ FROM activiti.ACT_RU_EXECUTION where proc_inst_id_=80011; +-------+---------------+------------+-------------------------+--------------------+---------+ | id_ | PROC_INST_ID_ | PARENT_ID_ | PROC_DEF_ID_ | ROOT_PROC_INST_ID_ | ACT_ID_ | +-------+---------------+------------+-------------------------+--------------------+---------+ | 80011 | 80011 | NULL | parallelProcess:1:80009 | 80011 | NULL | | 80018 | 80011 | 80011 | parallelProcess:1:80009 | 80011 | task4 | +-------+---------------+------------+-------------------------+--------------------+---------+ 2 rows in set (0.00 sec) mysql> select ID_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,ASSIGNEE_,TASK_DEF_KEY_ from ACT_RU_TASK WHERE PROC_INST_ID_=80011; +-------+---------------+---------------+-------------------------+-----------+---------------+ | ID_ | EXECUTION_ID_ | PROC_INST_ID_ | PROC_DEF_ID_ | ASSIGNEE_ | TASK_DEF_KEY_ | +-------+---------------+---------------+-------------------------+-----------+---------------+ | 80028 | 80018 | 80011 | parallelProcess:1:80009 | 004 | task4 | +-------+---------------+---------------+-------------------------+-----------+---------------+ 1 row in set (0.00 sec)
复用了80018这个execution
004审批后流程结束。
通过以上例子可以看出
实际上不只是分之,如果一个节点多个人审批,通过 multiInstanceLoopCharacteristics
指定,那么每次循环都会创建一个新的execution,这个就留给读者自行去验证。
这里说的task指的是usertask,usertask就是一个人的待办,可以通过activiti提供的api可以查询指定人当前待办信息,当activiti遇到usertask节点就会进入等待状态,等待用户审批完成后继续执行。用户的待办任务存储在 ACT_RU_TASK
表上。