您当前的位置: 首页 >  架构

衣舞晨风

暂无认证

  • 3浏览

    0关注

    1156博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Activiti核心架构之职责链与命令模式

衣舞晨风 发布时间:2017-06-26 07:33:54 ,浏览量:3

Activiti核心的骨架是:命令模式+职责链 本文的代码版本:activiti 5.22

一、职责链 1、什么是职责链模式

http://blog.csdn.net/jiankunking/article/details/50443294

2、Activiti中职责链初始化

     流程初始化流程引擎的时候会初始化很多信息,具体如下:

protected void init() {
    initConfigurators();
    configuratorsBeforeInit();
    initProcessDiagramGenerator();
    initHistoryLevel();
    initExpressionManager();
    initDataSource();
    initVariableTypes();
    initBeans();
    initFormEngines();
    initFormTypes();
    initScriptingEngines();
    initClock();
    initBusinessCalendarManager();
    initCommandContextFactory();
    initTransactionContextFactory();
    initCommandExecutors();
    initServices();
    initIdGenerator();
    initDeployers();
    initJobHandlers();
    initJobExecutor();
    initAsyncExecutor();
    initTransactionFactory();
    initSqlSessionFactory();
    initSessionFactories();
    initJpa();
    initDelegateInterceptor();
    initEventHandlers();
    initFailedJobCommandFactory();
    initEventDispatcher();
    initProcessValidator();
    initDatabaseEventLogging();
    configuratorsAfterInit();
  }

     由于本文主要探究:职责链与命令模式,所以这里只需要关注initCommandExecutors()即可,在该方法中会进行如下初始化:

protected void initCommandExecutors() {
    //初始化命令上下文
    initDefaultCommandConfig();
    initSchemaCommandConfig();
    //初始化CommandInvoker
    initCommandInvoker();
    //初始化命令拦截器
    //默认会添加LogInterceptor、TransactionInterceptor、CommandContextInterceptor、CommandInterceptor拦截器
    initCommandInterceptors();
    //初始化命令执行器
    //先初始化拦截器之间的指向关系,并获取到职责链的第一个元素first
    //根据CommandConfig与first来初始化CommandExecutorImpl类的实例
    initCommandExecutor();
  }

     后面三个方法具体代码如下:

protected List customPreCommandInterceptors;
protected List customPostCommandInterceptors;
protected void initCommandInvoker() {
    if (commandInvoker==null) {
      commandInvoker = new CommandInvoker();
    }
  }
protected void initCommandInterceptors() {
    if (commandInterceptors==null) {
      commandInterceptors = new ArrayList();
      if (customPreCommandInterceptors!=null) {
        commandInterceptors.addAll(customPreCommandInterceptors);
      }
      commandInterceptors.addAll(getDefaultCommandInterceptors());
      if (customPostCommandInterceptors!=null) {
        commandInterceptors.addAll(customPostCommandInterceptors);
      }
      //命令调用器,拦截器最后的一个,为调用具体的命令
      commandInterceptors.add(commandInvoker);
    }
  }
protected void initCommandExecutor() {
    if (commandExecutor==null) {
      CommandInterceptor first = initInterceptorChain(commandInterceptors);
      commandExecutor = new CommandExecutorImpl(getDefaultCommandConfig(), first);
    }
  }

     方法之间的调用关系及初始化了哪些信息如下图所示: 这里写图片描述

     最终命令拦截器链就是: customPreCommandInterceptors->LogInterceptor->TransactionInterceptor->CommandContextInterceptor->customPostCommandInterceptors->commandInvoker

注意:commandInvoker是CommandInterceptor的实例,即最后拦截的是具体命令的执行。

     在初始化过程中主要用到几个接口结构如下: 这里写图片描述

3、自定义Activiti命令拦截器

     通过上面两部分大家应该清楚了,activiti职责链:是在哪?如何初始化?这个两个问题了。      那么我们应该如何自定义一个拦截器,并指定其在职责链中的位置呢? 其实,很简单:

1、自定义拦截器
public class InterceptorTest extends AbstractCommandInterceptor {
    @Override
    public  T execute(CommandConfig config, Command command) {
        //输出字符串和命令
        System.out.println("this is InterceptorTest----->" + command.getClass().getName());
        //然后让职责链中的下一个请求处理者处理命令
        return next.execute(config, command);
    }
}
2、继承ProcessEngineConfigurationImpl根据需要重写其中的方法
public class MyProcessEngineConfiguration extends ProcessEngineConfigurationImpl {
    @Override
    protected CommandInterceptor createTransactionInterceptor() {
        // 事务拦截器 在activiti默认初始化的时候是空的
        // 此处返回自定义拦截器InterceptorTest2
        return new InterceptorTest2();
    }
    @Override
    protected Collection            
关注
打赏
1647422595
查看更多评论
0.0619s