1 什么是流程实例?
- 参与者(可以是用户也可以是程序)按照流程定义内容发起一个流程,这就是一个流程实例。是动态的。
- 以下是流程定义和流程实例的图解:
2 启动流程实例
- 流程定义部署在Activiti中后,就可以在系统中通过Activiti去管理该流程的执行,执行流程表示流程的一次执行。比如部署系统请假流程后,如果某个用户要申请请假这个时候就需要执行这个流程,如果另外一个用户也要申请请假则也需要执行该流程,每个执行互不影响,每个执行是单独的流程实例。
- 执行流程首先需要启动流程实例:
package com.sunxiaping;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
/**
* @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
* @version 1.0
* 2020-08-01 21:13
*/
public class ActivitiStartProcessInstanceTest {
/**
* act_hi_actinst 活动实例
* act_hi_identitylink 参与者信息
* act_hi_procinst 流程实例
* act_hi_taskinst 任务实例
* act_ru_execution 执行表
* act_ru_identitylink 参与者信息
* act_ru_task 任务表
*/
@Test
public void test() {
//创建ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
ProcessEngine processEngine = configuration.buildProcessEngine();
//获取RuntimeService对象
RuntimeService runtimeService = processEngine.getRuntimeService();
//根据流程定义的key启动流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holiday");
//获取流程实例的相关信息
String processDefinitionId = processInstance.getProcessDefinitionId();
System.out.println("流程定义的id = " + processDefinitionId);
String deploymentId = processInstance.getDeploymentId();
System.out.println("流程部署的id = " + deploymentId);
String id = processInstance.getId();
System.out.println("流程实例的id = " + id);
String activityId = processInstance.getActivityId();
System.out.println("当前活动的id = " + activityId);
}
}
3 BusinessKey(业务标识)
- 启动流程实例时,指定的businessKey,就会在act_run_execution表中存储businessKey。
- BusinessKey:业务标识,通常为业务表的主键,业务标识和流程实例一一对应。业务标识来源于业务系统。存储业务标识就是根据业务标识来关联查询业务系统的数据。比如:请假流程启动一个流程实例,就可以将请假单的id作为业务标识存储到Activiti中,将来查询Activiti的流程实例信息就可以获取请假单的id从而关联查询业务系统数据库得到请假单信息。
- 示例:
package com.sunxiaping;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
/**
* @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
* @version 1.0
* 2020-08-01 21:13
*/
public class ActivitiBusinessKeyTest {
@Test
public void test() {
String processDefinitionKey = "holiday";
String businessKey = "1";
//创建ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
ProcessEngine processEngine = configuration.buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey);
String businessKeyDb = processInstance.getBusinessKey();
System.out.println("businessKeyDb = " + businessKeyDb);
}
}
4 查询流程实例(关联businessKey)
- 需求:
- 在Activiti实际应用时,查询流程实例列表时可能要显示出业务系统的一些相关信息,比如:查询当前运行的请假流程列表需要将请假单名称、请假天数等信息显示出来,请假天数等信息在业务系统中存在,而并没有在Activiti数据库中存在,所以是无法通过Activiti的API查询到请假天数等信息。
- 实现:
5 流程实例挂起和激活
5.1 概述
- 某些情况可能由于流程变更需要将当前运行的流程暂停而不是直接删除,流程暂停后将不会继续执行。
5.2全部流程实例挂起和激活
- 操作流程定义为挂起状态,该操作定义下面的所有的流程实例将全部暂停。
- 流程定义为挂起状态,该流程定义下将不允许启动新的流程实例,同时该流程定义下的所有流程实例将全部挂起暂停执行。
- 示例:
package com.sunxiaping;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;
import java.util.List;
/**
* @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
* @version 1.0
* 2020-08-03 20:02
*/
public class ActivitiAllProcessSuspendActivateTest {
@Test
public void test() {
//获取ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
//获取ProcessEngine对象
ProcessEngine processEngine = configuration.buildProcessEngine();
//获取RepositoryService对象
RepositoryService repositoryService = processEngine.getRepositoryService();
//获取指定key的流程定义
List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().processDefinitionKey("holiday").list();
for (ProcessDefinition processDefinition : processDefinitionList) {
//如果该流程定义是暂停的
boolean suspended = processDefinition.isSuspended();
if (suspended) {
//如果该流程定义是暂停的,则激活该流程定义下的所有流程实例
//第一个参数,表示流程定义的id
//第二个参数,表示是否级联激活该流程定义的流程实例
//第三个参数,表示激活这个流程定义的时间,如果不填写,从此刻开始
repositoryService.activateProcessDefinitionById(processDefinition.getId(), true, null);
System.out.println("流程定义" + processDefinition.getId() + "激活");
} else {
//如果该流程定义是激活的,则挂起该流程定义
repositoryService.suspendProcessDefinitionById(processDefinition.getId(), true, null);
System.out.println("流程定义" + processDefinition.getId() + "挂起");
}
}
}
}
5.3 单个流程实例挂起和激活
- 操作流程实例对象,针对单个流程执行挂起操作,某个流程实例挂起则此流程不再执行,完成该流程实例的当前任务将报异常。
- 示例:
package com.sunxiaping;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import java.util.List;
/**
* @author <a href="mailto:1900919313@qq.com">weiwei.xu</a>
* @version 1.0
* 2020-08-03 20:02
*/
public class ActivitiSingleProcessSuspendActivateTest {
@Test
public void test() {
String processInstanceId = "2501";
//获取ProcessEngineConfiguration对象
ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
//获取ProcessEngine对象
ProcessEngine processEngine = configuration.buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
List<ProcessInstance> processInstanceList = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).list();
for (ProcessInstance processInstance : processInstanceList) {
boolean suspended = processInstance.isSuspended();
if (suspended) {
runtimeService.activateProcessInstanceById(processInstance.getProcessInstanceId());
System.out.println("流程实例" + processInstance.getProcessDefinitionId() + "激活");
} else {
runtimeService.suspendProcessInstanceById(processInstance.getProcessInstanceId());
System.out.println("流程实例" + processInstance.getProcessDefinitionId() + "挂起");
}
}
}
}