Activiti系列:是否可以让某些流程的信息写到历史表,而另外一些不写?

探讨了Activiti在处理大规模自动触发流程时面临的历史记录表膨胀问题,并提出了通过自定义HistoryManager及利用流程变量来控制不同流程实例历史记录的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、起因
    打算使用activiti的定时启动事件来定时启动流程,然后再在该流程中针对每个用户启动另外一个流程实例来计算每个用户的实时账单,系统的用户数一般是1000~2000(此处假设是2000),实时账单一般每小时计算一次,那么一天的数据量就是 2000x24=48000,一个月就是150w,一年就是1800w,这样的记录数对于activiti的历史表来说就有点多了,很怀疑他在这样的数据量下是否还可以跑得比较顺畅。 我想activiti设计的初衷不是用来做这种大规模的自动触发的、没有人工参与的流程的。
    所以就考虑是不是通过某些设置,可以让这种自动执行的流程不用写历史表,但是其他的需要人工参与的流程还是要能够正常记录历史记录。
    

二、源码阅读    
    然后就阅读了activiti 5.18的源码,分析如下:
    写历史表示在DefaultHistoryManager,中实现的,通过HistoryLevel来控制,相关的代码如下:

   
  1. public void recordProcessInstanceStart(ExecutionEntity processInstance) {
  2. if(isHistoryLevelAtLeast(HistoryLevel.ACTIVITY)) {
  3. HistoricProcessInstanceEntity historicProcessInstance = new HistoricProcessInstanceEntity(processInstance);
  4. // Insert historic process-instance
  5. getDbSqlSession().insert(historicProcessInstance);
  6. ....
  7. }

而isHistoryLevelAtLeast的实现如下:

   
  1. public boolean isHistoryLevelAtLeast(HistoryLevel level) {
  2. if(log.isDebugEnabled()) {
  3. log.debug("Current history level: {}, level required: {}", historyLevel, level);
  4. }
  5. // Comparing enums actually compares the location of values declared in the enum
  6. return historyLevel.isAtLeast(level);
  7. }


HistoryLevel枚举类型的是实现如下

   
  1. /**
  2. * Enum that contains all possible history-levels.
  3. *
  4. * @author Frederik Heremans
  5. */
  6. public enum HistoryLevel {
  7. NONE("none"),
  8. ACTIVITY("activity"),
  9. AUDIT("audit"),
  10. FULL("full");
  11. private String key;
  12. private HistoryLevel(String key) {
  13. this.key = key;
  14. }
  15. /**
  16. * @param key string representation of level
  17. * @return {@link HistoryLevel} for the given key
  18. * @throws ActivitiException when passed in key doesn't correspond to existing level
  19. */
  20. public static HistoryLevel getHistoryLevelForKey(String key) {
  21. for(HistoryLevel level : values()) {
  22. if(level.key.equals(key)) {
  23. return level;
  24. }
  25. }
  26. throw new ActivitiIllegalArgumentException("Illegal value for history-level: " + key);
  27. }
  28. /**
  29. * String representation of this history-level.
  30. */
  31. public String getKey() {
  32. return key;
  33. }
  34. /**
  35. * Checks if the given level is the same as, or higher in order than the
  36. * level this method is executed on.
  37. */
  38. public boolean isAtLeast(HistoryLevel level) {
  39. // Comparing enums actually compares the location of values declared in the enum
  40. return this.compareTo(level) >= 0;
  41. }
  42. }

可以看出,是否要在历史记录表中记录流程执行信息,是由 DefaultHistoryManager的level成员来决定的,只有在ACTIVITY及其以上级别才会记录。
DefaultHistoryManager是全局的,和某个具体的流程或者流程实例是没有关系的,所以在默认实现中,activiti不支持设置单个流程或者流程实例是否写历史记录表。
 
三、解决方案
如果我们真的想要实现针对每个流程或者流程实例都可以控制他是否写日志,要有两点修改:
1.要给ExecutionEntity添加一个HistoryLevel属性,或者流程变量,可能用流程变量更方便一些,就不需要对原有的类做改动了;
2.要实现一个自定义的HistoryManager,然后注入到CommandContext中。在这个自定义的HistoryManager中需要能够判断ExecutionEntity的HistoryLevel的等级,以决定是否需要记录他的历史信息;




转载于:https://www.cnblogs.com/strinkbug/p/4977012.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值