ACTIVITI 是一个优秀开源软件,通过阅读源码,我们不但可以了解工作流引擎执行的原理还可以增加个人的编码功力。

ACTIVITI 所有执行过程都是采用命令模式进行执行。

本文主要描述流程引擎数据保存的过程。

流程引擎所有的操作都采用命令模式,使用命令执行器进行执行,命令执行器是一个采用拦截器链式执行模式。

1.命令执行器。

代码为org.activiti.engine.impl.interceptor.CommandExecutor.

命令执行器的构造代码如下:

1.获取拦截器列表。

1.获取客户自定义前置拦截器。

这个需要实现CommandInterceptor接口,并配置到流程定义配置文件中。

2.获取默认的拦截器。

 protected Collection< ? extends CommandInterceptor> getDefaultCommandInterceptors() {
List<CommandInterceptor> interceptors = new ArrayList<CommandInterceptor>();
interceptors.add(new LogInterceptor()); CommandInterceptor transactionInterceptor = createTransactionInterceptor();
if (transactionInterceptor != null) {
interceptors.add(transactionInterceptor);
} interceptors.add(new CommandContextInterceptor(commandContextFactory, this));
return interceptors;
}

这个是获取默认的拦截器。

这里有四个拦截器。

1.LogInterceptor日志拦截器,拦截器打印执行的日志。

2.事务拦截器。

3.CommandContextInterceptor 拦截器。

这个拦截器功能如下:

1.流程定义。

2.注入命令上下文,命令上下文包括数据只有代码。

3.调用命令上下文close方法,执行数据保存。

4.命令执行拦截器,CommandInvoker 这个是拦截器最后的一个,为调用具体的命令。

3.获取后置拦截器。

这个需要实现CommandInterceptor接口,并配置到流程定义配置文件中。

2.构建拦截器链。

  基础拦截器代码:

public abstract class AbstractCommandInterceptor implements CommandInterceptor {

  /** will be initialized by the {@link org.activiti.engine.ProcessEngineConfiguration ProcessEngineConfiguration} */
protected CommandInterceptor next; @Override
public CommandInterceptor getNext() {
return next;
} @Override
public void setNext(CommandInterceptor next) {
this.next = next;
}
}

  拦截器调用setNext方法设置下一个拦截器。

 protected CommandInterceptor initInterceptorChain(List<CommandInterceptor> chain) {
if (chain==null || chain.isEmpty()) {
throw new ActivitiException("invalid command interceptor chain configuration: "+chain);
}
for (int i = 0; i < chain.size()-1; i++) {
chain.get(i).setNext( chain.get(i+1) );
}
return chain.get(0);
}

这里将拦截器列表构建成链的模式。

2.现在介绍一下命令的执行模式顺序。

比如我们设置流程变量,参考代码如下:

RuntimeService setVariable(String executionId, String variableName, Object value);

RuntimeServiceImpl 实现代码:

Map<String, Object> variables = new HashMap<String, Object>();
variables.put(variableName, value);
commandExecutor.execute(new SetExecutionVariablesCmd(executionId, variables, false));

这个commandExecutor到底是如何注入的。

我们可以看到这个RuntimeServiceImpl 扩展了类ServiceImpl。

我们在ProcessEngineConfigurationImpl 中看到如下代码:

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(commandExecutor);
}
}

这里注入了commandExecutor。

执行顺序为:

1.执行前置拦截器,如果存在。

2.日志执行。

public class LogInterceptor extends AbstractCommandInterceptor {

  private static Logger log = LoggerFactory.getLogger(LogInterceptor.class);

  public <T> T execute(CommandConfig config, Command<T> command) {
if (!log.isDebugEnabled()) {
// do nothing here if we cannot log
return next.execute(config, command);
}
log.debug(" ");
log.debug("--- starting {} --------------------------------------------------------", command.getClass().getSimpleName());
try { return next.execute(config, command); } finally {
log.debug("--- {} finished --------------------------------------------------------", command.getClass().getSimpleName());
log.debug(" ");
}
}
}

参考日志代码记录日志,调用下一个拦截器(next.execute(config, command);),最后记录日志。

3.执行事务。

3.执行CommandContext拦截器。

这个拦截器执行数据库持久化。

try {
// Push on stack
Context.setCommandContext(context);
Context.setProcessEngineConfiguration(processEngineConfiguration); return next.execute(config, command); } catch (Exception e) { context.exception(e); } finally {
try {
if (!contextReused) {
context.close();
}
} finally {
// Pop from stack
Context.removeCommandContext();
Context.removeProcessEngineConfiguration();
}
}

在命令中并不执行数据库持久化,持久化在此拦截器中调用context.close();执行。

4.执行后置拦截器,如果存在。

5.调用命令拦截器执行命令。

ACTIVITI 源码研究之命令模式执行的更多相关文章

  1. Appium Android Bootstrap源码分析之命令解析执行

    通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...

  2. activiti源码分析学习

    activiti源码分析学习导语 目前项目中用activiti来做工作流的相关工作,最近遇到一些情况下,公司二次开发的流程图渲染出现了问题,会造成流程图出不来的情况.初步分析数据库中记录以及简单的代码 ...

  3. Chrome自带恐龙小游戏的源码研究(七)

    在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...

  4. Chrome自带恐龙小游戏的源码研究(三)

    在上一篇<Chrome自带恐龙小游戏的源码研究(二)>中实现了云朵的绘制和移动,这一篇主要研究如何让游戏实现昼夜交替. 昼夜交替的效果主要是通过样式来完成,但改变样式的时机则由脚本控制. ...

  5. 阿里sentinel源码研究深入

    1. 阿里sentinel源码研究深入 1.1. 前言 昨天已经把sentinel成功部署到线上环境,可参考我上篇博文,该走的坑也都走了一遍,已经可以初步使用它的限流和降级功能,根据我目前的实践,限流 ...

  6. 从源码研究如何不重启Springboot项目实现redis配置动态切换

    上一篇Websocket的续篇暂时还没有动手写,这篇算是插播吧.今天讲讲不重启项目动态切换redis服务. 背景 多个项目或微服务场景下,各个项目都需要配置redis数据源.但是,每当运维搞事时(修改 ...

  7. java io 源码研究记录(一)

    Java IO 源码研究: 一.输入流 1  基类 InputStream 简介: 这是Java中所有输入流的基类,它是一个抽象类,下面我们简单来了解一下它的基本方法和抽象方法. 基本方法: publ ...

  8. maven 下载 源码和javadoc 命令

    摘要:我们在写代码时候,往往是想查看一下源码,看看源码的一些细节内容.一般情况下,在IDE(如eclipse)中近仅仅只需按住ctrl+ 点击对应的方法即可进入对应的源码部分.但是有些时候很多依赖项并 ...

  9. Android开源项目 Universal imageloader 源码研究之Lru算法

    https://github.com/nostra13/Android-Universal-Image-Loader universal imageloader 源码研究之Lru算法 LRU - Le ...

随机推荐

  1. PHP 全局变量 $_SERVER

    $_SERVER['SERVER_ADDR']    当前运行脚本所在的服务器的 IP 地址. $_SERVER['REQUEST_TIME']    请求开始时的时间戳.从 PHP 5.1.0 起可 ...

  2. C语言单链表实现19个功能完全详解

    谢谢Lee.Kevin分享了这篇文章 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的. 自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将 ...

  3. ubuntu的dns设置

    ubuntu的dns设置为: dns-nameservers 8.8.8.8 注意不要少s

  4. iOS - UISlider

    前言 NS_CLASS_AVAILABLE_IOS(2_0) __TVOS_PROHIBITED @interface UISlider : UIControl <NSCoding> @a ...

  5. Android应用Design Support Library完全使用实例

    阅读目录 2-1 综述 2-2 TextInputLayout控件 2-3 FloatingActionButton控件 2-4 Snackbar控件 2-5 TabLayout控件 2-6 Navi ...

  6. oracle中between

    oracle中between and包含边界值,也就是所谓的闭区间. 如 between 1 and 100,则表示包含1和100及以内的一切数值. 如以下语句: 1 2 3 4 5 6 7 8 9 ...

  7. CSS3_边框属性之圆角的基本图形案例

    一.正方形 div{ background:#F00; width:100px; height:100px;}   二.长方形 div{background:#F00;width:200px;heig ...

  8. Microsoft SQL Server

    instance / database / schema / object login / user / schema (dbo) sequence Collation PSM: Both Insta ...

  9. SVN使用报错 Synchronize operation failed. RA layer request failed svn: REPORT request on

    使用SVN同步项目时报错 RA layer request failed svn 百度了下解决方法: 删除C:\Documents and Settings\用户名\Application Data\ ...

  10. ListView 搭配SimpleAdapter

    这是SimplerAdapter的构造函数 public SimpleAdapter(Context context, List<? extends Map<String, ?>&g ...