ACTIVITI 源码研究之命令模式执行
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 源码研究之命令模式执行的更多相关文章
- Appium Android Bootstrap源码分析之命令解析执行
通过上一篇文章<Appium Android Bootstrap源码分析之控件AndroidElement>我们知道了Appium从pc端发送过来的命令如果是控件相关的话,最终目标控件在b ...
- activiti源码分析学习
activiti源码分析学习导语 目前项目中用activiti来做工作流的相关工作,最近遇到一些情况下,公司二次开发的流程图渲染出现了问题,会造成流程图出不来的情况.初步分析数据库中记录以及简单的代码 ...
- Chrome自带恐龙小游戏的源码研究(七)
在上一篇<Chrome自带恐龙小游戏的源码研究(六)>中研究了恐龙的跳跃过程,这一篇研究恐龙与障碍物之间的碰撞检测. 碰撞盒子 游戏中采用的是矩形(非旋转矩形)碰撞.这类碰撞优点是计算比较 ...
- Chrome自带恐龙小游戏的源码研究(三)
在上一篇<Chrome自带恐龙小游戏的源码研究(二)>中实现了云朵的绘制和移动,这一篇主要研究如何让游戏实现昼夜交替. 昼夜交替的效果主要是通过样式来完成,但改变样式的时机则由脚本控制. ...
- 阿里sentinel源码研究深入
1. 阿里sentinel源码研究深入 1.1. 前言 昨天已经把sentinel成功部署到线上环境,可参考我上篇博文,该走的坑也都走了一遍,已经可以初步使用它的限流和降级功能,根据我目前的实践,限流 ...
- 从源码研究如何不重启Springboot项目实现redis配置动态切换
上一篇Websocket的续篇暂时还没有动手写,这篇算是插播吧.今天讲讲不重启项目动态切换redis服务. 背景 多个项目或微服务场景下,各个项目都需要配置redis数据源.但是,每当运维搞事时(修改 ...
- java io 源码研究记录(一)
Java IO 源码研究: 一.输入流 1 基类 InputStream 简介: 这是Java中所有输入流的基类,它是一个抽象类,下面我们简单来了解一下它的基本方法和抽象方法. 基本方法: publ ...
- maven 下载 源码和javadoc 命令
摘要:我们在写代码时候,往往是想查看一下源码,看看源码的一些细节内容.一般情况下,在IDE(如eclipse)中近仅仅只需按住ctrl+ 点击对应的方法即可进入对应的源码部分.但是有些时候很多依赖项并 ...
- Android开源项目 Universal imageloader 源码研究之Lru算法
https://github.com/nostra13/Android-Universal-Image-Loader universal imageloader 源码研究之Lru算法 LRU - Le ...
随机推荐
- thinkphp中ajaxReturn方法实现ajax效果
前台代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- Android listview和ListAdapter搭配使用
ListView时Android中自带的数据显示控件,要使用ListView填充数据,必须要通过适配器来填充,这里给大家介绍一下ListAdapter适配器,效果图如下: java源码: packag ...
- Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数
格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...
- 转:如何学习SQL(第二部分:从关系角度理解SQL)
转自:http://blog.163.com/mig3719@126/blog/static/285720652010950825538/ 6. 从关系角度理解SQL 6.1. 关系和表 众所周知,我 ...
- Spring MVC 之请求处理方法可接收参数(三)
请求处理方法可接收参数 今天学习了前三个方法. 1.作用域对象2.单个表单提交数据3.表单数据封装的Bean对象 首先创建一个实体对象. package com.cy.springannotation ...
- C++ Primer 第三章 标准库类型vector+迭代器iterator 运算
1.vector: 标准库类型vector表示对象的集合,其中所有对象的类型都相同,集合中的每个对象都有一个与之对应的索引,索引用于访问对象.因为vector“容纳着”其他对象,所以它也常被称作容器( ...
- PV UV IP含义及区别
--------首先来看看ip.uv和pv的定义---------- PV(访问量):即Page View, 即页面浏览量或点击量,用户每次刷新即被计算一次. UV(独立访客):即Unique Vis ...
- Android相机开发那些坑
版权声明:本文由王梓原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/168 来源:腾云阁 https://www.qclou ...
- 图片流量节省大杀器:基于CDN的sharpP自适应图片技术实践
版权声明:本文由陈忱原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/156 来源:腾云阁 https://www.qclou ...
- css读书笔记1:HTML标记和文档结构
块级元素和行内元素:块级元素:上下堆叠,每个块级元素都独立占一行.块级元素的盒子宽度与父元素同宽.行内元素:左右堆叠,只有在空间不足的情况下才会折到下一行显示.行内元素的盒子会收缩包裹其内容,并尽可能 ...