源碼系列-Activiti7流程啟動(dòng)和命令執(zhí)行

演示地址

前端流程編輯器

啟動(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);
    }
  }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容