Activiti内部实现中,各主要部件关系

对外,提供Service服务,它是无状态的。

这些Service包括:

  • protected RepositoryService repositoryService = new RepositoryServiceImpl();
  • protected RuntimeService runtimeService = new RuntimeServiceImpl();
  • protected HistoryService historyService = new HistoryServiceImpl();
  • protected IdentityService identityService = new IdentityServiceImpl();
  • protected TaskService taskService = new TaskServiceImpl();
  • protected FormService formService = new FormServiceImpl();
  • protected ManagementService managementService = new ManagementServiceImpl();

对service提供的服务,基本上每一个需要执行具体任务的方法,都有一个对应的Command实现与之对应。

  • 一般来说,一个Command对应一个完整事物;
  • 调用Command之前,都会经过CommandInterceptor,这个在初始化时就确定了,如:LogInterceptor >> CommandContextInterceptor >> CommandInvoker

如果Command不涉及节点相关内容,而是直接对数据库进行操作,则直接关联DB,入DbSqlSession的缓存;

否则,一般会通过ExecutionEntity来完成具体的操作,这里,封装了一些基本的原子操作,它们都是AtomicOperation的接口实现:

流程实例类:

  • AtomicOperationProcessStart
  • AtomicOperationProcessStartInitial
  • AtomicOperationProcessEnd

流程活动类:

  • AtomicOperationActivityStart
  • AtomicOperationActivityExecute
  • AtomicOperationActivityEnd

流程活动流转类:

  • AtomicOperationTransitionNotifyListenerEnd
  • AtomicOperationTransitionDestroyScope
  • AtomicOperationTransitionNotifyListenerTake
  • AtomicOperationTransitionCreateScope
  • AtomicOperationTransitionNotifyListenerStart

流程执行树清理类:

  • AtomicOperationDeleteCascade
  • AtomicOperationDeleteCascadeFireActivityEnd

其中,活动执行时,具体动作是由ActivityBehavior的实现类来决定的:

关于默认ID生成器

Activiti的默认生成器是DbIdGenerator,实际是一次从数据库中取一块数据,然后慢慢用,用完后再取。

/**
* @author Tom Baeyens
*/
public class DbIdGenerator implements IdGenerator { protected int idBlockSize;
protected long nextId = 0;
protected long lastId = -1; protected CommandExecutor commandExecutor;
protected CommandConfig commandConfig; public synchronized String getNextId() {
if (lastId<nextId) {
getNewBlock();
}
long _nextId = nextId++;
return Long.toString(_nextId);
} protected synchronized void getNewBlock() {
IdBlock idBlock = commandExecutor.execute(commandConfig, new GetNextIdBlockCmd(idBlockSize));
this.nextId = idBlock.getNextId();
this.lastId = idBlock.getLastId();
}
  • 在ProcessEngineConfiguration中定义的块默认大小100;

  • 在ProcessEngineComfigurationImpl中,完成初始化:
  // id generator /////////////////////////////////////////////////////////////

  protected void initIdGenerator() {
if (idGenerator==null) {
CommandExecutor idGeneratorCommandExecutor = null;
if (idGeneratorDataSource!=null) {
ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
processEngineConfiguration.setDataSource(idGeneratorDataSource);
processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
processEngineConfiguration.init();
idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
} else if (idGeneratorDataSourceJndiName!=null) {
ProcessEngineConfigurationImpl processEngineConfiguration = new StandaloneProcessEngineConfiguration();
processEngineConfiguration.setDataSourceJndiName(idGeneratorDataSourceJndiName);
processEngineConfiguration.setDatabaseSchemaUpdate(DB_SCHEMA_UPDATE_FALSE);
processEngineConfiguration.init();
idGeneratorCommandExecutor = processEngineConfiguration.getCommandExecutor();
} else {
idGeneratorCommandExecutor = getCommandExecutor();
} DbIdGenerator dbIdGenerator = new DbIdGenerator();
dbIdGenerator.setIdBlockSize(idBlockSize);
dbIdGenerator.setCommandExecutor(idGeneratorCommandExecutor);
dbIdGenerator.setCommandConfig(getDefaultCommandConfig().transactionRequiresNew());
idGenerator = dbIdGenerator;
}
}

注意:此处对getNextId()方法加了synchronize关键字,它在单机部署下,确定不会出现网上分析的什么ID重复问题。

关于task的start_time_字段取值问题

在TaskEntity中:

  /** creates and initializes a new persistent task. */
public static TaskEntity createAndInsert(ActivityExecution execution) {
TaskEntity task = create();
task.insert((ExecutionEntity) execution);
return task;
} public void insert(ExecutionEntity execution) {
CommandContext commandContext = Context.getCommandContext();
DbSqlSession dbSqlSession = commandContext.getDbSqlSession();
dbSqlSession.insert(this); if(execution != null) {
execution.addTask(this);
} commandContext.getHistoryManager().recordTaskCreated(this, execution);
} /*
* 。。。。
*/ /** Creates a new task. Embedded state and create time will be initialized.
* But this task still will have to be persisted. See {@link #insert(ExecutionEntity)}. */
public static TaskEntity create() {
TaskEntity task = new TaskEntity();
task.isIdentityLinksInitialized = true;
task.createTime = ClockUtil.getCurrentTime();
return task;
}

由此知道,活动的时间是由ClockUtil.getCurrentTime()决定的。再来看看CockUtil的源码:

/**
* @author Joram Barrez
*/
public class ClockUtil { private volatile static Date CURRENT_TIME = null; public static void setCurrentTime(Date currentTime) {
ClockUtil.CURRENT_TIME = currentTime;
} public static void reset() {
ClockUtil.CURRENT_TIME = null;
} public static Date getCurrentTime() {
if (CURRENT_TIME != null) {
return CURRENT_TIME;
}
return new Date();
} }

注意:

因为可能多线程情况,而且只有set,不会执行类似++,--这样的操作,所以这里用volatile关键字完全满足需要。

默认实现在分布式中问题

在上面介绍了DbIdGenerator以及ClockUtil之后,可以清楚明白他们的原理,那么在分布式部署中,如果还是使用这种默认的实现而不加以改善,会出现什么问题。

1.DbIdGenerator的getNextId()/getNewBlock()两个方法,在分布式主机中,synchronize不能顺利实现锁控制;

2.ClockUtil严重依赖容器所在服务器时间,但是分布式主机的时间不可能达到完全的同步;

3.在分布式主机中,对同一个任务,可以同时执行,因为他们都是DbSqlSession缓存,不会立马入库;也就是说,可能存在一个任务被同时自行两次的情况。

对1,2两点分析,基本上是确定会存在的,至于第三点,限于猜想,不知道实际是否有相关的解决策略,目前对activiti关于此处的设置猜想还没有完全弄清楚。

其实之所以那么在乎任务Id以及任务执行时间,主要是在流程跟踪图中,需要根据有序的历史任务结果集模仿重现走过的路径,而做到有序,这两个要素是非常关键的。

对分布式应用,如果同库,那ID的生成问题都会是一个问题,常见的解决方式是把他扔给数据库去解决,比如一个序列、一个类似序列的自定义函数等都是不错的选择。

当然,放到DB中以后,那么频繁访问数据库,activiti中设计的blocksize也就基本失效了,这个也算是衡量的结果吧。

实际问题分析

上述数据,是在生产上出现的问题数据,环境是为了负载均衡做了两个应用Server,同时连接一个DB;

从数据可以分析出“活动节点会在双机中运行”;

61011(A) >> 60852(B) >> 60866(B) >> 61022(A) >> 61028(A) >> 60889(B) >> 60893(B)

A机器上的61011执行完毕以后,事件如何转到B机器上的60852,这个还不明白,待解决!!

activiti主要组件解析的更多相关文章

  1. .NetCore中的日志(1)日志组件解析

    .NetCore中的日志(1)日志组件解析 0x00 问题的产生 日志记录功能在开发中很常用,可以记录程序运行的细节,也可以记录用户的行为.在之前开发时我一般都是用自己写的小工具来记录日志,输出目标包 ...

  2. Ext 常用组件解析

    Ext 常用组件解析 Panel 定义&常用属性 //1.使用initComponent Ext.define('MySecurity.view.resource.ResourcePanel' ...

  3. Ionic 常用组件解析

    Ionic 常用组件解析 $ionicModal(弹出窗口): //创建一个窗口 //此处注意目录的起始位置为app $ionicModal.fromTemplateUrl('app/security ...

  4. React Native组件(三)Text组件解析

    相关文章 React Native探索系列 React Native组件系列 前言 此前介绍了最基本的View组件,接下来就是最常用的Text组件,对于Text组件的一些常用属性,这篇文章会给出简单的 ...

  5. 2016.11.25 activiti的配置文件解析

    参考来自activiti的用户手册.   activiti的配置文件解析 1.processEngine的配置 注意,单独创建流程引擎与spring方式创建流程引擎是不一样的,区别在于:process ...

  6. React Native组件解析(二)之Text

    React Native组件解析(二)之Text 1. 概述 Text组件对应于iOS的UILabel,Android的TextView,用来显示文本.但是Text组件的内部使用的并不是flexbox ...

  7. SpringMVC组件解析

    SpringMVC组件解析 1. 前端控制器:DispatcherServlet 用户请求到达前端控制器,它就相当于 MVC 模式中的 C,DispatcherServlet 是整个流程控制的中心,由 ...

  8. Sprign-mvc系列之Spring快速入门 什么是sprign-mvc spring-mvc的作用及其基本使用+组件解析+注解解析

    Spring-mvc 什么是SpringMvc SpringMvc是一种基于java的实现Mvc设计模式的请求驱动类型的轻量级web框架,属于SpringFrameWork的后续产品,已经融合在Spr ...

  9. 跨平台的.NET邮件协议MailKit组件解析

    发起的.NET Core开源组织号召,进展的速度是我自己也没有想到的,很多园友都积极参与(虽然有些人诚心砸场子,要是以我以前的宝脾气,这会应该被我打住院了吧,不过幸好是少数,做一件事总有人说好,也有人 ...

随机推荐

  1. 什么是Asp.net Core?和 .net core有什么区别?

    为什么要写这篇文章 写这篇文章有两个原因,第一个是因为新站点创建出来后一直空置着,所以写一篇文章放在这里.第二就是因为近来在做一些基于Asp.net core平台的项目开发,也遇到了一些问题,正好趁此 ...

  2. Python进阶-配置文件

    一. 什么是配置文件?为什么要做配置文件? 将所有的代码和配置都变成模块化可配置化,这样就提高了代码的重用性,不再每次都去修改代码内部,这个就是我们逐步要做的事情,可配置化 二. 配置文件长啥样? 配 ...

  3. Delphi用户登录窗口框架

    经常看到一些新手在CSDN上问登录窗口如何写,也看到N多人form1.show/form1.create/…中做form2.show之类.实在看不下去了.这种写法实在不是很好,于是还是把自己理解的登录 ...

  4. codeforces71A

    Way Too Long Words CodeForces - 71A XUPT_ACM的杨队是一个强迫症晚期的大神,他特别反感长单词,就像 "localization" 和&qu ...

  5. Django_基于模块的单例模式

    基于模块的单例模式  原理: Python 的独有特性 : 模块的导入只能生效一次. 再重复导入只要基于一套环境都是使用的 最初 的那份资源.  示例: 文档结构: # mysingleton.py ...

  6. 【刷题】LOJ 2587 「APIO2018」铁人两项

    题目描述 比特镇的路网由 \(m\) 条双向道路连接的 \(n\) 个交叉路口组成. 最近,比特镇获得了一场铁人两项锦标赛的主办权.这场比赛共有两段赛程:选手先完成一段长跑赛程,然后骑自行车完成第二段 ...

  7. emWin notes

    emwin 学习小记 @2018-7-5 ## 在使用 graph 控件时,需要在坐标上显示波形,波形刷新函数 GRAPH_DATA_YT_AddValue() 放在一个 while(1) 中,这样便 ...

  8. 利用scrapy_redis实现分布式爬虫

    介绍 Scrapy框架不支持分布式,所以需要将一些关键代码进行修改使之支持分布式.scrapy-redis相当于一个插件,用来替换scrapy中的一些模块,使得scrapy支持分布式.github地址 ...

  9. 为什么使用消息队列,为什么使用RabbitMQ、springAMQP

    1.为什么使用消息队列? 2.为什么使用RabbbitMQ? 3.为什么使用spring AMQP?

  10. Scala进阶之路-高级数据类型之数组的使用

    Scala进阶之路-高级数据类型之数组的使用 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数组的初始化方式 1>.长度不可变数组Array 注意:顾名思义,长度不可变数 ...