工作流activiti 6 学习笔记五 结束事件

举报
lu_zhishen 发表于 2021/09/24 15:51:46 2021/09/24
【摘要】 结束事件分以下四类:endevent,errorendevent,terminateendevent,cancelendevent

结束事件分以下四类:,他们都是抛出事件。

第一类 endevent ,是最常用的普通事件,什么也不处理。

第二类 errorEndEvent,错误结束事件,需要成对使用,能与之一起使用的有错误边界事件,错误开始事件,以下代码示例展示错误结束事件与错误开始事件。

bpmn 文件

 <error id="errorid" name="throwNname"></error>
  <process id="myProcess" name="My process" isExecutable="true">
    <subProcess id="eventsubprocess1" name="Event sub Process" triggeredByEvent="true">
      <startEvent id="errorstartevent1" name="Error start">
        <errorEventDefinition errorRef="errorid"></errorEventDefinition>
      </startEvent>
      <serviceTask id="servicetask1" name="report" activiti:class="org.scf.act.api.event.start.ReportDelegate"></serviceTask>
      <endEvent id="endevent1" name="End"></endEvent>
      <sequenceFlow id="flow3" sourceRef="errorstartevent1" targetRef="servicetask1"></sequenceFlow>
      <sequenceFlow id="flow4" sourceRef="servicetask1" targetRef="endevent1"></sequenceFlow>
    </subProcess>
    <startEvent id="startevent1" name="Start"></startEvent>
    <serviceTask id="servicetask2" name="count people" activiti:class="org.scf.act.api.event.start.CountDelegate"></serviceTask>
    <endEvent id="endevent2" name="End"></endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="servicetask2"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="servicetask2" targetRef="endevent2"></sequenceFlow>
  </process>

java 代码

package org.scf.act.api.event.end;

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

public class CountDelegate implements JavaDelegate{

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("轻点人数开始");// 处理正常业务
//		//抛出error,则触发错误开始流程
//		throw new BpmnError("throwNname");
	}

}
package org.scf.act.api.event.end;

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

public class ReportDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("上报流程开始了");
	}

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

代码解释:

第三类:terminateeventend 终止结束事件,该事件触发,则整个流程结束

bpmn文件

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <parallelGateway id="parallelgateway1" name="Parallel Gateway"></parallelGateway>
    <userTask id="usertask1" name="task1"></userTask>
    <userTask id="usertask2" name="task2"></userTask>
    <userTask id="usertask3" name="task3"></userTask>
    <endEvent id="endevent1" name="End"></endEvent>
    <endEvent id="terminateendevent1" name="TerminateEndEvent">
      <terminateEventDefinition></terminateEventDefinition>
    </endEvent>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="parallelgateway1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="parallelgateway1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow4" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow6" sourceRef="usertask1" targetRef="terminateendevent1"></sequenceFlow>
  </process>

java代码

Deployment deploye = rs.createDeployment().addClasspathResource("terminateEnd.bpmn").deploy();
		
		ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
		
		ProcessInstance  pi = runS.startProcessInstanceById(pdf.getId());
		System.out.println(pi.getId());
		
		List<Task> tasks = ts.createTaskQuery().processInstanceId(pi.getId()).list();
		for(Task task:tasks) {
			if("task1".equals(task.getName())) {
				ts.complete(task.getId());// 触发终结事件
			}
		}
		long taskcount = ts.createTaskQuery().processInstanceId(pi.getId()).count();
		System.out.println(taskcount);
		// 查询实例是否还存在
		ProcessInstance newpi = runS.createProcessInstanceQuery().processInstanceId(pi.getId()).singleResult();
		System.out.println(newpi);

代码解释:

第四类:canceleventend 取消结束事件,只能用在事务子流程中,与取消边界事件一起使用。

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <transaction id="transaction1" name="Transaction">
      <startEvent id="startevent2" name="Start"></startEvent>
      <userTask id="usertask1" name="t_task1"></userTask>
      <endEvent id="cancelendevent1" name="CancelEnd">
        <cancelEventDefinition></cancelEventDefinition>
      </endEvent>
      <serviceTask id="servicetask1" name="Service Task" isForCompensation="true" activiti:class="org.scf.act.api.event.end.CompastionDelegate"></serviceTask>
      <boundaryEvent id="boundarycompensation1" name="Compensate" attachedToRef="usertask1" cancelActivity="true">
        <compensateEventDefinition></compensateEventDefinition>
      </boundaryEvent>
      <sequenceFlow id="flow2" sourceRef="startevent2" targetRef="usertask1"></sequenceFlow>
      <userTask id="usertask4" name="t_task2"></userTask>
      <sequenceFlow id="flow8" sourceRef="usertask1" targetRef="usertask4"></sequenceFlow>
      <sequenceFlow id="flow9" sourceRef="usertask4" targetRef="cancelendevent1"></sequenceFlow>
      <association id="association1" sourceRef="boundarycompensation1" targetRef="servicetask1"></association>
    </transaction>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask2" name="User Task"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="transaction1"></sequenceFlow>
    <boundaryEvent id="boundarycancel1" name="Cancel" attachedToRef="transaction1" cancelActivity="true">
      <cancelEventDefinition></cancelEventDefinition>
    </boundaryEvent>
    <sequenceFlow id="flow5" sourceRef="boundarycancel1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow6" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
    <sequenceFlow id="flow10" sourceRef="transaction1" targetRef="endevent1"></sequenceFlow>
  </process>

Java代码

package org.scf.act.api.event.end;

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

public class CompastionDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("补偿机制");
	}

}
// 部署流程
		Deployment deploye = rs.createDeployment().addClasspathResource("cancelEnd.bpmn").deploy();
		
		ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
		
		ProcessInstance  pi = runS.startProcessInstanceById(pdf.getId());
		System.out.println(pi.getId());
		
		Task task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
		System.out.println("当前任务节点"+task.getName());
		ts.complete(task.getId());
		task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
		System.out.println("当前任务节点"+task.getName());
		ts.complete(task.getId());
		task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
		System.out.println("当前任务节点"+task.getName());

运行结果

147505
当前任务节点t_task1
当前任务节点t_task2
补偿机制
当前任务节点User Task

代码解释

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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