当我们部署一个流程并启动之后,流程就会按照流程的定义进行节点处理以及自动流转,从一个节点流向下一个节点,直至结束,并且在此过程中完成数据库中各种表的数据更新。那么在这个过程中,activiti引擎是如何进行流程的运转的呢?
在流程运转过程中的核心是:
-
ActivitiEngineAgenda
默认实现DefaultActivitiEngineAgenda
该类持有变量:
private static final Logger logger = LoggerFactory.getLogger(DefaultActivitiEngineAgenda.class);
protected LinkedList<Runnable> operations = new LinkedList();
protected CommandContext commandContext;
其中最关键的地方就是operations,operations是一个堆栈,在流程开始运转的过程中,通过ActivitiEngineAgenda的一系列实现可以将不同需要执行操作按照顺序压入栈中,然后运转过程中再将每一个操作弹出进行命令的执行。何为操作呢?上述operations中存放的元素的实现类为AbstractOperation的子类,AbstractOperation持有执行 实例execution,以便对当前execution进行操作。AbstractOperation的实现如下图
AbstractOperation的实现类.png
比如要继续运转流程,则向operations中压入ContinueProcessOperation,要结束流程则压入EndExecutionOperation。在每一个操作完成的时候,将接下来要执行的操作压入operations栈中,这样就达到了流程运转的效果。
-
CommandInvoker
该类是一个命令调用类,查看执行操作的方式:
protected void executeOperations(CommandContext commandContext) {
while(!commandContext.getAgenda().isEmpty()) {
Runnable runnable = commandContext.getAgenda().getNextOperation();
this.executeOperation(runnable);
}
}
-
ActivityBehavior
活动行为类,每一个节点都有行为类,主要完成关于该节点的一些操作,比如对于开始节点,开始节点一般没有什么操作,它的行为自然就是离开当前节点;当节点为userTask,则在该行为类中为userTask分配处理人等等。连线是没有行为类的
这里的方式逻辑其实就是实现了上述当operations的元素不为空时,从operations堆栈中弹出并执行,直到operations中没有操作为止。
下面以该流程为例:
流程定义.png
1、在流程启动初期,operations堆栈中压入 ContinueProcessOperation操作,传入的execution的当前节点为startEvent
2、进入CommandInvoker的executeOperations方法,取出栈顶操作元素并执行。
3、调用this.commandContext.getHistoryManager().recordActivityStart(this.execution);进行节点开始持久化,之后调用开始节点的行为类执行,执行完毕压入TaskOutgoingSequenceOperation操作类。
4、弹出TaskOutgoingSequenceOperation执行。调用this.commandContext.getHistoryManager().recordActivityEnd(this.execution, (String)null);进行节点结束的持久化,计算节点出线以及其他逻辑后,压入ContinueProcessOperation操作类,此时execution的当前节点为startEvent-->userTask的连线。
5、弹出ContinueProcessOperation执行。执行相关逻辑,获取连线的目标节点并设置为当前执行实例的当前节点,并压入ContinueProcessOperation操作类,此时执行实例的当前节点为userTask
6、弹出ContinueProcessOperation执行。获取用户任务行为类对userTask进行处理,返回,之后再没有操作类可以弹出后,判断还有可执行的执行实例,则压入ExecuteInactiveBehaviorsOperation操作类执行。
到这里流程运转结束,程序退出。
那么如果我们想要继续使流程进行运转,则要完成用户任务。
基于上述原理我们可以很容易的对activiti功能进行扩展,比如节点任意跳转。
流程示例.png
实现:
首先从思路上应该是处理掉当前的节点(也就是删除正在运行的该任务,更新维护历史数据),然后将流程的当前节点设置到userTask1节点。那么如何做呢?
1、实现我们自己的命令类,实现execute方法
2、在实现execute逻辑中找出要跳转任务节点所在的执行实例,以及跳往的目标节点
3、通知当前的活动节点结束,并且删除,然后设置执行实例的当前节点为目标节点。
4、往operations栈中压入ContinueProcessInCompensation操作类,并且传入的执行实例的当前节点为跳转的目标节点。
实战
在部署启动流程之后,完成第一个用户任务,使得流程运转到第二个用户任务节点,此时数据库情况如下
act_ru_execution.png
act_ru_task.png
act_hi_actinst.png
act_hi_taskinst.png
自定义跳转命令类
public class JumpAnyWhereCmd implements Command {
private String taskId;
private String targetNodeId;
public JumpAnyWhereCmd(String taskId, String targetNodeId) {
this.taskId = taskId;
this.targetNodeId = targetNodeId;
}
public Object execute(CommandContext commandContext) {
//获取任务实例管理类
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//获取当前任务实例
TaskEntity currentTask = taskEntityManager.findById(taskId);
//获取当前节点的执行实例
ExecutionEntity execution = currentTask.getExecution();
String executionId = execution.getId();
//获取流程定义id
String processDefinitionId = execution.getProcessDefinitionId();
//获取目标节点
Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
FlowElement flowElement = process.getFlowElement(targetNodeId);
//获取历史管理
HistoryManager historyManager = commandContext.getHistoryManager();
//通知当前活动结束(更新act_hi_actinst)
historyManager.recordActivityEnd(execution,"jump to userTask1");
//通知任务节点结束(更新act_hi_taskinst)
historyManager.recordTaskEnd(taskId,"jump to userTask1");
//删除正在执行的当前任务
taskEntityManager.delete(taskId);
//此时设置执行实例的当前活动节点为目标节点
execution.setCurrentFlowElement(flowElement);
//向operations中压入继续流程的操作类
commandContext.getAgenda().planContinueProcessOperation(execution);
return null;
}
}
调用命令类
@Test
public void testInvokeCommand(){
processEngine.getManagementService().executeCommand(new JumpAnyWhereCmd("5002","usertask1"));
}
此时数据库:
act_ru_execution.png
act_ru_task.png
act_hi_actinst.png
act_hi_taskinst.png
此时当前流程已经完美的从userTask2跳转到了userTask1节点。