启动流程
下面的流程是针对于springboot启动器的启动流程
// 1.springboot启动类中配置的启动配置类
ProcessEngineAutoConfiguration
// 2.自启动类中会向spring容器中注入ProcessEngineFactoryBean
AbstractProcessEngineAutoConfiguration.processEngine() -> ProcessEngineFactoryBean
// 3.ProcessEngineFactoryBean中会向spring容器中注入ProcessEngine
ProcessEngineFactoryBean.getObject() -> ProcessEngine
// 4.ProcessEngine具体初始化
ProcessEngineFactoryBean#processEngineConfiguration.buildProcessEngine()
// 5.上面的方法跟进去后是下面的两个方法
init(); // 初始化流程引擎的必须的组件
ProcessEngineImpl processEngine = new ProcessEngineImpl(this);
// 6.流程引擎启动完成
activiti 流程引擎组件初始化
public void init() {
initConfigurators();
configuratorsBeforeInit();
initHistoryLevel();
initExpressionManager();
if (usingRelationalDatabase) {
initDataSource();
}
// 初始化流程引擎中的议程工厂
initAgendaFactory();
initHelpers();
initVariableTypes();
initBeans();
initScriptingEngines();
initClock();
initBusinessCalendarManager();
// 初始化命令上下文工厂
initCommandContextFactory();
initTransactionContextFactory();
// 初始化命令执行器
initCommandExecutors();
initServices();
initIdGenerator();
// 初始化行为工厂
initBehaviorFactory();
initListenerFactory();
initBpmnParser();
// 初始化流程定义缓存
initProcessDefinitionCache();
initProcessDefinitionInfoCache();
initKnowledgeBaseCache();
initJobHandlers();
initJobManager();
initAsyncExecutor();
initTransactionFactory();
if (usingRelationalDatabase) {
initSqlSessionFactory();
}
initSessionFactories();
// 初始化数据管理层, dao层
initDataManagers();
// 初始化实例管理层, entity层
initEntityManagers();
// 初始化历史记录管理层
initHistoryManager();
initJpa();
initDeployers();
initDelegateInterceptor();
initEventHandlers();
initFailedJobCommandFactory();
initEventDispatcher();
initProcessValidator();
initDatabaseEventLogging();
configuratorsAfterInit();
}
拦截器链初始化
拦截器链初始化是在
initCommandExecutors()
中初始化的
// 1.跟踪initCommandExecutors()
initCommandInterceptors()
// 2.下面是拦截器链的初始化代码
public void initCommandInterceptors() {
if (commandInterceptors == null) {
commandInterceptors = new ArrayList<CommandInterceptor>();
// 初始化前置拦截器
if (customPreCommandInterceptors != null) {
commandInterceptors.addAll(customPreCommandInterceptors);
}
// 初始化默认拦截器
commandInterceptors.addAll(getDefaultCommandInterceptors());
// 初始化后置拦截器
if (customPostCommandInterceptors != null) {
commandInterceptors.addAll(customPostCommandInterceptors);
}
// 添加最后一个命令请求拦截器
commandInterceptors.add(commandInvoker);
}
}
// 3.所以整个拦截器链拦截顺序是下面的样子
customPreCommandInterceptors
LogInterceptor
SpringTransactionInterceptor
CommandContextInterceptor
TransactionContextInterceptor
customPostCommandInterceptors
CommandInvoker
执行命令流程
activiti执行流程除了走了拦截器链之外, 还会走一个议程链, 具体流程如下
每个命令执行都会初始化一个CommandContext, 随之也会初始化一个议程链
// 1.议程链的初始化在 CommandContextInterceptor 拦截器中初始化CommandContext时完成
CommandContext context = Context.getCommandContext();
if (!config.isContextReusePossible() || context == null || context.getException() != null) {
// 命令上下文为null, 初始化上下文
context = commandContextFactory.createCommandContext(command);
}
public CommandContext(Command<?> command,
ProcessEngineConfigurationImpl processEngineConfiguration) {
this.command = command;
this.processEngineConfiguration = processEngineConfiguration;
this.failedJobCommandFactory = processEngineConfiguration.getFailedJobCommandFactory();
this.sessionFactories = processEngineConfiguration.getSessionFactories();
// 初始化Agenda对象
this.agenda = processEngineConfiguration.getEngineAgendaFactory().createAgenda(this);
}
// 2.命令走到最后一个拦截器 CommandInvoker 执行 execute 方法
// 3.议程链就是在这个方法中展开的, 具体的代码如下
public <T> T execute(final CommandConfig config, final Command<T> command) {
// 获取命令上下文
final CommandContext commandContext = Context.getCommandContext();
// 向议程中添加第一个命令
commandContext.getAgenda().planOperation(new Runnable() {
@Override
public void run() {
commandContext.setResult(command.execute(commandContext));
}
});
// 循环执行议程方法
executeOperations(commandContext);
// 执行被涉及到议程
if (commandContext.hasInvolvedExecutions()) {
Context.getAgenda().planExecuteInactiveBehaviorsOperation();
executeOperations(commandContext);
}
return (T) commandContext.getResult();
}
// 4.下面是executeOperations方法的实现
protected void executeOperations(final CommandContext commandContext) {
// 循环执行议程, 一般是在上面执行的命令中加入下一个执行议程
while (!commandContext.getAgenda().isEmpty()) {
Runnable runnable = commandContext.getAgenda().getNextOperation();
executeOperation(runnable);
}
}