51工具盒子

依楼听风雨
笑看云卷云舒,淡观潮起潮落

使用 Spring Boot 3.x + Flowable 实现任意节点跳转功能


本专题将深度剖析十个工作流操作模式的定义、应用场景,及其实际设定方法。这些模式包括顺序会签、并行会签、或签、票签、抄送、驳回、分配、转办、委派和代理模式。我们还将面对每个操作模式可能出现的问题提出解决方案,以及提供优化的策略和建议。目的是帮助读者全面掌握和应用工作流逻辑,解决实际问题并提升业务效率。

使用 Spring Boot 3.x + Flowable 实现任意节点跳转功能

我们在开发使用Spring Boot 3.x和Flowable进行流程控制的应用时,可能需要实现在流程图中任意节点之间跳转的功能,以支持更复杂的业务场景。在这篇文章中,我们将详细介绍如何实现这个功能。

一、环境准备:
在Spring Boot项目的pom.xml文件中添加对应的依赖:

<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>6.7.0</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

二、配置信息:
在application.properties文件中配置Flowable的自由跳转:

flowable.process.allow-process-definition-info-cache=true

三、实现自由跳转:我们来设置Controller部分,为jump方法建立一个映射:

// JumpController.java
@RestController
@RequestMapping("/flowable")
public class JumpController {
    @Autowired
    private FlowableService flowableService;

    @PutMapping("/jump/{executionId}/{targetNodeId}")
    public ResponseEntity<Void> jumpToTargetNode(@PathVariable String executionId,
        @PathVariable String targetNodeId) {
        flowableService.jump(executionId, targetNodeId);
        return ResponseEntity.ok().build();
    }
}

在这里,我们设定了一个对外的 /flowable/jump/{executionId}/{targetNodeId} API ,使用PUT方法访问,将对应的执行实例ID和目标节点ID作为参数,通过这个API可以跳转流程到指定的节点。

实现Service部分:

// FlowableService.java
@Service
public class FlowableService {

    @Autowired
    private ManagementService managementService;

    public void jump(String executionId, String targetNodeId) {
        managementService.executeCommand(new Command<ExecutionEntity>() {
            @Override
            public ExecutionEntity execute(CommandContext commandContext) {

                ExecutionEntityManager executionEntityManager = CommandContextUtil.getExecutionEntityManager(commandContext);
                ExecutionEntity execution = executionEntityManager.findById(executionId);

                // 获取我们的流程定义并找到我们的目标节点
                BpmnModel bpmnModel = commandContext.getBpmnModelInstance();
                FlowElement targetFlowElement = bpmnModel.getFlowElementById(targetNodeId);
                if (!(targetFlowElement instanceof FlowNode)) {
                    throw new RuntimeException("目标节点不是一个有效节点");
                }

                // 1.清理当前节点活动状态
                executionEntityManager.updateActivityInstanceIdInOtherExecutions(execution, null);

                // 2.创建并设置新流程节点
                execution.setCurrentFlowElement((FlowNode) targetFlowElement);

                // 3. 更新执行实例相关状态
                executionEntityManager.updateExecution(execution);

                // 4.向外执行任务
                executionEntityManager.performOperation((e, c) -> 
                    ExecutionEntityManagerImpl.getExecutionManager(c).performOperation(AtomicOperation.TRANSITION_NOTIFY_LISTENER_END, e), execution);

                return execution;
            }
        });
    }
}

对于Service我们提供jump方法实现了具体流程和节点的跳转逻辑,并且此jump方法可以被Controller调用。

这整个过程包括取出执行实例,找到执行实例所述的流程模型,根据目标节点ID找到目标节点,清理当前执行实例状态,设置执行实例当前节点为目标节点,然后更新执行实例,最后触发外部任务提醒监听。

以上,我们就实现了Spring Boot 3.x + Flowable 具备任意节点跳转的功能,包括并行分支内外节点之间的跳转。这样用户有了更多的弹性,可以对流程进行细粒度的控制,以满足更复杂的业务需求。

这样,我们就实现了在并行分支内外节点之间进行跳转的功能。需要注意的是,本文中的代码示例仅仅是一个基本实现,而实际应用中可能需要考虑很多其他因素,例如权限控制、事务管理、异常处理等。针对这些问题,您可能需要结合您的实际业务需求进一步优化和扩展这个功能。

赞(3)
未经允许不得转载:工具盒子 » 使用 Spring Boot 3.x + Flowable 实现任意节点跳转功能