// 执行 ContinueProcessOperation#run 方法@Overridepublicvoidrun(){// 获取当前节点FlowElementcurrentFlowElement=getCurrentFlowElement(execution);if(currentFlowElementinstanceofFlowNode){// 处理节点continueThroughFlowNode((FlowNode)currentFlowElement);}elseif(currentFlowElementinstanceofSequenceFlow){// 处理连线continueThroughSequenceFlow((SequenceFlow)currentFlowElement);}else{thrownewActivitiException("Programmatic error: no current flow element found or invalid type: "+currentFlowElement+". Halting.");}}
// 处理节点protectedvoidcontinueThroughFlowNode(FlowNodeflowNode){// Check if it's the initial flow element. If so, we must fire the execution listeners for the process tooif(flowNode.getIncomingFlows()!=null&&flowNode.getIncomingFlows().size()==0&&flowNode.getSubProcess()==null){// 发布 StartExecution 事件executeProcessStartExecutionListeners();}...if(isMultiInstance(flowNode)){// the multi instance execution will look at asyncexecuteMultiInstanceSynchronous(flowNode);}elseif(forceSynchronousOperation||!flowNode.isAsynchronous()){// 同步执行,这里会等待流转节点完成, 重点分析这个,默认都是同步执行executeSynchronous(flowNode);}else{// 异步执行, 不会等待executeAsynchronous(flowNode);}}
// 同步执行,这里会等待流转节点完成protectedvoidexecuteSynchronous(FlowNodeflowNode){// 会插入到历史节点表 ACT_HI_ACTINSTcommandContext.getHistoryManager().recordActivityStart(execution);// 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 behaviorif(!inCompensation&&flowNodeinstanceofActivity){// Only activities can have boundary eventsList<BoundaryEvent>boundaryEvents=((Activity)flowNode).getBoundaryEvents();if(CollectionUtil.isNotEmpty(boundaryEvents)){// 执行 BoundaryEvent,这个很重要,会在以后的章节解析// 这里会新增一条 ACT_RU_EXECUTION 表的数据executeBoundaryEvents(boundaryEvents,execution);}}// Execute actual behavior// 获取 behavior, 在【解析流程】章节说过的ActivityBehavioractivityBehavior=(ActivityBehavior)flowNode.getBehavior();if(activityBehavior!=null){// 执行 behavior// 当前的 flowNode 是 StartEvent,所以 behavior 为 NoneStartEventActivityBehaviorexecuteActivityBehavior(activityBehavior,flowNode);}else{logger.debug("No activityBehavior on activity '{}' with execution {}",flowNode.getId(),execution.getId());// behavior 为 null,会流转到下个节点Context.getAgenda().planTakeOutgoingSequenceFlowsOperation(execution,true);}}
// TakeOutgoingSequenceFlowsOperation@Overridepublicvoidrun(){// 当前节点元素,此时是 startEventFlowElementcurrentFlowElement=getCurrentFlowElement(execution);...// When leaving the current activity, we need to delete any related execution (eg active boundary events)// 这里会清除 BoundaryEvent, 也就是删除表中的数据cleanupExecutions(currentFlowElement);if(currentFlowElementinstanceofFlowNode){// 处理节点,最终会执行 leaveFlowNode 方法handleFlowNode((FlowNode)currentFlowElement);}elseif(currentFlowElementinstanceofSequenceFlow){// 处理连线, 会执行 planContinueProcessOperationhandleSequenceFlow();}}
// leaveFlowNode, 离开节点protectedvoidleaveFlowNode(FlowNodeflowNode){...// Get default sequence flow (if set)// 计算默认的连线,当所有的条件都不满足时,会选择默认的连线StringdefaultSequenceFlowId=null;if(flowNodeinstanceofActivity){defaultSequenceFlowId=((Activity)flowNode).getDefaultFlow();}elseif(flowNodeinstanceofGateway){defaultSequenceFlowId=((Gateway)flowNode).getDefaultFlow();}// Determine which sequence flows can be used for leavingList<SequenceFlow>outgoingSequenceFlows=newArrayList<SequenceFlow>();// 计算每个连线的条件表达式for(SequenceFlowsequenceFlow:flowNode.getOutgoingFlows()){StringskipExpressionString=sequenceFlow.getSkipExpression();if(!SkipExpressionUtil.isSkipExpressionEnabled(execution,skipExpressionString)){if(!evaluateConditions||(evaluateConditions&&ConditionUtil.hasTrueCondition(sequenceFlow,execution)&&(defaultSequenceFlowId==null||!defaultSequenceFlowId.equals(sequenceFlow.getId())))){outgoingSequenceFlows.add(sequenceFlow);}}elseif(flowNode.getOutgoingFlows().size()==1||SkipExpressionUtil.shouldSkipFlowElement(commandContext,execution,skipExpressionString)){// The 'skip' for a sequence flow means that we skip the condition, not the sequence flow.outgoingSequenceFlows.add(sequenceFlow);}}...// No outgoing found. Ending the executionif(outgoingSequenceFlows.size()==0){if(flowNode.getOutgoingFlows()==null||flowNode.getOutgoingFlows().size()==0){logger.debug("No outgoing sequence flow found for flow node '{}'.",flowNode.getId());// 没有连线,直接结束当前流程Context.getAgenda().planEndExecutionOperation(execution);}else{thrownewActivitiException("No outgoing sequence flow of element '"+flowNode.getId()+"' could be selected for continuing the process");}}else{...// Executions for all the other one// 工作流框架支持多节点并行,所有这里会存在多个连线if(outgoingSequenceFlows.size()>1){for(inti=1;i<outgoingSequenceFlows.size();i++){ExecutionEntityparent=execution.getParentId()!=null?execution.getParent():execution;ExecutionEntityoutgoingExecutionEntity=commandContext.getExecutionEntityManager().createChildExecution(parent);SequenceFlowoutgoingSequenceFlow=outgoingSequenceFlows.get(i);// 设置当前流程元素为连线outgoingExecutionEntity.setCurrentFlowElement(outgoingSequenceFlow);// 每个连线都要插入到 ACT_RU_EXECUTION 表executionEntityManager.insert(outgoingExecutionEntity);outgoingExecutions.add(outgoingExecutionEntity);}}// Leave (only done when all executions have been made, since some queries depend on this)for(ExecutionEntityoutgoingExecution:outgoingExecutions){// 每一个连线,最终执行 continueThroughSequenceFlow 方法Context.getAgenda().planContinueProcessOperation(outgoingExecution);}}}
// 流转到第二个节点protectedvoidcontinueThroughSequenceFlow(SequenceFlowsequenceFlow){...// 获取目标节点,也就是第二个节点FlowElementtargetFlowElement=sequenceFlow.getTargetFlowElement();execution.setCurrentFlowElement(targetFlowElement);logger.debug("Sequence flow '{}' encountered. Continuing process by following it using execution {}",sequenceFlow.getId(),execution.getId());// 继续执行 ContinueProcessOperation,这里就回到了第一个方法调用的逻辑Context.getAgenda().planContinueProcessOperation(execution);}