工作流系列--行为Behavior分析

Posted by 张伟真 on 2024-11-05
Estimated Reading Time 2 Minutes
Words 615 In Total
Viewed Times

工作流系列–行为Behavior分析

在agenda提供的流转类里面看到类多个behavior 今天简单说下,扒了下继承关系如下
行为分析
想要理解 Behavior 是什么东西,必须要和流程图联系起来,流程图上的一些元素比如:网关、用户任务、子流程、事件等等都有对应的行为,每种行为的处理方式都不同,例如互斥网关,究竟通过网关后怎么走,都有各自的判断行为,离开行为
例如完成任务中的TriggerExecutionOperation里面,如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
@Override
public void run() {
// 获取当前节点
FlowElement currentFlowElement = getCurrentFlowElement(execution);
if (currentFlowElement instanceof FlowNode) { // 如果是流程节点

ActivityBehavior activityBehavior = (ActivityBehavior) ((FlowNode) currentFlowElement).getBehavior();
if (activityBehavior instanceof TriggerableActivityBehavior) {
// 获取节点行为并且是可出发行为
if (currentFlowElement instanceof BoundaryEvent) {
// 如果节点是边界事件则记录开始事件
commandContext.getHistoryManager().recordActivityStart(execution);
}
// 执行对应行为(这里TriggerableActivityBehavior是个接口类,说明这里设计上留了口子,在开发过程可以实现类去达到重写操作逻辑)
((TriggerableActivityBehavior) activityBehavior).trigger(execution, null, null);

if (currentFlowElement instanceof BoundaryEvent) {
commandContext.getHistoryManager().recordActivityEnd(execution, null);
}

} else {
throw new ActivitiException("Invalid behavior: " + activityBehavior + " should implement " + TriggerableActivityBehavior.class.getName());
}

} else {
throw new ActivitiException("Programmatic error: no current flow element found or invalid type: " + currentFlowElement + ". Halting.");
}
}

TriggerExecutionOperation源码中可以看出,TriggerExecutionOperation主要用于离开节点,触发等待状态并继续该过程的操作,继而离开该活动。

如果节点行为属于可以触发的行为即节点行为类实现类以下接口

1
2
3
4
5
6
@Internal
public interface TriggerableActivityBehavior extends ActivityBehavior {

void trigger(DelegateExecution execution, String signalEvent, Object signalData);

}

则继续执行:
a. 当是边界事件,则记录历史活动开始(入库ACT_HI_ACTINST)
b. 触发行为,触发当前节点行为类的trigger (大多数trigger用于离开)
c. 当是边界事件,则:记录历史活动结束(更新结束信息(结束时间、花费时间、删除原因));
d. 历史活动实例结束事件调度;

如下,activity 实现类很多类做不同的功能,以用户任务为例
行为分析
UserTaskActivityBehavior如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public void trigger(DelegateExecution execution, String signalName, Object signalData) {
CommandContext commandContext = Context.getCommandContext();

TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
List<TaskEntity> taskEntities = taskEntityManager.findTasksByExecutionId(execution.getId()); // Should be only one
for (TaskEntity taskEntity : taskEntities) {
if (!taskEntity.isDeleted()) {
throw new ActivitiException("UserTask should not be signalled before complete");
}
}

propagateVariablesToProcess(execution,
commandContext);

leave(execution);
}

发现最后调用了leave方法,跟进去最终调用了planTakeOutgoingSequenceFlowsOperation离开节点

行为分析


如果您喜欢此博客或发现它对您有用,则欢迎对此发表评论。 也欢迎您共享此博客,以便更多人可以参与。 如果博客中使用的图像侵犯了您的版权,请与作者联系以将其删除。 谢谢 !