啟動(dòng)流程
下面的流程是針對(duì)于springboot啟動(dòng)器的啟動(dòng)流程
// 1.springboot啟動(dòng)類中配置的啟動(dòng)配置類
ProcessEngineAutoConfiguration
// 2.自啟動(dòng)類中會(huì)向spring容器中注入ProcessEngineFactoryBean
AbstractProcessEngineAutoConfiguration.processEngine() -> ProcessEngineFactoryBean
// 3.ProcessEngineFactoryBean中會(huì)向spring容器中注入ProcessEngine
ProcessEngineFactoryBean.getObject() -> ProcessEngine
// 4.ProcessEngine具體初始化
ProcessEngineFactoryBean#processEngineConfiguration.buildProcessEngine()
// 5.上面的方法跟進(jìn)去后是下面的兩個(gè)方法
init(); // 初始化流程引擎的必須的組件
ProcessEngineImpl processEngine = new ProcessEngineImpl(this);
// 6.流程引擎啟動(dòng)完成
activiti 流程引擎組件初始化
public void init() {
initConfigurators();
configuratorsBeforeInit();
initHistoryLevel();
initExpressionManager();
if (usingRelationalDatabase) {
initDataSource();
}
// 初始化流程引擎中的議程工廠
initAgendaFactory();
initHelpers();
initVariableTypes();
initBeans();
initScriptingEngines();
initClock();
initBusinessCalendarManager();
// 初始化命令上下文工廠
initCommandContextFactory();
initTransactionContextFactory();
// 初始化命令執(zhí)行器
initCommandExecutors();
initServices();
initIdGenerator();
// 初始化行為工廠
initBehaviorFactory();
initListenerFactory();
initBpmnParser();
// 初始化流程定義緩存
initProcessDefinitionCache();
initProcessDefinitionInfoCache();
initKnowledgeBaseCache();
initJobHandlers();
initJobManager();
initAsyncExecutor();
initTransactionFactory();
if (usingRelationalDatabase) {
initSqlSessionFactory();
}
initSessionFactories();
// 初始化數(shù)據(jù)管理層, dao層
initDataManagers();
// 初始化實(shí)例管理層, 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);
}
// 初始化默認(rèn)攔截器
commandInterceptors.addAll(getDefaultCommandInterceptors());
// 初始化后置攔截器
if (customPostCommandInterceptors != null) {
commandInterceptors.addAll(customPostCommandInterceptors);
}
// 添加最后一個(gè)命令請(qǐng)求攔截器
commandInterceptors.add(commandInvoker);
}
}
// 3.所以整個(gè)攔截器鏈攔截順序是下面的樣子
customPreCommandInterceptors
LogInterceptor
SpringTransactionInterceptor
CommandContextInterceptor
TransactionContextInterceptor
customPostCommandInterceptors
CommandInvoker
執(zhí)行命令流程
activiti執(zhí)行流程除了走了攔截器鏈之外, 還會(huì)走一個(gè)議程鏈, 具體流程如下
每個(gè)命令執(zhí)行都會(huì)初始化一個(gè)CommandContext, 隨之也會(huì)初始化一個(gè)議程鏈
// 1.議程鏈的初始化在 CommandContextInterceptor 攔截器中初始化CommandContext時(shí)完成
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對(duì)象
this.agenda = processEngineConfiguration.getEngineAgendaFactory().createAgenda(this);
}
// 2.命令走到最后一個(gè)攔截器 CommandInvoker 執(zhí)行 execute 方法
// 3.議程鏈就是在這個(gè)方法中展開的, 具體的代碼如下
public <T> T execute(final CommandConfig config, final Command<T> command) {
// 獲取命令上下文
final CommandContext commandContext = Context.getCommandContext();
// 向議程中添加第一個(gè)命令
commandContext.getAgenda().planOperation(new Runnable() {
@Override
public void run() {
commandContext.setResult(command.execute(commandContext));
}
});
// 循環(huán)執(zhí)行議程方法
executeOperations(commandContext);
// 執(zhí)行被涉及到議程
if (commandContext.hasInvolvedExecutions()) {
Context.getAgenda().planExecuteInactiveBehaviorsOperation();
executeOperations(commandContext);
}
return (T) commandContext.getResult();
}
// 4.下面是executeOperations方法的實(shí)現(xiàn)
protected void executeOperations(final CommandContext commandContext) {
// 循環(huán)執(zhí)行議程, 一般是在上面執(zhí)行的命令中加入下一個(gè)執(zhí)行議程
while (!commandContext.getAgenda().isEmpty()) {
Runnable runnable = commandContext.getAgenda().getNextOperation();
executeOperation(runnable);
}
}