【弄nèng - Activiti6】Activiti6入门篇(二十一)—— 事务子流程

部分简介摘抄自官方文档
参考杨大仙的程序空间https://my.oschina.net/JavaLaw

*后台服务基于Springboot2 + Activiti6,整合文章请参考:https://blog.csdn.net/yy756127197/article/details/101211510 不需要流程设计器就排除3,4步骤 *

1. 子流程

1.1 简介

子流程是一种特殊的流程,它包含在大流程中的一部分。
分为嵌入式子流程,调用式子流程,事件子流程,事务子流程,特别子流程(Activiti6新添加的)。

事件子流程请参考错误开始事件事例5

子流程请参考错误结束事件事例2

2. 事务子流程

2.1 简介

事务子流程是内嵌子流程,可以用来把多个流程放到一个事务里。 事务是一个逻辑单元,可以把一些单独的节点放在一起,这样它们就可以一起成功或一起失败。

事务可以有三种可能的结果:

  • 事务成功,如果没有取消也没有因为问题终结。如果事务子流程是成功的, 就会使用外出顺序流继续执行。 如果流程后来抛出了一个补偿事件,成功的事务可能被补偿。
    注意:和普通内嵌子流程一样,事务可能在成功后, 使用中间补偿事件进行补偿。
  • 事务取消,如果流程到达取消结束事件。这时, 所有流程都会终结和删除。触发补偿的一个单独的流程,会通过取消边界事件继续执行。 在补偿完成之后,事务子流程会使用取消边界事务的外出顺序流向下执行。
  • 事务被问题结束,如果跑出了一个错误事件, 而且没有在事务子流程中捕获。(如果错误被事务子流程的边界事件处理了,也会这样应用。) 这时,不会执行补偿。

下面的图形演示了三种不同的结果:

图原官方
与ACID事务的关系:一定不要吧bpmn事务子流程与技术(ACID)事务相混淆。 bpmn事务子流程不是技术事务领域的东西。要理解activiti中的事务管理,请参考 并发与事务。 bpmn事务和技术事务有以下不同点:

  • ACID事务一般是短期的,bpmn事务可能持续几小时,几天,甚至几个月才能完成。 (考虑事务中包含的节点可能有用户任务,一般人员响应的时间比应用时间要长。或者, 或者,在其他情况下,bpmn事务可能要等待发生一些事务事件,就像要根据某种次序执行。) 这种操作通常要相比更新数据库的一条数据,或把一条信息保存到事务性队列中,消耗更长的时间来完成。

  • 因为不能在整个业务节点的过程中保持一个技术性的事务,所以bpmn事务一般要跨越多个ACID事务。

  • 因为bpmn事务会跨越多个ACID事务,所以会丧失ACID的特性。比如,考虑上述例子。 假设“约定旅店”和“刷信用卡”操作在单独的ACID事务中执行。 也假设“预定旅店”节点已经成功了。现在我们处于一个中间不稳定状态,因为我们预定了酒店,但是还没有刷信用卡。 现在,在一个ACID事务中,我们要依次执行不同的操作,也会有一个中间不稳定状态。 不同的是,这个中间状态对事务的外部是可见的。 比如,如果通过外部预定服务进行了预定,其他使用相同预定服务的部分就可以看到旅店被预定了。 这意味着实现业务事务时,我们完全失去了隔离属性 (注:我们也经常放弃隔离性,来为ACID事务获得更高的并发,但是我们可以完全控制,中间不稳定状态也只持续很短的时间)。

  • bpmn业务事务也不能使用通常的方式回滚。因为它跨越了多个事务,bpmn事务取消时一些ACID事务可能已经提交了。 这时,它们不能被回滚了。

2.2 流程设计

设置子流程
在这里插入图片描述
在普通子流程中勾选该项就是事务子流程。

流程图
在这里插入图片描述

流程文件bpmn

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/processdef">
  <process id="transactionProcess" name="transactionProcess" isExecutable="true">
    <documentation>transactionProcess</documentation>
    <startEvent id="startEvent1"></startEvent>
    <transaction id="sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B" name="事务子流程">
      <startEvent id="sid-B3CEB01C-5C91-433A-B878-2E6DDAE69ABA"></startEvent>
      <serviceTask id="sid-3FD22AFB-603B-4381-BD0D-1521C813A603" name="提交订单" activiti:class="com.it.cloud.activiti.structural.delegate.SubmitDelegate"></serviceTask>
      <serviceTask id="sid-68971120-0526-43BF-AB3C-A12E63628B68" name="支付" activiti:class="com.it.cloud.activiti.structural.delegate.PayDelegate"></serviceTask>
      <endEvent id="sid-73A91D58-6868-4FD9-91C9-EF25428FE200"></endEvent>
      <endEvent id="sid-0F8C3EEA-AAFD-4245-A993-B94BC8A95665">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <serviceTask id="sid-B60928BB-CE96-4CA2-A40E-B6F9818F05E0" name="取消订单" isForCompensation="true" activiti:class="com.it.cloud.activiti.structural.delegate.CancelDelegate"></serviceTask>
      <boundaryEvent id="sid-1C06CDE3-36D0-4AEA-BBD7-3332E3619406" attachedToRef="sid-3FD22AFB-603B-4381-BD0D-1521C813A603" cancelActivity="false">
        <compensateEventDefinition></compensateEventDefinition>
      </boundaryEvent>
      <boundaryEvent id="sid-FF993176-607D-4B14-A2B7-0E2C2F475A72" attachedToRef="sid-68971120-0526-43BF-AB3C-A12E63628B68">
        <errorEventDefinition errorRef="error"></errorEventDefinition>
      </boundaryEvent>
      <sequenceFlow id="sid-5AB19127-201A-4304-BBEB-668F66BEAE3D" sourceRef="sid-B3CEB01C-5C91-433A-B878-2E6DDAE69ABA" targetRef="sid-3FD22AFB-603B-4381-BD0D-1521C813A603"></sequenceFlow>
      <sequenceFlow id="sid-B2060AD7-2C3D-40EE-B4D0-F9612C15860B" sourceRef="sid-3FD22AFB-603B-4381-BD0D-1521C813A603" targetRef="sid-68971120-0526-43BF-AB3C-A12E63628B68"></sequenceFlow>
      <sequenceFlow id="sid-AAE43433-1C3E-4820-8560-AE0723E6DB2A" sourceRef="sid-68971120-0526-43BF-AB3C-A12E63628B68" targetRef="sid-73A91D58-6868-4FD9-91C9-EF25428FE200"></sequenceFlow>
      <sequenceFlow id="sid-CDDE4B78-79BD-4BBD-94AD-E405C5936FCE" sourceRef="sid-FF993176-607D-4B14-A2B7-0E2C2F475A72" targetRef="sid-0F8C3EEA-AAFD-4245-A993-B94BC8A95665"></sequenceFlow>
    </transaction>
    <endEvent id="sid-8A480075-9B9B-4F90-A429-467773E4E71F"></endEvent>
    <sequenceFlow id="sid-2BB9FAFD-4E54-4FAE-B82A-463F9C14A575" sourceRef="sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B" targetRef="sid-8A480075-9B9B-4F90-A429-467773E4E71F"></sequenceFlow>
    <endEvent id="sid-5AE9E1E6-4C91-446A-830D-0D61EBFDD531"></endEvent>
    <sequenceFlow id="sid-6FB99981-3828-4B0B-8D5C-49B5055E9178" sourceRef="sid-E6254E08-417E-4E45-A49D-62F6D8C537CD" targetRef="sid-5AE9E1E6-4C91-446A-830D-0D61EBFDD531"></sequenceFlow>
    <sequenceFlow id="sid-5104E75D-DC98-4AA5-89CE-B54000CC7384" sourceRef="startEvent1" targetRef="sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B"></sequenceFlow>
    <boundaryEvent id="sid-E6254E08-417E-4E45-A49D-62F6D8C537CD" attachedToRef="sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B" cancelActivity="false">
      <cancelEventDefinition></cancelEventDefinition>
    </boundaryEvent>
    <association id="sid-5226B772-9565-4192-8275-40DCDC8C84B4" sourceRef="sid-1C06CDE3-36D0-4AEA-BBD7-3332E3619406" targetRef="sid-B60928BB-CE96-4CA2-A40E-B6F9818F05E0" associationDirection="None"></association>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_transactionProcess">
    <bpmndi:BPMNPlane bpmnElement="transactionProcess" id="BPMNPlane_transactionProcess">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="150.0" y="162.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B" id="BPMNShape_sid-9B2AAA0F-98D9-4AAD-9C8D-89DB90D8FF8B">
        <omgdc:Bounds height="265.0" width="513.0" x="255.0" y="45.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B3CEB01C-5C91-433A-B878-2E6DDAE69ABA" id="BPMNShape_sid-B3CEB01C-5C91-433A-B878-2E6DDAE69ABA">
        <omgdc:Bounds height="30.0" width="30.0" x="285.0" y="120.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-3FD22AFB-603B-4381-BD0D-1521C813A603" id="BPMNShape_sid-3FD22AFB-603B-4381-BD0D-1521C813A603">
        <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="95.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-68971120-0526-43BF-AB3C-A12E63628B68" id="BPMNShape_sid-68971120-0526-43BF-AB3C-A12E63628B68">
        <omgdc:Bounds height="80.0" width="100.0" x="535.0" y="95.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-73A91D58-6868-4FD9-91C9-EF25428FE200" id="BPMNShape_sid-73A91D58-6868-4FD9-91C9-EF25428FE200">
        <omgdc:Bounds height="28.0" width="28.0" x="690.0" y="121.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-0F8C3EEA-AAFD-4245-A993-B94BC8A95665" id="BPMNShape_sid-0F8C3EEA-AAFD-4245-A993-B94BC8A95665">
        <omgdc:Bounds height="28.0" width="28.0" x="690.0" y="225.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B60928BB-CE96-4CA2-A40E-B6F9818F05E0" id="BPMNShape_sid-B60928BB-CE96-4CA2-A40E-B6F9818F05E0">
        <omgdc:Bounds height="80.0" width="100.0" x="450.0" y="210.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-1C06CDE3-36D0-4AEA-BBD7-3332E3619406" id="BPMNShape_sid-1C06CDE3-36D0-4AEA-BBD7-3332E3619406">
        <omgdc:Bounds height="30.0" width="30.0" x="407.6988095844615" y="160.27083227192398"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-FF993176-607D-4B14-A2B7-0E2C2F475A72" id="BPMNShape_sid-FF993176-607D-4B14-A2B7-0E2C2F475A72">
        <omgdc:Bounds height="30.0" width="30.0" x="576.5262820960537" y="160.24540625899758"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8A480075-9B9B-4F90-A429-467773E4E71F" id="BPMNShape_sid-8A480075-9B9B-4F90-A429-467773E4E71F">
        <omgdc:Bounds height="28.0" width="28.0" x="825.0" y="163.5"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-5226B772-9565-4192-8275-40DCDC8C84B4" id="BPMNShape_sid-5226B772-9565-4192-8275-40DCDC8C84B4">
        <omgdc:Bounds height="23.71424055237702" width="24.530435440812028" x="433.68434054372915" y="185.89084447644584"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-5AE9E1E6-4C91-446A-830D-0D61EBFDD531" id="BPMNShape_sid-5AE9E1E6-4C91-446A-830D-0D61EBFDD531">
        <omgdc:Bounds height="28.0" width="28.0" x="690.0" y="345.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-E6254E08-417E-4E45-A49D-62F6D8C537CD" id="BPMNShape_sid-E6254E08-417E-4E45-A49D-62F6D8C537CD">
        <omgdc:Bounds height="30.0" width="30.0" x="614.9125235228856" y="295.39091067852235"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-2BB9FAFD-4E54-4FAE-B82A-463F9C14A575" id="BPMNEdge_sid-2BB9FAFD-4E54-4FAE-B82A-463F9C14A575">
        <omgdi:waypoint x="768.0" y="177.5"></omgdi:waypoint>
        <omgdi:waypoint x="825.0" y="177.5"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-B2060AD7-2C3D-40EE-B4D0-F9612C15860B" id="BPMNEdge_sid-B2060AD7-2C3D-40EE-B4D0-F9612C15860B">
        <omgdi:waypoint x="475.0" y="135.0"></omgdi:waypoint>
        <omgdi:waypoint x="535.0" y="135.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5104E75D-DC98-4AA5-89CE-B54000CC7384" id="BPMNEdge_sid-5104E75D-DC98-4AA5-89CE-B54000CC7384">
        <omgdi:waypoint x="180.0" y="177.5"></omgdi:waypoint>
        <omgdi:waypoint x="255.0" y="177.5"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-6FB99981-3828-4B0B-8D5C-49B5055E9178" id="BPMNEdge_sid-6FB99981-3828-4B0B-8D5C-49B5055E9178">
        <omgdi:waypoint x="629.9125235228856" y="325.39091067852235"></omgdi:waypoint>
        <omgdi:waypoint x="629.9125235228856" y="359.0"></omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="359.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-CDDE4B78-79BD-4BBD-94AD-E405C5936FCE" id="BPMNEdge_sid-CDDE4B78-79BD-4BBD-94AD-E405C5936FCE">
        <omgdi:waypoint x="591.5262820960537" y="190.24540625899758"></omgdi:waypoint>
        <omgdi:waypoint x="591.5262820960537" y="239.0"></omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="239.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5AB19127-201A-4304-BBEB-668F66BEAE3D" id="BPMNEdge_sid-5AB19127-201A-4304-BBEB-668F66BEAE3D">
        <omgdi:waypoint x="315.0" y="135.0"></omgdi:waypoint>
        <omgdi:waypoint x="375.0" y="135.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5226B772-9565-4192-8275-40DCDC8C84B4" id="BPMNEdge_sid-5226B772-9565-4192-8275-40DCDC8C84B4">
        <omgdi:waypoint x="433.48330869252095" y="185.6965015064751"></omgdi:waypoint>
        <omgdi:waypoint x="458.6232831085064" y="210.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-AAE43433-1C3E-4820-8560-AE0723E6DB2A" id="BPMNEdge_sid-AAE43433-1C3E-4820-8560-AE0723E6DB2A">
        <omgdi:waypoint x="635.0" y="135.0"></omgdi:waypoint>
        <omgdi:waypoint x="690.0" y="135.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

2.3 测试类

TransactionpTest.java

import com.it.cloud.modules.activiti.service.IActReModelService;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事务子流程
 */
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class TransactionpTest {

    @Autowired
    private IActReModelService actReModelService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    /**
     * 部署流程定义
     */
    @Test
    public void deploy() {
        Deployment deployment = repositoryService.createDeployment() // 创建部署
                .addClasspathResource("diagrams/transactionProcess.bpmn20.xml") // 流程资源文件
                .name("transactionProcess流程") // 流程名称
                .deploy(); // 部署
        System.out.println("流程部署ID:" + deployment.getId());
        System.out.println("流程部署Name:" + deployment.getName());
    }

    /**
     * 启动流程实例
     */
    @Test
    public void start() {
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("transactionProcess"); // 流程定义表的KEY字段值
        System.out.println("流程实例ID:" + pi.getId());
        System.out.println("流程定义ID:" + pi.getProcessDefinitionId());
    }
}

2.4 运行

运行deploy()

2.4.1 启动任务

运行start()

效果:
流程启动后进入子流程,到达提交节点,之后到达支付节点,支付节点抛出异常被错误边界事件捕获,随后到达取消结束事件,之后被子流程的取消边界事件捕获,触发了提交订单的补偿事件,进入取消订单节点。

在这里插入图片描述


源码地址

IT-CLOUD-ACTIVITI6
开源项目,持续更新中,喜欢请 Star~

项目推荐

IT-CLOUD :IT服务管理平台,集成基础服务,中间件服务,监控告警服务等。
开源项目,持续更新中,喜欢请 Star~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值