Activiti6:一个支付订单的例子(事务子流程+定时事件+取消事件+边界补偿事件)

1.声明

当前内容主要为本人学习和使用Activiti6的操作,主要为一个简单的支付订单的例子(简陋版)

其中涉及的内容有:

  1. 用户任务和服务任务
  2. 取消边界事件、错误边界事件和定时边界事件
  3. 平行网关和判断网关
  4. 取消结束事件
  5. 信号的catching和throwing事件

2.基本的流程

在这里插入图片描述

与此对应的xml为:

<?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/test">
  <signal id="paySuccess" name="用户支付成功" activiti:scope="processInstance"></signal>
  <error id="errorFlag" errorCode="500" ></error>
  <process id="TransationSubProcessTest" name="TransationSubProcessTest" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <transaction id="transaction1" name="用户支付订单事务">
      <startEvent id="startevent2" name="Start"></startEvent>
      <userTask id="usertask1" name="用户提交订单"></userTask>
      <serviceTask id="servicetask1" name="锁定库存" activiti:class="com.hy.springboot.activiti6.service.ServiceTaskLockStockJavaDelegate"></serviceTask>
      <serviceTask id="servicetask2" name="校验支付" activiti:class="com.hy.springboot.activiti6.service.ServiceTaskPayValidateJavaDelegate"></serviceTask>
      <boundaryEvent id="boundarycompensation1" name="Compensate" attachedToRef="servicetask1" cancelActivity="true">
        <compensateEventDefinition></compensateEventDefinition>
      </boundaryEvent>
      <serviceTask id="servicetask3" name="取消库存锁定" isForCompensation="true" activiti:class="com.hy.springboot.activiti6.service.ServiceTaskUnLockStockJavaDelegate"></serviceTask>
      <boundaryEvent id="boundaryerror1" name="Error" attachedToRef="servicetask2">
        <errorEventDefinition errorRef="errorFlag"></errorEventDefinition>
      </boundaryEvent>
      <sequenceFlow id="flow3" name="to Service Task" sourceRef="usertask1" targetRef="servicetask1"></sequenceFlow>
      <sequenceFlow id="flow4" name="to User Task" sourceRef="startevent2" targetRef="usertask1"></sequenceFlow>
      <endEvent id="cancelendevent1" name="CancelEnd">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <sequenceFlow id="flow7" name="to CancelEnd" sourceRef="boundaryerror1" targetRef="cancelendevent1"></sequenceFlow>
      <parallelGateway id="parallelgateway1" name="Parallel Gateway"></parallelGateway>
      <sequenceFlow id="flow9" name="to Parallel Gateway" sourceRef="servicetask1" targetRef="parallelgateway1"></sequenceFlow>
      <userTask id="usertask2" name="用户支付"></userTask>
      <sequenceFlow id="flow10" name="to User Task" sourceRef="parallelgateway1" targetRef="usertask2"></sequenceFlow>
      <boundaryEvent id="boundarytimer1" name="Timer" attachedToRef="usertask2" cancelActivity="true">
        <timerEventDefinition>
          <timeDuration>PT15S</timeDuration>
        </timerEventDefinition>
      </boundaryEvent>
      <endEvent id="cancelendevent2" name="CancelEnd">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <sequenceFlow id="flow13" name="to CancelEnd" sourceRef="boundarytimer1" targetRef="cancelendevent2"></sequenceFlow>
      <parallelGateway id="parallelgateway2" name="Parallel Gateway"></parallelGateway>
      <sequenceFlow id="flow15" name="to Parallel Gateway" sourceRef="servicetask2" targetRef="parallelgateway2"></sequenceFlow>
      <endEvent id="endevent4" name="End"></endEvent>
      <sequenceFlow id="flow16" name="to End" sourceRef="parallelgateway2" targetRef="endevent4"></sequenceFlow>
      <userTask id="usertask3" name="用户取消订单"></userTask>
      <sequenceFlow id="flow17" name="to User Task" sourceRef="parallelgateway1" targetRef="usertask3"></sequenceFlow>
      <endEvent id="cancelendevent3" name="CancelEnd">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <sequenceFlow id="flow20" name="to CancelEnd" sourceRef="usertask3" targetRef="cancelendevent3"></sequenceFlow>
      <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
      <sequenceFlow id="flow21" name="to Exclusive Gateway" sourceRef="usertask2" targetRef="exclusivegateway1"></sequenceFlow>
      <sequenceFlow id="flow23" name="支付失败" sourceRef="exclusivegateway1" targetRef="usertask2">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${!isPay}]]></conditionExpression>
      </sequenceFlow>
      <intermediateCatchEvent id="signalintermediatecatchevent1" name="SignalCatchEvent">
        <signalEventDefinition signalRef="paySuccess"></signalEventDefinition>
      </intermediateCatchEvent>
      <sequenceFlow id="flow26" name="to SignalCatchEvent" sourceRef="parallelgateway1" targetRef="signalintermediatecatchevent1"></sequenceFlow>
      <sequenceFlow id="flow27" name="to 校验支付" sourceRef="signalintermediatecatchevent1" targetRef="servicetask2"></sequenceFlow>
      <intermediateThrowEvent id="signalintermediatethrowevent1" name="SignalThrowEvent">
        <signalEventDefinition signalRef="paySuccess"></signalEventDefinition>
      </intermediateThrowEvent>
      <sequenceFlow id="flow28" name="to SignalThrowEvent" sourceRef="exclusivegateway1" targetRef="signalintermediatethrowevent1">
        <conditionExpression xsi:type="tFormalExpression"><![CDATA[${isPay}]]></conditionExpression>
      </sequenceFlow>
      <sequenceFlow id="flow29" name="to Parallel Gateway" sourceRef="signalintermediatethrowevent1" targetRef="parallelgateway2"></sequenceFlow>
      <association id="association1" sourceRef="boundarycompensation1" targetRef="servicetask3"></association>
    </transaction>
    <sequenceFlow id="flow5" name="to Transaction" sourceRef="startevent1" targetRef="transaction1"></sequenceFlow>
    <endEvent id="endevent2" name="End"></endEvent>
    <sequenceFlow id="flow6" name="to End" sourceRef="transaction1" targetRef="endevent2"></sequenceFlow>
    <boundaryEvent id="boundarycancel1" name="Cancel" attachedToRef="transaction1" cancelActivity="true">
      <cancelEventDefinition></cancelEventDefinition>
    </boundaryEvent>
    <endEvent id="endevent3" name="End"></endEvent>
    <sequenceFlow id="flow8" name="to End" sourceRef="boundarycancel1" targetRef="endevent3"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_TransaionSubProcessTest">
    <bpmndi:BPMNPlane bpmnElement="TransaionSubProcessTest" id="BPMNPlane_TransaionSubProcessTest">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="20.0" y="199.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="transaction1" id="BPMNShape_transaction1">
        <omgdc:Bounds height="374.0" width="1071.0" x="110.0" y="30.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="startevent2" id="BPMNShape_startevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="120.0" y="203.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="200.0" y="193.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
        <omgdc:Bounds height="55.0" width="105.0" x="350.0" y="193.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundarycompensation1" id="BPMNShape_boundarycompensation1">
        <omgdc:Bounds height="30.0" width="30.0" x="398.0" y="237.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask2" id="BPMNShape_servicetask2">
        <omgdc:Bounds height="55.0" width="105.0" x="770.0" y="193.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundaryerror1" id="BPMNShape_boundaryerror1">
        <omgdc:Bounds height="30.0" width="30.0" x="804.0" y="227.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="servicetask3" id="BPMNShape_servicetask3">
        <omgdc:Bounds height="55.0" width="105.0" x="390.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="cancelendevent1" id="BPMNShape_cancelendevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="880.0" y="256.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="parallelgateway1" id="BPMNShape_parallelgateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="500.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
        <omgdc:Bounds height="55.0" width="105.0" x="570.0" y="90.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundarytimer1" id="BPMNShape_boundarytimer1">
        <omgdc:Bounds height="30.0" width="30.0" x="640.0" y="130.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="cancelendevent2" id="BPMNShape_cancelendevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="690.0" y="159.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="parallelgateway2" id="BPMNShape_parallelgateway2">
        <omgdc:Bounds height="40.0" width="40.0" x="1030.0" y="200.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent4" id="BPMNShape_endevent4">
        <omgdc:Bounds height="35.0" width="35.0" x="1120.0" y="203.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="570.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="cancelendevent3" id="BPMNShape_cancelendevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="760.0" y="310.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="770.0" y="97.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="signalintermediatecatchevent1" id="BPMNShape_signalintermediatecatchevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="605.0" y="203.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="signalintermediatethrowevent1" id="BPMNShape_signalintermediatethrowevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="914.0" y="100.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="boundarycancel1" id="BPMNShape_boundarycancel1">
        <omgdc:Bounds height="30.0" width="30.0" x="442.0" y="386.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
        <omgdc:Bounds height="35.0" width="35.0" x="1220.0" y="199.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent3" id="BPMNShape_endevent3">
        <omgdc:Bounds height="35.0" width="35.0" x="530.0" y="460.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
        <omgdi:waypoint x="305.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="350.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="305.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="155.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="200.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="155.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="834.0" y="242.0"></omgdi:waypoint>
        <omgdi:waypoint x="897.0" y="256.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="834.0" y="242.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="455.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="500.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="455.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="520.0" y="200.0"></omgdi:waypoint>
        <omgdi:waypoint x="519.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="570.0" y="117.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="520.0" y="200.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow13" id="BPMNEdge_flow13">
        <omgdi:waypoint x="670.0" y="145.0"></omgdi:waypoint>
        <omgdi:waypoint x="707.0" y="159.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="670.0" y="145.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow15" id="BPMNEdge_flow15">
        <omgdi:waypoint x="875.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="1030.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="875.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow16" id="BPMNEdge_flow16">
        <omgdi:waypoint x="1070.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="1120.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="1070.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow17" id="BPMNEdge_flow17">
        <omgdi:waypoint x="520.0" y="240.0"></omgdi:waypoint>
        <omgdi:waypoint x="519.0" y="327.0"></omgdi:waypoint>
        <omgdi:waypoint x="570.0" y="327.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="520.0" y="240.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow20" id="BPMNEdge_flow20">
        <omgdi:waypoint x="675.0" y="327.0"></omgdi:waypoint>
        <omgdi:waypoint x="760.0" y="327.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="675.0" y="327.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow21" id="BPMNEdge_flow21">
        <omgdi:waypoint x="675.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="770.0" y="117.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="675.0" y="117.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow23" id="BPMNEdge_flow23">
        <omgdi:waypoint x="790.0" y="97.0"></omgdi:waypoint>
        <omgdi:waypoint x="789.0" y="50.0"></omgdi:waypoint>
        <omgdi:waypoint x="624.0" y="50.0"></omgdi:waypoint>
        <omgdi:waypoint x="622.0" y="90.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="790.0" y="97.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow26" id="BPMNEdge_flow26">
        <omgdi:waypoint x="540.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="605.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="540.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow27" id="BPMNEdge_flow27">
        <omgdi:waypoint x="640.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="770.0" y="220.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="79.0" x="640.0" y="220.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow28" id="BPMNEdge_flow28">
        <omgdi:waypoint x="810.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="914.0" y="117.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="810.0" y="117.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow29" id="BPMNEdge_flow29">
        <omgdi:waypoint x="949.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="1050.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="1050.0" y="200.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="48.0" width="100.0" x="949.0" y="117.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="association1" id="BPMNEdge_association1">
        <omgdi:waypoint x="413.0" y="267.0"></omgdi:waypoint>
        <omgdi:waypoint x="442.0" y="300.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="55.0" y="216.0"></omgdi:waypoint>
        <omgdi:waypoint x="110.0" y="217.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="55.0" y="216.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="1181.0" y="217.0"></omgdi:waypoint>
        <omgdi:waypoint x="1220.0" y="216.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="1181.0" y="217.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="457.0" y="416.0"></omgdi:waypoint>
        <omgdi:waypoint x="547.0" y="460.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="16.0" width="100.0" x="457.0" y="416.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

一般由取消事件触发补偿服务,其中为了方便设定支付订单超时为15秒

3.对应的具体服务

锁定库存服务

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

/**
 * 
 * @author hy
 * @createTime 2021-04-04 08:19:10
 * @description 当前内容主要为TransationSubProcessTest中的service Task 的锁定库存服务
 *
 */
public class ServiceTaskLockStockJavaDelegate implements JavaDelegate {
	public static final int stockCount = 100; // 默认库存在为100

	@Override
	public void execute(DelegateExecution execution) {
		// TODO Auto-generated method stub
		System.out.println("执行锁定库存的执行操作......");
	}

}

取消锁定库存服务

import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.JavaDelegate;

/**
 * 
 * @author hy
 * @createTime 2021-04-04 08:19:10
 * @description 当前内容主要为TransationSubProcessTest中的service Task 的取消锁定库存服务
 *
 */
public class ServiceTaskUnLockStockJavaDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		// TODO Auto-generated method stub
		System.out.println("执行解除锁定库存的执行操作......");
	}

}

校验订单支付服务

/**
 * 
 * @author hy
 * @createTime 2021-04-04 08:22:10
 * @description 当前内容主要为TransationSubProcessTest中的支付校验的Service Task
 *
 */
public class ServiceTaskPayValidateJavaDelegate implements JavaDelegate{

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("开始校验当前的用户支付结果......");
		boolean isPay = false;//默认检查结果为支付失败
		if(!isPay) {
			System.out.println("用户支付失败!");
			throw new BpmnError("500"); // 未支付
		}else {
			System.out.println("用户支付成功!");
		}
		
	}
	
}

4.实际的代码操作

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.HistoryService;
import org.activiti.engine.ManagementService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.asyncexecutor.DefaultAsyncJobExecutor;
import org.activiti.engine.impl.history.HistoryLevel;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 
 * @author hy
 * @createTime 2021-04-04 08:33:21
 * @description 当前内容主要为测试和使用TransationSubProcessTest.bpmn20.xml这个用户订单支付事务
 *
 */
public class TransationSubProcessTest {
	public static void main(String[] args) {
		ProcessEngine processEngine = ProcessEngineConfiguration.createStandaloneInMemProcessEngineConfiguration()
				.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_CREATE_DROP)
				.setJdbcUrl("jdbc:h2:mem:my-own-db;DB_CLOSE_DELAY=1000")
				.setAsyncExecutor(new DefaultAsyncJobExecutor())
				.setAsyncExecutorActivate(true)
				.setHistoryLevel(HistoryLevel.FULL) // 开启所有历史数据保存的功能,不开启直接查询显示0
				.buildProcessEngine();

		RepositoryService repositoryService = processEngine.getRepositoryService();
		repositoryService.createDeployment().addClasspathResource("\\test\\TransationSubProcessTest.bpmn20.xml").deploy();
		System.out.println("Number of process definitions: " + repositoryService.createProcessDefinitionQuery().count());

		TaskService taskService = processEngine.getTaskService();
		RuntimeService runtimeService = processEngine.getRuntimeService();
		HistoryService historyService = processEngine.getHistoryService();
		ManagementService managementService = processEngine.getManagementService();

		ProcessInstance mainProcessInstance = runtimeService.startProcessInstanceByKey("TransationSubProcessTest");

		// 1. 直接得到一个user task(用户提交订单)
		Task nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		System.out.println("用户提交了一个订单操作......");
		taskService.complete(nextTask.getId());
		
		// 2.获取下一步执行的操作步骤
		List<Task> taskList = taskService.createTaskQuery().list();
		printTasks(taskList);
		
		// 休眠30秒等待触发当前的取消事件,该部分主要测试定时边界事件触发的取消结束事件
		/*
		try {
			Thread.sleep(30*1000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}*/
		
		// 实际操作中直接执行支付任务
		for (Task task : taskList) {
			/* 用户支付操作
			if("用户支付".equals(task.getName())) {
				// 开始执行用户支付
				System.out.println("用户支付订单....");
				boolean isPay = true;
				Map<String,Object> variables =new HashMap<String,Object>();
				variables.put("isPay", isPay);
				// 默认为成功
				taskService.complete(task.getId(), variables);
			}*/
			// 用户取消订单操作
			if("用户取消订单".equals(task.getName())){
				System.out.println("用户执行取消订单操作....");
				taskService.complete(task.getId());
			}
		}
		processEngine.close();
	}
	
	public static void printTasks(Collection<Task> taskList) {
		for (Task task : taskList) {
			printTask(task);
		}
	}

	public static void printTask(Task task) {
		System.out.println("当前任务为:" + task.getId() + "," + task.getName());
	}
}

5.测试

开启定时等待30秒发现触发订单取消服务(将其他的进行注释)

Number of process definitions: 1
当前任务为:14,用户提交订单
用户提交了一个订单操作......
执行锁定库存的执行操作......
当前任务为:24,用户支付
当前任务为:26,用户取消订单
执行解除锁定库存的执行操作......

执行直接支付操作(注释掉其他的)

Number of process definitions: 1
当前任务为:14,用户提交订单
用户提交了一个订单操作......
执行锁定库存的执行操作......
当前任务为:24,用户支付
当前任务为:26,用户取消订单
用户支付订单....
开始校验当前的用户支付结果......
用户支付失败!
执行解除锁定库存的执行操作......

结果正确(由于默认的校验服务中默认为支付失败)

执行支付取消操作(注释掉其他的)

Number of process definitions: 1
当前任务为:14,用户提交订单
用户提交了一个订单操作......
执行锁定库存的执行操作......
当前任务为:24,用户支付
当前任务为:26,用户取消订单
用户执行取消订单操作....
执行解除锁定库存的执行操作......

6.总结

1.默认取消结束事件触发边界取消事件,触发补偿事件

2.编写过程中完善了对各种事件的理解操作,更加深入理解了activiti的各种流程

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值