flowable源码解读——并行多实例节点任务是否是顺序生成

本文主要是介绍flowable源码解读——并行多实例节点任务是否是顺序生成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

 最近在项目开发中需要在多实例开始监听里修改一个全局的计数变量,不太确定并行多实例任务在底层引擎是顺序生成还是并行生成的,如果是顺序生成的则不影响,如果是并行生成 则修改一个全局的计数变量就会出现数据错误问题,查阅了flowable源码,做个记录。

查看并行多实例节点任务类 ParallelMultiInstanceBehavior,代码如下:
    protected int createInstances(DelegateExecution multiInstanceRootExecution) {int nrOfInstances = resolveNrOfInstances(multiInstanceRootExecution);if (nrOfInstances < 0) {throw new FlowableIllegalArgumentException("Invalid number of instances: must be non-negative integer value" + ", but was " + nrOfInstances);}setLoopVariable(multiInstanceRootExecution, NUMBER_OF_INSTANCES, nrOfInstances);setLoopVariable(multiInstanceRootExecution, NUMBER_OF_COMPLETED_INSTANCES, 0);setLoopVariable(multiInstanceRootExecution, NUMBER_OF_ACTIVE_INSTANCES, nrOfInstances);List<ExecutionEntity> concurrentExecutions = new ArrayList<>();for (int loopCounter = 0; loopCounter < nrOfInstances; loopCounter++) {ExecutionEntity concurrentExecution = CommandContextUtil.getExecutionEntityManager().createChildExecution((ExecutionEntity) multiInstanceRootExecution);concurrentExecution.setCurrentFlowElement(activity);concurrentExecution.setActive(true);concurrentExecution.setScope(false);concurrentExecutions.add(concurrentExecution);logLoopDetails(concurrentExecution, "initialized", loopCounter, 0, nrOfInstances, nrOfInstances);//CommandContextUtil.getHistoryManager().recordActivityStart(concurrentExecution);}// Before the activities are executed, all executions MUST be created up front// Do not try to merge this loop with the previous one, as it will lead// to bugs, due to possible child execution pruning.for (int loopCounter = 0; loopCounter < nrOfInstances; loopCounter++) {ExecutionEntity concurrentExecution = concurrentExecutions.get(loopCounter);// executions can be inactive, if instances are all automatics// (no-waitstate) and completionCondition has been met in the meantimeif (concurrentExecution.isActive() && !concurrentExecution.isEnded() && !concurrentExecution.getParent().isEnded()) {executeOriginalBehavior(concurrentExecution, (ExecutionEntity) multiInstanceRootExecution, loopCounter);} }// See ACT-1586: ExecutionQuery returns wrong results when using multi// instance on a receive task The parent execution must be set to false, so it wouldn't show up in// the execution query when using .activityId(something). Do not we cannot nullify the// activityId (that would have been a better solution), as it would break boundary event behavior.if (!concurrentExecutions.isEmpty()) {multiInstanceRootExecution.setActive(false);}return nrOfInstances;}

通过源码可以看出,并行多实例任务底层还是按顺序生成的,并不是并行生成。

以下是 该类的结构图:

 可以看出该类是继承 FlowNodeActivityBehavior 类,以下是FlowNodeActivityBehavior的源码:

/* Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at* *      http://www.apache.org/licenses/LICENSE-2.0* * Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/
package org.flowable.engine.impl.bpmn.behavior;import org.flowable.bpmn.model.FlowNode;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.impl.delegate.TriggerableActivityBehavior;
import org.flowable.engine.impl.persistence.entity.ExecutionEntity;/*** Superclass for all 'connectable' BPMN 2.0 process elements: tasks, gateways and events. This means that any subclass can be the source or target of a sequenceflow.* * Corresponds with the notion of the 'flownode' in BPMN 2.0.* * @author Joram Barrez*/
public abstract class FlowNodeActivityBehavior implements TriggerableActivityBehavior {private static final long serialVersionUID = 1L;protected BpmnActivityBehavior bpmnActivityBehavior = new BpmnActivityBehavior();/*** Default behaviour: just leave the activity with no extra functionality.*/@Overridepublic void execute(DelegateExecution execution) {leave(execution);}/*** Default way of leaving a BPMN 2.0 activity: evaluate the conditions on the outgoing sequence flow and take those that evaluate to true.*/public void leave(DelegateExecution execution) {bpmnActivityBehavior.performDefaultOutgoingBehavior((ExecutionEntity) execution);}public void leaveIgnoreConditions(DelegateExecution execution) {bpmnActivityBehavior.performIgnoreConditionsOutgoingBehavior((ExecutionEntity) execution);}@Overridepublic void trigger(DelegateExecution execution, String signalName, Object signalData) {// concrete activity behaviours that do accept signals should override this method;throw new FlowableException("this activity isn't waiting for a trigger");}protected String parseActivityType(FlowNode flowNode) {String elementType = flowNode.getClass().getSimpleName();elementType = elementType.substring(0, 1).toLowerCase() + elementType.substring(1);return elementType;}
}
FlowNodeActivityBehavior是流节点活动行为类,是所有“可连接”BPMN 2.0流程元素的超类:任务、网关和事件。这意味着任何子类都可以是sequenceflow的源或目标。与BPMN 2.0中的“流节点”(flownode)概念相对应。

FlowNodeActivityBehavior 的职责和作用

  1. 定义流程节点的行为:

    • FlowNodeActivityBehavior接口定义了处理流程节点的标准行为,例如开始、结束、分支等。
    • 实现这个接口的类负责处理特定类型的流程节点,比如服务任务、用户任务、网关等。
  2. 执行流程节点的逻辑:

    • 当流程执行器到达某个流程节点时,它会查找该节点的行为实现,并调用相应的方法来执行节点的逻辑。
    • 这些方法包括但不限于executeleave等,它们负责处理节点的进入、执行和离开等操作。
  3. 支持不同类型的流程节点:

    • 不同类型的流程节点会有不同的行为实现,例如用户任务和服务任务会有各自的实现类。
    • 例如,用户任务的行为实现类可能需要处理用户界面交互,而服务任务的行为实现类则可能需要调用外部系统的服务。

关键方法

FlowNodeActivityBehavior接口包含以下方法:

  • execute(Execution execution): 负责执行流程节点的主要逻辑。当流程执行器到达该节点时,会调用此方法。
  • leave(Execution execution): 负责在流程节点执行完成后离开该节点的操作。这通常涉及到更新执行状态、触发后续路径等。

这篇关于flowable源码解读——并行多实例节点任务是否是顺序生成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1098834

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

Java中JSON格式反序列化为Map且保证存取顺序一致的问题

《Java中JSON格式反序列化为Map且保证存取顺序一致的问题》:本文主要介绍Java中JSON格式反序列化为Map且保证存取顺序一致的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未... 目录背景问题解决方法总结背景做项目涉及两个微服务之间传数据时,需要提供方将Map类型的数据序列化为co

Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析

《Spring组件实例化扩展点之InstantiationAwareBeanPostProcessor使用场景解析》InstantiationAwareBeanPostProcessor是Spring... 目录一、什么是InstantiationAwareBeanPostProcessor?二、核心方法解

Nacos注册中心和配置中心的底层原理全面解读

《Nacos注册中心和配置中心的底层原理全面解读》:本文主要介绍Nacos注册中心和配置中心的底层原理的全面解读,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录临时实例和永久实例为什么 Nacos 要将服务实例分为临时实例和永久实例?1.x 版本和2.x版本的区别

Python实现自动化Word文档样式复制与内容生成

《Python实现自动化Word文档样式复制与内容生成》在办公自动化领域,高效处理Word文档的样式和内容复制是一个常见需求,本文将展示如何利用Python的python-docx库实现... 目录一、为什么需要自动化 Word 文档处理二、核心功能实现:样式与表格的深度复制1. 表格复制(含样式与内容)2

java String.join()方法实例详解

《javaString.join()方法实例详解》String.join()是Java提供的一个实用方法,用于将多个字符串按照指定的分隔符连接成一个字符串,这一方法是Java8中引入的,极大地简化了... 目录bVARxMJava String.join() 方法详解1. 方法定义2. 基本用法2.1 拼接

python如何生成指定文件大小

《python如何生成指定文件大小》:本文主要介绍python如何生成指定文件大小的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录python生成指定文件大小方法一(速度最快)方法二(中等速度)方法三(生成可读文本文件–较慢)方法四(使用内存映射高效生成

C++类和对象之默认成员函数的使用解读

《C++类和对象之默认成员函数的使用解读》:本文主要介绍C++类和对象之默认成员函数的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、默认成员函数有哪些二、各默认成员函数详解默认构造函数析构函数拷贝构造函数拷贝赋值运算符三、默认成员函数的注意事项总结一

MySQL的ALTER TABLE命令的使用解读

《MySQL的ALTERTABLE命令的使用解读》:本文主要介绍MySQL的ALTERTABLE命令的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、查看所建表的编China编程码格式2、修改表的编码格式3、修改列队数据类型4、添加列5、修改列的位置5.1、把列

Linux CPU飙升排查五步法解读

《LinuxCPU飙升排查五步法解读》:本文主要介绍LinuxCPU飙升排查五步法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录排查思路-五步法1. top命令定位应用进程pid2.php top-Hp[pid]定位应用进程对应的线程tid3. printf"%