Activiti-5.2工作流引擎-源码解析(引擎初始化)

来源:互联网 发布:杨幂挑拨周迅赵薇 知乎 编辑:程序博客网 时间:2024/05/10 07:05

 

       从整体结构来看ProcessEngineConfiguration很关键,其实现类为org.activiti.engine.impl.cfg. ProcessEngineConfigurationImpl,其中其初始化的方法为:
   protected void init() {
    initHistoryLevel();
    initExpressionManager();
    initVariableTypes();
    initFormEngines();
    initFormTypes();
    initScriptingEngines();
    initBusinessCalendarManager();
    initCommandContextFactory();
    initTransactionContextFactory();
    initCommandExecutors();
    initServices();
    initIdGenerator();
    initDeployers();
    initJobExecutor();
    initDataSource();
    initTransactionFactory();
    initSqlSessionFactory();
    initSessionFactories();
    initJpa();
  }

   
上面的各个初始化语句的作用如下:

1、initHistoryLevel() :初始化HistoryLevel,主要内容如下:

  public void initHistoryLevel() {
    if (HISTORY_NONE.equalsIgnoreCase(history)) {
      historyLevel = 0;
    } else if (HISTORY_ACTIVITY.equalsIgnoreCase(history)) {
      historyLevel = 1;
    } else if (HISTORY_AUDIT.equalsIgnoreCase(history)) {
      historyLevel = 2;
    } else if (HISTORY_FULL.equalsIgnoreCase(history)) {
      historyLevel = 3;
    } else {
      throw new ActivitiException("invalid history level: "+history);
    }
  }

2、initExpressionManager() :初始化ExpressionManager,主要内容如下:

 protected void initExpressionManager() {
    if (expressionManager==null) {
      expressionManager = new ExpressionManager();
    }
  }

3、initVariableTypes() :初始化VariableTypes,主要内容如下:

protected void initVariableTypes() {
    if (variableTypes==null) {
      variableTypes = new DefaultVariableTypes();
      if (customPreVariableTypes!=null) {
        for (VariableType customVariableType: customPreVariableTypes) {
          variableTypes.addType(customVariableType);
        }
      }
      variableTypes.addType(new NullType());
      variableTypes.addType(new StringType());
      variableTypes.addType(new BooleanType());
      variableTypes.addType(new ShortType());
      variableTypes.addType(new IntegerType());
      variableTypes.addType(new LongType());
      variableTypes.addType(new DateType());
      variableTypes.addType(new DoubleType());
      variableTypes.addType(new ByteArrayType());
      variableTypes.addType(new SerializableType());
      variableTypes.addType(new CustomObjectType("item", ItemInstance.class));
      variableTypes.addType(new CustomObjectType("message", MessageInstance.class));
      if (customPostVariableTypes!=null) {
        for (VariableType customVariableType: customPostVariableTypes) {
          variableTypes.addType(customVariableType);
        }
      }
    }
  }

4、initFormEngines() :初始化FormEngines,主要内容如下:

   protected void initFormEngines() {
    if (formEngines==null) {
      formEngines = new HashMap<String, FormEngine>();
      FormEngine defaultFormEngine = new JuelFormEngine();
      formEngines.put(null, defaultFormEngine); // default form engine is looked up with null
      formEngines.put(defaultFormEngine.getName(), defaultFormEngine);
    }
    if (customFormEngines!=null) {
      for (FormEngine formEngine: customFormEngines) {
        formEngines.put(formEngine.getName(), formEngine);
      }
    }
  }

5、initFormTypes():

6、initScriptingEngines():

7、initBusinessCalendarManager():

8、initCommandContextFactory():初始化CommandContextFactory,主要内容如下:

 protected void initCommandContextFactory() {
    if (commandContextFactory==null) {
      commandContextFactory = new CommandContextFactory();
      commandContextFactory.setProcessEngineConfiguration(this);
    }
  }

9、initTransactionContextFactory():

10、initCommandExecutors():初始化CommandExecutors,主要内容如下:

 protected void initCommandExecutors() {
    initCommandInterceptorsTxRequired();
    initCommandExecutorTxRequired();
    initCommandInterceptorsTxRequiresNew();
    initCommandExecutorTxRequiresNew();
  }

       注意:从上面的四个方法的实现(源代码不在这里贴出)可以看出,“命令执行拦截器”可以增加自定义的拦截器,分别可以指定成预处理和后处理两部分,具体的变量为customPreCommandInterceptorsTxRequired、customPostCommandInterceptorsTxRequired、customPreCommandInterceptorsTxRequiresNew、customPostCommandInterceptorsTxRequiresNew。这些都是集合类型的变量,另外“命令执行拦截器”之所以要区分为“TxRequired”和“TxRequiresNew”还不是很清楚,从目前源代码中可以看出initIdGenerator()初始化的方法中采用了commandExecutorTxRequiresNew的“命令执行拦截器”的集合。拦截器的类结构图如下:

Activiti-5.2工作流引擎-源码解析(引擎初始化) - homeland520 - 好好学习,天天向上

 

11、initServices():初始化工作流引擎的各个Service,主要内容如下:

 protected void initServices() {
    initService(repositoryService);
    initService(runtimeService);
    initService(historyService);
    initService(identityService);
    initService(taskService);
    initService(formService);
    initService(managementService);
  }

protected void initService(Object service) {
    if (service instanceof ServiceImpl) {
      ((ServiceImpl)service).setCommandExecutor(commandExecutorTxRequired);
    }
  }


       注意:  此处初始化只是为各个Service设置“命令执行拦截器”,并且该“命令执行拦截器”已经在initCommandExecutors()方法中初始化过了,另外从源代码中可以看出,每个Service的“命令执行者”都是由“LogInterceptor-->CommandContextInterceptor-->CommandExecutorImpl”组成的执行链状结构。注意,其中CommandContextInterceptor方法中对数据库操作的作用较大,要多关注。

12、initIdGenerator():初始化自增长序列生成器。

13、initDeployers():初始化部署者,具体代码如下:

   protected void initDeployers() {
    if (deployers==null) {
      deployers = new ArrayList<Deployer>();
      if (customPreDeployers!=null) {
        deployers.addAll(customPreDeployers);
      }
      deployers.addAll(getDefaultDeployers());
      if (customPostDeployers!=null) {
        deployers.addAll(customPostDeployers);
      }
    }
  }
       注意:从代码中可以看到部署者分为自定义的预部署者和后部署者,另外从代码里可以按到默认的部署者是BpmnDeployer。至于自定义部署者的用途还需要继续分析。

14、initJobExecutor():初始化定时任务执行器

15、initDataSource():初始化数据源

16、initTransactionFactory():初始化事务工厂

17、initSqlSessionFactory():初始化sqlSessionFactory,主要是初始化iBatis的配置等相关信息,代码如下:

   protected void initSqlSessionFactory() {
    if (sqlSessionFactory==null) {
      InputStream inputStream = null;
      try {
        inputStream = ReflectUtil.getResourceAsStream("org/activiti/db/ibatis/activiti.ibatis.mem.conf.xml");

        // update the jdbc parameters to the configured ones...
        Environment environment = new Environment("default", transactionFactory, dataSource);
        Reader reader = new InputStreamReader(inputStream);
        XMLConfigBuilder parser = new XMLConfigBuilder(reader);
        Configuration configuration = parser.getConfiguration();
        configuration.setEnvironment(environment);
        configuration.getTypeHandlerRegistry().register(VariableType.class, JdbcType.VARCHAR, new IbatisVariableTypeHandler());
        configuration = parser.parse();

        sqlSessionFactory = new DefaultSqlSessionFactory(configuration);

      } catch (Exception e) {
        throw new ActivitiException("Error while building ibatis SqlSessionFactory: " + e.getMessage(), e);
      } finally {
        IoUtil.closeSilently(inputStream);
      }
    }
  }

   

18、initSessionFactories():初始化各个主要Service的SessionFactory

19、initJpa():初始化JPA              

原创粉丝点击