A-A+
Activiti和Flowable源码解析
上一次介绍了命令和责任链模式,就是为了这一章做准备,今天就解析一下Activiti的源码,看看在它内部,命令和责任链模式是如何生效的。
因为Activiti和Flowable的相关性,所以本次解析是以Flowable6.4.2为基础,可能不同的版本细节上有所出入,但是基本原理相同。
通过之前的一些示例代码,发现Spring是通过实例化
org.flowable.spring.SpringProcessEngineConfiguration
这个类来实现流程引擎的调用的。
在源码内搜索相关的类,会发现此类继承自:
SpringProcessEngineConfiguration extends ProcessEngineConfigurationImpl
然后来看一下ProcessEngineConfigurationImpl这个类的内部是如何实现命令和责任链模式的。找到init()方法,会发现有一行调用了方法:
initCommandExecutors();
这就是关键的切入点,看一看此方法的内部实现,着重关注一下三个方法:
initCommandInvoker();
initCommandInterceptors();
initCommandExecutor();
initCommandInvoker:这个方法主要是执行命令的,之后会剖析一下。
initCommandInterceptors:这个方法是装载默认命令的方法,可以看到它装载了日志、事务等默认的命令,并且将commandInvoker装载到尾部。
initCommandExecutor:这个方法将之前initCommandInterceptors中装载的命令集合连接成责任链,集合尾部的commandInvoker将最后执行。
以之前节点跳转的命令为例,当调用
managementService.executeCommand
执行命令时,追踪代码后发现,其实就是调用了
this.commandExecutor.execute(command);
然后按照之前生成的责任链执行,最终交由CommandInvoker执行,执行时,会不停的将Agenda内的Operation执行完毕,代码如下:
// Execute the command.
// This will produce operations that will be put on the agenda.
agenda.planOperation(new Runnable() {
@Override
public void run() {
commandContext.setResult(command.execute(commandContext));
}
});
// Run loop for agenda
executeOperations(commandContext);
以上就是整个命令的初始化和执行过程,暂时剖析到这里,如果有问题请指正。接下来,会试验一下Activiti和Flowable的加签操作。
