文章目录
亲测可用,其他方式会导致历史的任务未完成
重点:先对历史的任务end掉,再删除当前任务
//获取历史管理
HistoryManager historyManager = commandContext.getHistoryManager();
CommentEntityManager commentEntityManager = commandContext.getCommentEntityManager();
// commentEntityManager.insert();
//通知当前活动结束(更新act_hi_actinst)
historyManager.recordActivityEnd(execution,"jump to userTask1");
//通知任务节点结束(更新act_hi_taskinst)
historyManager.recordTaskEnd(taskId,"jump to userTask1");
// historyManager.createAttachmentComment();
//删除正在执行的当前任务
taskEntityManager.delete(taskId);
前言
为什么叫通用拒绝或者跳转? ,因为在activiti里,一般的拒绝或者节点跳转都是通过连接线加条件判断实现,你可以定义一个变量如status,拒绝的时候给这个变量赋值0,在连接线上设置条件表达式从而实现拒绝操作。如图:
优点:
实现简单,标准支持,灵活性强,能够从任意节点拒绝回任意节点
缺点:
一般流程每个节点都有可能拒绝,那就意味着每个节点都需要设置判断条件,如果都要拒绝回发起人,那么都要跟发起人节点进行连接,如果节点多的话会大大增加流程图的复杂度,让流程图变成一张“蜘蛛网”。
因此我们需要一个通用流程节点自有跳转功能。
原理
当我们部署一个流程并启动之后,流程就会按照流程的定义进行节点处理以及自动流转,从一个节点流向下一个节点,直至结束,并且在此过程中完成数据库中各种表的数据更新。那么在这个过程中,activiti引擎是如何进行流程的运转的呢?
ActivitiEngineAgenda
默认实现DefaultActivitiEngineAgenda
该类持有变量:
private static final Logger logger = LoggerFactory.getLogger(DefaultActivitiEngineAgenda.class);
protected LinkedList<Runnable> operations = new LinkedList();
protected CommandContext commandContext;
其中最关键的地方就是operations,operations是一个堆栈,在流程开始运转的过程中,通过ActivitiEngineAgenda的一系列实现可以将不同需要执行操作按照顺序压入栈中,然后运转过程中再将每一个操作弹出进行命令的执行。
何为操作呢?
上述operations中存放的元素的实现类为AbstractOperation的子类,AbstractOperation持有执行 实例execution,以便对当前execution进行操作。AbstractOperation的实现如下图
- 要继续运转流程,则向operations中压入ContinueProcessOperation,
- 要结束流程则压入EndExecutionOperation。
- 在每一个操作完成的时候,将接下来要执行的操作压入operations栈中,这样就达到了流程运转的效果。
CommandInvoker
该类是一个命令调用类,查看执行操作的方式:
protected void executeOperations(CommandContext commandContext) {
while(!commandContext.getAgenda().isEmpty()) {
Runnable runnable = commandContext.getAgenda().getNextOperation();
this.executeOperation(runnable);
}
}
ActivityBehavior
活动行为类,每一个节点都有行为类,主要完成关于该节点的一些操作,比如对于开始节点,开始节点一般没有什么操作,它的行为自然就是离开当前节点;当节点为userTask,则在该行为类中为userTask分配处理人等等。
- 连线是没有行为类的
这里的方式逻辑其实就是实现了上述当operations的元素不为空时,从operations堆栈中弹出并执行,直到operations中没有操作为止。
下面以该流程为例:
1、在流程启动初期,operations堆栈中压入 ContinueProcessOperation操作,传入的execution的当前节点为startEvent
2、进入CommandInvoker的executeOperations方法,取出栈顶操作元素并执行。
3、调用this.commandContext.getHistoryManager().recordActivityStart(this.execution);进行节点开始持久化,之后调用开始节点的行为类执行,执行完毕压入TaskOutgoingSequenceOperation操作类。
4、弹出TaskOutgoingSequenceOperation执行。调用this.commandContext.getHistoryManager().recordActivityEnd(this.execution, (String)null);进行节点结束的持久化,计算节点出线以及其他逻辑后,压入ContinueProcessOperation操作类,此时execution的当前节点为startEvent–>userTask的连线。
5、弹出ContinueProcessOperation执行。执行相关逻辑,获取连线的目标节点并设置为当前执行实例的当前节点,并压入ContinueProcessOperation操作类,此时执行实例的当前节点为userTask
6、弹出ContinueProcessOperation执行。获取用户任务行为类对userTask进行处理,返回,之后再没有操作类可以弹出后,判断还有可执行的执行实例,则压入ExecuteInactiveBehaviorsOperation操作类执行。
到这里流程运转结束,程序退出。
那么如果我们想要继续使流程进行运转,则要完成用户任务。
基于上述原理我们可以很容易的对activiti功能进行扩展,比如节点任意跳转。
实战任意节点跳转
那么接下来将说明如何进行任意节点之间的跳转,先来说下何为任意节点的跳转。
比如现在流程已经运转到userTask2节点,那么如果我现在想要流程回到userTask1节点怎么做呢?
实现思路
- 首先从思路上应该是处理掉当前的节点(也就是删除正在运行的该任务,更新维护历史数据),
- 然后将流程的当前节点设置到userTask1节点。
1、实现我们自己的命令类,实现execute方法
2、在实现execute逻辑中找出要跳转任务节点所在的执行实例,以及跳往的目标节点
3、通知当前的活动节点结束,并且删除,然后设置执行实例的当前节点为目标节点。
4、往operations栈中压入ContinueProcessInCompensation操作类,并且传入的执行实例的当前节点为跳转的目标节点。
相关代码实现
在部署启动流程之后,完成第一个用户任务,使得流程运转到第二个用户任务节点,此时数据库情况如下
- act_ru_execution
- act_ru_task
- act_hi_actinst
- act_hi_taskinst
自定义跳转命令类
public class JumpAnyWhereCmd implements Command {
private String taskId;
private String targetNodeId;
public JumpAnyWhereCmd(String taskId, String targetNodeId) {
this.taskId = taskId;
this.targetNodeId = targetNodeId;
}
public Object execute(CommandContext commandContext) {
//获取任务实例管理类
TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//获取当前任务实例
TaskEntity currentTask = taskEntityManager.findById(taskId);
//获取当前节点的执行实例
ExecutionEntity execution = currentTask.getExecution();
String executionId = execution.getId();
//获取流程定义id
String processDefinitionId = execution.getProcessDefinitionId();
//获取目标节点
Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
FlowElement flowElement = process.getFlowElement(targetNodeId);
//获取历史管理
HistoryManager historyManager = commandContext.getHistoryManager();
//通知当前活动结束(更新act_hi_actinst)
historyManager.recordActivityEnd(execution,"jump to userTask1");
//通知任务节点结束(更新act_hi_taskinst)
historyManager.recordTaskEnd(taskId,"jump to userTask1");
//删除正在执行的当前任务
taskEntityManager.delete(taskId);
//此时设置执行实例的当前活动节点为目标节点
execution.setCurrentFlowElement(flowElement);
//向operations中压入继续流程的操作类
commandContext.getAgenda().planContinueProcessOperation(execution);
return null;
}
}
调用命令类
@Test
public void testInvokeCommand(){
processEngine.getManagementService().executeCommand(new JumpAnyWhereCmd("5002","usertask1"));
}
此时数据库:
- act_ru_execution
- act_ru_task
- act_hi_actinst
- act_hi_taskinst
此时当前流程已经完美的从userTask2跳转到了userTask1节点。
声明:
原理参考:www.jianshu.com/p/71682b04d…
更新
有同学反馈,她在使用过程中会有如下报错:
可以看到是提示已经很清楚了act_ru_identitylink和act_ru_task表外键删除问题
即在删除act_ru_task中的任务之前,必须把关联的act_ru_identitylink数据先删除。
解决方案
在任务删除前,删除act_ru_identitylink相关联数据
代码如下:
IdentityLinkEntityManager identityLinkEntityManager = commandContext.getIdentityLinkEntityManager();
identityLinkEntityManager.deleteIdentityLinksByTaskId(taskId);
加群一起抱团取暖,共同进步
🍎QQ群【837324215】
🍎关注我的公众号【Java大厂面试官】,一起学习呗🍎🍎🍎
🍎个人vx【lakernote】