@Override publicvoidrun(){ // 获取当前元素 FlowElement currentFlowElement = getCurrentFlowElement(execution); if (currentFlowElement instanceof FlowNode) { // 当前是节点则执行continueThroughFlowNode continueThroughFlowNode((FlowNode) currentFlowElement); } elseif(currentFlowElement instanceof SequenceFlow){ // 当前如果是线则执行continueThroughSequenceFlow continueThroughSequenceFlow((SequenceFlow) currentFlowElement); } else { thrownew ActivitiException("Programmatic error: no current flow element found or invalid type: " + currentFlowElement + ". Halting."); } }
做的事情比较简单主要就是根据元素类型执行不同的操作
看方法continueThroughFlowNode
这里主要做了3件事情:
如果是起始节点,触发开始节点启动执行监听器
如果是子流程:创建子执行实例execution入库并作为子流程返回。
多实例则执行多实例同步操作;强制同步操作或者非异步(默认)则执行同步操作;否则异步操作。
第一件事情
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
protectedvoidexecuteProcessStartExecutionListeners(){ Process process = ProcessDefinitionUtil.getProcess(execution.getProcessDefinitionId()); executeExecutionListeners(process, execution.getParent(), ExecutionListener.EVENTNAME_START); }
/** * Executes the execution listeners defined on the given element, with the given event type, * and passing the provided execution to the {@link ExecutionListener} instances. */ protectedvoidexecuteExecutionListeners(HasExecutionListeners elementWithExecutionListeners, ExecutionEntity executionEntity, String eventType){ commandContext.getProcessEngineConfiguration().getListenerNotificationHelper() .executeExecutionListeners(elementWithExecutionListeners, executionEntity, eventType); }
if (executionListener != null) { if (activitiListener.getOnTransaction() != null) { planTransactionDependentExecutionListener(listenerFactory, execution, (TransactionDependentExecutionListener) executionListener, activitiListener); } else { execution.setEventName(eventType); // eventName is used to differentiate the event when reusing an execution listener for various events execution.setCurrentActivitiListener(activitiListener); ((ExecutionListener) executionListener).notify(execution); execution.setEventName(null); execution.setCurrentActivitiListener(null); } } } } } }
protected void createChildExecutionForSubProcess(SubProcess subProcess) { //找父实例,也就是isScope=true的第一个实例 ExecutionEntity parentScopeExecution = findFirstParentScopeExecution(execution); // Create the sub process execution that can be used toset variables // We create a new execution anddelete the incoming one to have a proper scope that // does notconflict anything withany existing scopes // 创建可用于设置变量的子流程, 执行我们创建一个新的执行并删除传入的执行以具有适当的范围,该范围不会与任何现有范围发生冲突 ExecutionEntity subProcessExecution = commandContext.getExecutionEntityManager().createChildExecution(parentScopeExecution); subProcessExecution.setCurrentFlowElement(subProcess); subProcessExecution.setScope(true);
// Execute any boundary events, sub process boundary events will be executed from the activity behavior // 执行任何边界事件,子流程边界事件将从活动行为中执行(只有活动(排他网关和事件)可以有边界事件) if (!inCompensation && flowNode instanceof Activity) { // Only activities can have boundary events List<BoundaryEvent> boundaryEvents = ((Activity) flowNode).getBoundaryEvents(); if (CollectionUtil.isNotEmpty(boundaryEvents)) { executeBoundaryEvents(boundaryEvents, execution); } } // Execute the multi instance behavior // 执行多实例的行为操作 ActivityBehavior activityBehavior = (ActivityBehavior) flowNode.getBehavior();
if (activityBehavior != null) { executeActivityBehavior(activityBehavior, flowNode); } else { thrownew ActivitiException("Expected an activity behavior in flow node " + flowNode.getId()); } }
同步操作默认执行executeSynchronous,执行内容和1有点像,不一样的点在d多了一步
a. 入库ACT_HI_ACTINST 记录历史节点行为实例
b. 执行start类型事件监听器
c. 执行边界事件
d. 行为不为null则执行行为类(行为里最后出线),否则执行出线操作
// Execution listener: event 'start' 执行开始事件监听器 if (CollectionUtil.isNotEmpty(flowNode.getExecutionListeners())) { executeExecutionListeners(flowNode,ExecutionListener.EVENTNAME_START); } // Execute any boundary events, sub process boundary events will be executed from the activity behavior // 和上面一样执行边界事件 if (!inCompensation && flowNode instanceof Activity) { // Only activities can have boundary events List<BoundaryEvent> boundaryEvents = ((Activity) flowNode).getBoundaryEvents(); if (CollectionUtil.isNotEmpty(boundaryEvents)) { executeBoundaryEvents(boundaryEvents,execution); } }
// Execute actual behavior ActivityBehavior activityBehavior = (ActivityBehavior) flowNode.getBehavior();
if (activityBehavior != null) { executeActivityBehavior(activityBehavior, flowNode); } else { logger.debug("No activityBehavior on activity '{}' with execution {}", flowNode.getId(), execution.getId()); Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(execution, true); } }
话不多说,上代码看看
主要执行以下操作:
a. 执行所有的线上的监听器:执行start\take\end监听器
b. 创建并发布一个线条流转事件:表明已从源活动到目标活动的sequenceflow已经执行完
c. 获取target节点设置到execution,并继续进行流转Context.getAgenda().planContinueProcessOperation(execution);//也就是当前分析的类
protected void continueThroughSequenceFlow(SequenceFlow sequenceFlow) { // Execution listener. Sequenceflow only 'take' makes sense ... but we've supported all three since the beginning // 执行所有的线上的监听器,包括开始,过程,结束. 3种状态 if (CollectionUtil.isNotEmpty(sequenceFlow.getExecutionListeners())) { executeExecutionListeners(sequenceFlow, ExecutionListener.EVENTNAME_START); executeExecutionListeners(sequenceFlow, ExecutionListener.EVENTNAME_TAKE); executeExecutionListeners(sequenceFlow, ExecutionListener.EVENTNAME_END); }
logger.debug("Sequence flow '{}' encountered. Continuing process by following it using execution {}", sequenceFlow.getId(), execution.getId()); Context.getAgenda().planContinueProcessOperation(execution); }