activiti学习(二十)——流程虚拟机源码分析(二)——从开始节点离开到下个节点前

本文主要是介绍activiti学习(二十)——流程虚拟机源码分析(二)——从开始节点离开到下个节点前,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简述

《activiti学习(十九)——流程虚拟机源码分析(一)——流程启动源码分析》一文中最后,流程从startEvent节点执行takeAll准备离开。本文我们分析一下从开始节点到下一个节点之前虚拟机做了什么。由于流程虚拟机条件分支情况很多,一一叙述非常繁琐(有些我自己理解也不透彻),所以我会用最简单的模型去跟踪其流转,如下图:

我们假设起点startEvent的下一个节点是一个普通的userTask,本文跟踪其在流程虚拟机的代码走向。

 

跟踪源码

上一篇文章我们跟踪到ExecutionEntity的takeAll方法。startEvent活动类通过调用这个方法离开startEvent节点。

public class ExecutionEntity extends VariableScopeImpl implements ActivityExecution, ExecutionListenerExecution, Execution, PvmExecution, ProcessInstance, InterpretableExecution, PersistentObject, HasRevision {//......public void takeAll(List<PvmTransition> transitions, List<ActivityExecution> recyclableExecutions) {fireActivityCompletedEvent();transitions = new ArrayList<PvmTransition>(transitions);recyclableExecutions = (recyclableExecutions!=null ? new ArrayList<ActivityExecution>(recyclableExecutions) : new ArrayList<ActivityExecution>());if (recyclableExecutions.size()>1) {for (ActivityExecution recyclableExecution: recyclableExecutions) {if (((ExecutionEntity)recyclableExecution).isScope()) {throw new PvmException("joining scope executions is not allowed");}}}ExecutionEntity concurrentRoot = ((isConcurrent && !isScope) ? getParent() : this);List<ExecutionEntity> concurrentActiveExecutions = new ArrayList<ExecutionEntity>();List<ExecutionEntity> concurrentInActiveExecutions = new ArrayList<ExecutionEntity>();for (ExecutionEntity execution: concurrentRoot.getExecutions()) {if (execution.isActive()) {concurrentActiveExecutions.add(execution);} else {concurrentInActiveExecutions.add(execution);}}if ( (transitions.size()==1)&& (concurrentActiveExecutions.isEmpty())&& allExecutionsInSameActivity(concurrentInActiveExecutions)) {List<ExecutionEntity> recyclableExecutionImpls = (List) recyclableExecutions;recyclableExecutions.remove(concurrentRoot);for (ExecutionEntity prunedExecution: recyclableExecutionImpls) {Context.getCommandContext().getHistoryManager().recordActivityEnd(prunedExecution);prunedExecution.remove();}concurrentRoot.setActive(true);concurrentRoot.setActivity(activity);concurrentRoot.setConcurrent(false);concurrentRoot.take(transitions.get(0), false);} else {
//......}public void take(PvmTransition transition, boolean fireActivityCompletionEvent) {if (fireActivityCompletionEvent) {fireActivityCompletedEvent();}if (this.transition!=null) {throw new PvmException("already taking a transition");}if (transition==null) {throw new PvmException("transition is null");}setActivity((ActivityImpl)transition.getSource());setTransition((TransitionImpl) transition);performOperation(AtomicOperation.TRANSITION_NOTIFY_LISTENER_END);}//......
}

第8行fireActivityCompletedEvent方法会触发全局事件转发器转发ACTIVITY_COMPLETED事件。11行由于入参recyclableExecutions为空,所以为recyclableExecutions新建一个空的数组。22行isConcurrent默认为false,isScope默认是true,因此concurrentRoot赋值为this。32-34行判断假设startEvent的“出线”只有一条,则此时判断为true。48行通过take离开startEvent节点。66行设置当前活动为startEvent的ActivitiImpl,67行设置当前的连线,68行调用原子操作performOperation(AtomicOperation.TRANSITION_NOTIFY_LISTENER_END)。这个原子操作的调用我们上一篇文章分析过,这里不再细述。看看AtomicOperation.TRANSITION_NOTIFY_LISTENER_END对应的AtomicOperationTransitionNotifyListenerEnd类:

public class AtomicOperationTransitionNotifyListenerEnd extends AbstractEventAtomicOperation {//......protected String getEventName() {return org.activiti.engine.impl.pvm.PvmEvent.EVENTNAME_END;}protected void eventNotificationsCompleted(InterpretableExecution execution) {execution.performOperation(TRANSITION_DESTROY_SCOPE);}
}

这里原子操作首先会触发实行监听器的end事件,然后执行原子操作performOperation(TRANSITION_DESTROY_SCOPE)。即调用AtomicOperationTransitionDestroyScope类

public class AtomicOperationTransitionDestroyScope implements AtomicOperation {//......@SuppressWarnings("unchecked")public void execute(InterpretableExecution execution) {InterpretableExecution propagatingExecution = null;ActivityImpl activity = (ActivityImpl) execution.getActivity();if (activity.isScope()) {      
//......} else {propagatingExecution = execution;}ScopeImpl nextOuterScopeElement = activity.getParent();TransitionImpl transition = propagatingExecution.getTransition();ActivityImpl destination = transition.getDestination();if (transitionLeavesNextOuterScope(nextOuterScopeElement, destination)) {propagatingExecution.setActivity((ActivityImpl) nextOuterScopeElement);propagatingExecution.performOperation(TRANSITION_NOTIFY_LISTENER_END);} else {propagatingExecution.performOperation(TRANSITION_NOTIFY_LISTENER_TAKE);}}public boolean transitionLeavesNextOuterScope(ScopeImpl nextScopeElement, ActivityImpl destination) {return !nextScopeElement.contains(destination);}
}

执行execution方法,第10行的if判断为false,注意execution的isScope为true,但是ActivityImpl的isScope为false,所以这里执行13行的代码。16行获取activity.getParent(),即startEvent的parent,是Process对象。18行获取连线终点的活动。19行判断Process中是否含有下一个节点的活动,因此28行函数判断为false(!符号取了相反的值),因此走23行performOperation(TRANSITION_NOTIFY_LISTENER_TAKE),会调用AtomicOperationTransitionNotifyListenerTake类。

public class AtomicOperationTransitionNotifyListenerTake implements AtomicOperation {//......public void execute(InterpretableExecution execution) {TransitionImpl transition = execution.getTransition();List<ExecutionListener> executionListeners = transition.getExecutionListeners();int executionListenerIndex = execution.getExecutionListenerIndex();if (executionListeners.size()>executionListenerIndex) {execution.setEventName(org.activiti.engine.impl.pvm.PvmEvent.EVENTNAME_TAKE);execution.setEventSource(transition);ExecutionListener listener = executionListeners.get(executionListenerIndex);try {listener.notify(execution);} catch (RuntimeException e) {throw e;} catch (Exception e) {throw new PvmException("couldn't execute event listener : "+e.getMessage(), e);}execution.setExecutionListenerIndex(executionListenerIndex+1);execution.performOperation(this);} else {execution.setExecutionListenerIndex(0);execution.setEventName(null);execution.setEventSource(null);ActivityImpl activity = (ActivityImpl) execution.getActivity();ActivityImpl nextScope = findNextScope(activity.getParent(), transition.getDestination());execution.setActivity(nextScope);if(Context.getProcessEngineConfiguration() != null && Context.getProcessEngineConfiguration().getEventDispatcher().isEnabled()) {Context.getProcessEngineConfiguration().getEventDispatcher().dispatchEvent(ActivitiEventBuilder.createSequenceFlowTakenEvent(ActivitiEventType.SEQUENCEFLOW_TAKEN, transition.getId(),activity.getId(), (String) activity.getProperties().get("name") ,(String) activity.getProperties().get("type"), activity.getActivityBehavior().getClass().getCanonicalName(),nextScope.getId(), (String) nextScope.getProperties().get("name"), (String) nextScope.getProperties().get("type"), nextScope.getActivityBehavior().getClass().getCanonicalName()));}execution.performOperation(TRANSITION_CREATE_SCOPE);}}/** finds the next scope to enter.  the most outer scope is found first */public static ActivityImpl findNextScope(ScopeImpl outerScopeElement, ActivityImpl destination) {ActivityImpl nextScope = destination;while( (nextScope.getParent() instanceof ActivityImpl)&& (nextScope.getParent() != outerScopeElement)) {nextScope = (ActivityImpl) nextScope.getParent();}return nextScope;}
}

8-23行嵌套调用,遍历每一个执行监听器触发take事件。30-32行把execution的当前活动设置为下一个活动节点。34-38行触发全局事件转发器SEQUENCEFLOW_TAKEN事件。从此execution进入下一个节点。

 

小结

普遍来说,流程从一般的活动离开,就是通过ExecutionEntity的take方法,然后经历数个原子操作,触发执行监听器和全局事件转发器对应的事件,如果涉及子流程、网关形成的分支流程等,则情况会更复杂一些。最后把该触发的事件都触发完毕之后,把ExecutionEntity当前的活动设置为下一个节点,就此进入下一个节点相关的活动中。

这篇关于activiti学习(二十)——流程虚拟机源码分析(二)——从开始节点离开到下个节点前的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/279748

相关文章

Nginx分布式部署流程分析

《Nginx分布式部署流程分析》文章介绍Nginx在分布式部署中的反向代理和负载均衡作用,用于分发请求、减轻服务器压力及解决session共享问题,涵盖配置方法、策略及Java项目应用,并提及分布式事... 目录分布式部署NginxJava中的代理代理分为正向代理和反向代理正向代理反向代理Nginx应用场景

Redis中的有序集合zset从使用到原理分析

《Redis中的有序集合zset从使用到原理分析》Redis有序集合(zset)是字符串与分值的有序映射,通过跳跃表和哈希表结合实现高效有序性管理,适用于排行榜、延迟队列等场景,其时间复杂度低,内存占... 目录开篇:排行榜背后的秘密一、zset的基本使用1.1 常用命令1.2 Java客户端示例二、zse

Redis中的AOF原理及分析

《Redis中的AOF原理及分析》Redis的AOF通过记录所有写操作命令实现持久化,支持always/everysec/no三种同步策略,重写机制优化文件体积,与RDB结合可平衡数据安全与恢复效率... 目录开篇:从日记本到AOF一、AOF的基本执行流程1. 命令执行与记录2. AOF重写机制二、AOF的

Spring Boot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)

《SpringBoot分层架构详解之从Controller到Service再到Mapper的完整流程(用户管理系统为例)》本文将以一个实际案例(用户管理系统)为例,详细解析SpringBoot中Co... 目录引言:为什么学习Spring Boot分层架构?第一部分:Spring Boot的整体架构1.1

nodejs打包作为公共包使用的完整流程

《nodejs打包作为公共包使用的完整流程》在Node.js项目中,打包和部署是发布应用的关键步骤,:本文主要介绍nodejs打包作为公共包使用的相关资料,文中通过代码介绍的非常详细,需要的朋友可... 目录前言一、前置准备二、创建与编码三、一键构建四、本地“白嫖”测试(可选)五、发布公共包六、常见踩坑提醒

MyBatis Plus大数据量查询慢原因分析及解决

《MyBatisPlus大数据量查询慢原因分析及解决》大数据量查询慢常因全表扫描、分页不当、索引缺失、内存占用高及ORM开销,优化措施包括分页查询、流式读取、SQL优化、批处理、多数据源、结果集二次... 目录大数据量查询慢的常见原因优化方案高级方案配置调优监控与诊断总结大数据量查询慢的常见原因MyBAT

分析 Java Stream 的 peek使用实践与副作用处理方案

《分析JavaStream的peek使用实践与副作用处理方案》StreamAPI的peek操作是中间操作,用于观察元素但不终止流,其副作用风险包括线程安全、顺序混乱及性能问题,合理使用场景有限... 目录一、peek 操作的本质:有状态的中间操作二、副作用的定义与风险场景1. 并行流下的线程安全问题2. 顺

Ubuntu向多台主机批量传输文件的流程步骤

《Ubuntu向多台主机批量传输文件的流程步骤》:本文主要介绍在Ubuntu中批量传输文件到多台主机的方法,需确保主机互通、用户名密码统一及端口开放,通过安装sshpass工具,准备包含目标主机信... 目录Ubuntu 向多台主机批量传输文件1.安装 sshpass2.准备主机列表文件3.创建一个批处理脚

MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决

《MyBatis/MyBatis-Plus同事务循环调用存储过程获取主键重复问题分析及解决》MyBatis默认开启一级缓存,同一事务中循环调用查询方法时会重复使用缓存数据,导致获取的序列主键值均为1,... 目录问题原因解决办法如果是存储过程总结问题myBATis有如下代码获取序列作为主键IdMappe

一个Java的main方法在JVM中的执行流程示例详解

《一个Java的main方法在JVM中的执行流程示例详解》main方法是Java程序的入口点,程序从这里开始执行,:本文主要介绍一个Java的main方法在JVM中执行流程的相关资料,文中通过代码... 目录第一阶段:加载 (Loading)第二阶段:链接 (Linking)第三阶段:初始化 (Initia