首页 > 技术文章 > flowable-流程中心设计之边界事件(五)

LQBlog 2022-01-18 15:52 原文

简介

边界事件:边界事件属于一种特殊的中间事件。

区别是: 中间事件 可以单独作为流程元素存在于流程中,而 边界事件 必须附属于某个流程元素(如任务、子流程等)。边界事件是Catching事件。

 

 

边界出错事件

说明

依附于子流程或者TASK上发生异常触发事件。

本质上是捕获BPMNError,如果配置了错误引用,则只会捕获指定code,没有则是捕获全部

 

依附在任务上

 

1.记录一个插曲,我是在任务办理阶段 想着抛出一BPMNError异常 以为就可以正常走边界异常这条线

结果始终没生效

委托类可以可以正常执行的 所以参考:org.flowable.engine.impl.bpmn.helper.ClassDelegate#execute

发现真正触发边界的是以下标红的方法 当出现异常可以选择是抛出还是走边界异常。

   try {
            activityBehaviorInstance.execute(execution);
        } catch (BpmnError error) {
            ErrorPropagation.propagateError(error, execution);
        } catch (RuntimeException e) {
            if (!ErrorPropagation.mapException(e, (ExecutionEntity) execution, mapExceptions))
                throw e;
        }

针对某些api并没有这样的捕获处理.所以自己定义,或者将来在最外层定义一个拦截器(必须最外层哦)。以下我的测试

必须最外层哦:比如我在ExecutinListener监听器try catch。try cath因为走了异常边界没有往上抛异常 还是会继续执行api办理的后续流程。导致出错。

最外层的意思就是以前那条线出现异常不能往下走,则尝试走异常边界

  /**
     * 任务办理
     * @param completeReqDTO
     */
    @Override
    @Transactional
    public void complete(CompleteReqDTO completeReqDTO) throws BusinessException {
//在flowable生命周期里面执行 processEngine.getProcessEngineConfiguration().getCommandExecutor().execute(
new Command<Object>() { @SneakyThrows @Override public Object execute(CommandContext commandContext) { TaskStatusEnum taskStatusEnum = null; Task task = processEngine.getTaskService().createTaskQuery().taskId(completeReqDTO.getTaskId()).singleResult();; try { taskStatusEnum = TaskStatusEnum.convert.convert(completeReqDTO.getStatus()); } catch (Exception e) { log.error("枚举转换错误:{},{}", TaskStatusEnum.class, completeReqDTO.getStatus()); Execution execution = ExecutionHelper.getExecution(task.getExecutionId()); //走错误边界事件 ErrorPropagation.propagateError(new BpmnError("aaa"), (ExecutionEntityImpl) execution); } if (taskStatusEnum == null) { throw new BusinessException(MessageFormatter.arrayFormat(ErrorCode.VALID_PARAMS_ERROR.getErrorMsg(), new String[]{"状态不能为空"}).getMessage()); } processEngine.getTaskService().complete(task.getId(), completeReqDTO.getTaskVariables()); return null; } }); }

 在flowable生命周期执行指的是 在执行我们的业务Command 之前会有一系列拦截器做初始化操作 比如Command.getContext();

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:flowable="http://flowable.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.flowable.org/processdef">
  <process id="boundary_errorf_sub" name="错误边界子流程" isExecutable="true">
    <startEvent id="startEvent1"></startEvent>
    <userTask id="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" name="组员审批" flowable:assignee="组员1">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <userTask id="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" name="总监审批" flowable:assignee="总监1">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C" sourceRef="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" targetRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF"></sequenceFlow>
    <endEvent id="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D"></endEvent>
    <sequenceFlow id="sid-6EEA2922-F966-471A-A4E3-3BB10E721254" sourceRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" targetRef="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D"></sequenceFlow>
    <userTask id="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" name="组长代审" flowable:assignee="组长1">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-8996B76B-F7AA-4123-AC89-301D0147C402" name="超时" sourceRef="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" targetRef="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5"></sequenceFlow>
    <sequenceFlow id="sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4" sourceRef="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" targetRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF"></sequenceFlow>
    <boundaryEvent id="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" attachedToRef="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" cancelActivity="true">
      <timerEventDefinition>
        <timeDuration>PT5M</timeDuration>
      </timerEventDefinition>
    </boundaryEvent>
    <userTask id="sid-72ABD643-8FDF-4364-B36A-B204ECD23250" name="发起人填表" flowable:assignee="小王">
      <extensionElements>
        <modeler:initiator-can-complete xmlns:modeler="http://flowable.org/modeler"><![CDATA[false]]></modeler:initiator-can-complete>
      </extensionElements>
    </userTask>
    <sequenceFlow id="sid-91282A7D-131A-4010-8A34-61B09A97974E" sourceRef="startEvent1" targetRef="sid-72ABD643-8FDF-4364-B36A-B204ECD23250"></sequenceFlow>
    <sequenceFlow id="sid-D660D588-A664-4B39-8CC2-5A7EFA14A2B3" sourceRef="sid-72ABD643-8FDF-4364-B36A-B204ECD23250" targetRef="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_boundary_errorf_sub">
    <bpmndi:BPMNPlane bpmnElement="boundary_errorf_sub" id="BPMNPlane_boundary_errorf_sub">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="0.0" y="160.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" id="BPMNShape_sid-87FCF890-845A-42F9-8B52-B40B80E1DA52">
        <omgdc:Bounds height="80.0" width="100.0" x="345.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" id="BPMNShape_sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF">
        <omgdc:Bounds height="80.0" width="100.0" x="525.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D" id="BPMNShape_sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D">
        <omgdc:Bounds height="28.0" width="28.0" x="670.0" y="161.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" id="BPMNShape_sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5">
        <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" id="BPMNShape_sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A">
        <omgdc:Bounds height="31.0" width="31.0" x="355.01441312765667" y="199.90121833936652"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-72ABD643-8FDF-4364-B36A-B204ECD23250" id="BPMNShape_sid-72ABD643-8FDF-4364-B36A-B204ECD23250">
        <omgdc:Bounds height="80.0" width="100.0" x="150.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C" id="BPMNEdge_sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C">
        <omgdi:waypoint x="444.9499999999431" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="524.9999999999723" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4" id="BPMNEdge_sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4">
        <omgdi:waypoint x="474.95000000000005" y="340.0"></omgdi:waypoint>
        <omgdi:waypoint x="575.0" y="340.0"></omgdi:waypoint>
        <omgdi:waypoint x="575.0" y="214.95000000000002"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-6EEA2922-F966-471A-A4E3-3BB10E721254" id="BPMNEdge_sid-6EEA2922-F966-471A-A4E3-3BB10E721254">
        <omgdi:waypoint x="624.949999999996" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="670.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-D660D588-A664-4B39-8CC2-5A7EFA14A2B3" id="BPMNEdge_sid-D660D588-A664-4B39-8CC2-5A7EFA14A2B3">
        <omgdi:waypoint x="249.94999999986936" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="344.9999999999363" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-8996B76B-F7AA-4123-AC89-301D0147C402" id="BPMNEdge_sid-8996B76B-F7AA-4123-AC89-301D0147C402">
        <omgdi:waypoint x="372.8906195078865" y="231.74010984107392"></omgdi:waypoint>
        <omgdi:waypoint x="376.0" y="257.99453814261057"></omgdi:waypoint>
        <omgdi:waypoint x="425.0" y="257.99453814261057"></omgdi:waypoint>
        <omgdi:waypoint x="425.0" y="300.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-91282A7D-131A-4010-8A34-61B09A97974E" id="BPMNEdge_sid-91282A7D-131A-4010-8A34-61B09A97974E">
        <omgdi:waypoint x="29.94999946593476" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="150.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
View Code

 

依附在子流程上

子流程执行过程中抛出BPMNError异常,也可以通过异常结束事件触发

定时边界事件

说明

任务到了指定事件没有办理执行边界,适用于超时任务 注:需要开启springProcessEngineConfiguration.setAsyncExecutorActivate(true);异步执行开关

依附在任务上

5分钟超时由组长代审,当执行到组员审批同时会执行一条job任务可以观察此表 act_ru_timer_job

超时后执行了定时边界这条线job会删除

未超时任务被办理了此job也会被删除

 

 

 取消任务指的是,当执行边界事件当前任务没有办理是否取消

   <boundaryEvent id="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" cancelActivity="true">
      <timerEventDefinition>
        <timeDate>PT5M</timeDate>
      </timerEventDefinition>
    </boundaryEvent>

相关参数

可以参考:https://www.cnblogs.com/LQBlog/p/15813380.html#autoid-3-2-0

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:flowable="http://flowable.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.flowable.org/processdef">
  <process id="boundary_errorf_sub" name="错误边界子流程" isExecutable="true">
    <startEvent id="startEvent1"></startEvent>
    <userTask id="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" name="组员审批"></userTask>
    <sequenceFlow id="sid-5FA93B8F-FFF3-47B1-B6BB-390D37E22AC5" sourceRef="startEvent1" targetRef="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52"></sequenceFlow>
    <userTask id="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" name="总监审批"></userTask>
    <sequenceFlow id="sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C" sourceRef="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" targetRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF"></sequenceFlow>
    <endEvent id="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D"></endEvent>
    <sequenceFlow id="sid-6EEA2922-F966-471A-A4E3-3BB10E721254" sourceRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" targetRef="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D"></sequenceFlow>
    <boundaryEvent id="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" cancelActivity="true">
      <timerEventDefinition>
        <timeDate>PT5M</timeDate>
      </timerEventDefinition>
    </boundaryEvent>
    <userTask id="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" name="组长代审"></userTask>
    <sequenceFlow id="sid-8996B76B-F7AA-4123-AC89-301D0147C402" name="超时" sourceRef="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" targetRef="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5"></sequenceFlow>
    <sequenceFlow id="sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4" sourceRef="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" targetRef="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_boundary_errorf_sub">
    <bpmndi:BPMNPlane bpmnElement="boundary_errorf_sub" id="BPMNPlane_boundary_errorf_sub">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-87FCF890-845A-42F9-8B52-B40B80E1DA52" id="BPMNShape_sid-87FCF890-845A-42F9-8B52-B40B80E1DA52">
        <omgdc:Bounds height="80.0" width="100.0" x="165.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF" id="BPMNShape_sid-B779F5AA-1D99-4CBF-87C7-EBBAD03E9DEF">
        <omgdc:Bounds height="80.0" width="100.0" x="345.0" y="135.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D" id="BPMNShape_sid-5539B9CE-EDEA-461E-9806-FFC5CB50708D">
        <omgdc:Bounds height="28.0" width="28.0" x="490.0" y="161.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A" id="BPMNShape_sid-8682C781-C10D-4DDF-9D61-9DBB6F72CD4A">
        <omgdc:Bounds height="31.0" width="31.0" x="180.0" y="195.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5" id="BPMNShape_sid-FB858F22-0036-40DE-AC16-BD63E1BEF9F5">
        <omgdc:Bounds height="80.0" width="100.0" x="195.0" y="300.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C" id="BPMNEdge_sid-633CEB6F-601D-44E2-B3E4-1885C104CA1C">
        <omgdi:waypoint x="264.9499999999431" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="345.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5FA93B8F-FFF3-47B1-B6BB-390D37E22AC5" id="BPMNEdge_sid-5FA93B8F-FFF3-47B1-B6BB-390D37E22AC5">
        <omgdi:waypoint x="129.94340692927761" y="177.55019845363262"></omgdi:waypoint>
        <omgdi:waypoint x="164.99999999999906" y="176.4985"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4" id="BPMNEdge_sid-5DBFFFA3-B1DE-4557-9B7B-828CE658B3A4">
        <omgdi:waypoint x="294.9499999998728" y="340.0"></omgdi:waypoint>
        <omgdi:waypoint x="395.0" y="340.0"></omgdi:waypoint>
        <omgdi:waypoint x="395.0" y="214.95000000000002"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-6EEA2922-F966-471A-A4E3-3BB10E721254" id="BPMNEdge_sid-6EEA2922-F966-471A-A4E3-3BB10E721254">
        <omgdi:waypoint x="444.95000000000005" y="175.0"></omgdi:waypoint>
        <omgdi:waypoint x="490.0" y="175.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-8996B76B-F7AA-4123-AC89-301D0147C402" id="BPMNEdge_sid-8996B76B-F7AA-4123-AC89-301D0147C402">
        <omgdi:waypoint x="196.0" y="226.94999118403769"></omgdi:waypoint>
        <omgdi:waypoint x="196.0" y="257.99453814261057"></omgdi:waypoint>
        <omgdi:waypoint x="245.0" y="257.99453814261057"></omgdi:waypoint>
        <omgdi:waypoint x="245.0" y="300.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>
View Code

依附在子流程上

同理,子流程未完结触发超时任务

 

 

边界信号事件

说明

该事件接收到指定的信号后触发,不同的是信号事件可以定义成全局的也可以额定义成流程实例作用范围,相同作用于一处发信号,所有信号的边界事件都能接收

信号配置以及启动可以参考《flowable-流程中心设计之开始事件(三)

设计

 

 

边界消息事件

配置以及发布消息参考:https://www.cnblogs.com/LQBlog/p/15813380.html#autoid-4-5-0

设计设信号事件一直 只是信号替换成消息

设计

 

 

边界取消事件

说明

配合子流程使用 可参考https://www.cnblogs.com/LQBlog/p/15816934.html#autoid-4-3-0

设计

 

 

补偿边界事件

说明

 

补偿边界事件与其他边界事件的策略不同。 其他边界事件(比如信号边界事件)当到达关联的节点就会被激活。 离开节点时,就会挂起,对应的事件订阅也会取消。 补偿边界事件则不同。补偿边界事件在关联的节点成功完成时激活。 当补偿事件触发或对应流程实例结束时,事件订阅才会删除。

触发补偿事件的2种情况

补偿边界事件的触发有两种情况:

                                   1. 事务子流程被取消时,会触发事务子流程里面的补偿边界事件。

                                   2. 使用补偿中间事件来触发,需要时Throwing事件。

特性

  1. 当补偿中间事件触发时,会触发当前执行流中的补偿Catching事件,如果补偿边界事件依附的节点是多实例的,那么补偿事件也会触发多次。
  2. 补偿的执行顺序会按照倒叙进行补偿。
  3. 只会补偿已经完成的任务,还没有完成的任务补偿边界事件是不会触发的。

设计

 

推荐阅读