工作流activiti 6 学习笔记八 补偿事件

举报
lu_zhishen 发表于 2021/09/27 13:16:14 2021/09/27
【摘要】 补偿事件分为:补偿边界事件(捕获事件)和补偿中间事件(抛出事件)。

补偿事件分为:补偿边界事件(捕获事件)和补偿中间事件(抛出事件)。

本示例以银行转账流程进行模拟,从工行转账到农行,工行先扣款,农行加款,抛出补偿中间事件,补偿边界事件捕获,进行补偿操作,农行扣款,工行加款。

bpm文件

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <serviceTask id="servicetask1" name="工商银行扣款" activiti:class="org.scf.act.api.event.compensation.IcbcReduceDelegate"></serviceTask>
    <serviceTask id="servicetask2" name="农业银行加款" activiti:class="org.scf.act.api.event.compensation.ABCAddDelegate"></serviceTask>
    <intermediateThrowEvent id="compensationintermediatethrowevent1" name="CompensationThrowingEvent">
      <compensateEventDefinition></compensateEventDefinition>
    </intermediateThrowEvent>
    <boundaryEvent id="boundarycompensation1" name="Compensate" attachedToRef="servicetask1" cancelActivity="true">
      <compensateEventDefinition></compensateEventDefinition>
    </boundaryEvent>
    <boundaryEvent id="boundarycompensation2" name="Compensate" attachedToRef="servicetask2" cancelActivity="true">
      <compensateEventDefinition></compensateEventDefinition>
    </boundaryEvent>
    <serviceTask id="servicetask3" name="工商银行补偿加款" activiti:class="org.scf.act.api.event.compensation.IcbcAddDelegate" isForCompensation="true"></serviceTask>
    <serviceTask id="servicetask4" name="农业银行补偿扣款" activiti:class="org.scf.act.api.event.compensation.ABCReduceDelegate" isForCompensation="true"></serviceTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="servicetask1" targetRef="servicetask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="servicetask2" targetRef="compensationintermediatethrowevent1"></sequenceFlow>
    <sequenceFlow id="flow4" sourceRef="compensationintermediatethrowevent1" targetRef="endevent1"></sequenceFlow>
    <association id="association1" sourceRef="boundarycompensation2" targetRef="servicetask4"></association>
    <association id="association2" sourceRef="boundarycompensation1" targetRef="servicetask3"></association>
  </process>

java 代码,四个service task 代码

public class IcbcAddDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("工商银行加款");
	}

}

public class IcbcReduceDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("工商银行扣款");
	}

}
public class ABCAddDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("农业银行加款");
	}
	
}
public class ABCReduceDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("农业银行扣款");
	}

}

测试代码

// 部署流程
			Deployment deploye = rs.createDeployment().addClasspathResource("compensation.bpmn").deploy();
			
			ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
			
			ProcessInstance  pi = runS.startProcessInstanceById(pdf.getId());
			System.out.println(pi.getId());

运行结果

工商银行扣款
农业银行加款
农业银行扣款
工商银行加款
227505

代码解析:

需要注意的点:补偿事件的触发顺序是,后执行节点先触发。农行加款后执行,因此农行补偿扣款先执行。

补偿参数设置,如果在工行扣款处设置本地变量在工行补偿加款时能获得本地变量

补偿次数执行,如果一个节点执行三次,触发补偿后,补偿也会执行三次

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。