工作流activiti 6 学习笔记十二 子流程

举报
lu_zhishen 发表于 2021/09/29 17:46:00 2021/09/29
【摘要】 子流程分为SubProcess,callActivity 和 adHocSubProcess第一类:SubProcess示例:新建一个子流程,触发错误边界事件,使流程转向task1bpmn文件<process id="myProcess" name="My process" isExecutable="true"> <startEvent id="startevent1" name="...

子流程分为SubProcess,callActivity 和 adHocSubProcess

第一类:SubProcess

示例:新建一个子流程,触发错误边界事件,使流程转向task1

bpmn文件

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="task1"></userTask>
    <subProcess id="subprocess1" name="Sub Process">
      <startEvent id="startevent2" name="Start"></startEvent>
      <endEvent id="endevent2" name="End"></endEvent>
      <serviceTask id="servicetask1" name="Service Task" activiti:class="org.scf.act.api.subprocess.SubProcessDelegate"></serviceTask>
      <sequenceFlow id="flow2" sourceRef="startevent2" targetRef="servicetask1"></sequenceFlow>
      <sequenceFlow id="flow3" sourceRef="servicetask1" targetRef="endevent2"></sequenceFlow>
    </subProcess>

serviceTask 类

public class SubProcessDelegate implements JavaDelegate {

	@Override
	public void execute(DelegateExecution execution) {
		System.out.println("这是一个子流程");
		throw new BpmnError("子流程抛出错误边界事件");
	}

}

测试类

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

运行结果:

这是一个子流程
390005

第二类:callActivity 

使用场景:当某些审批节点被多个流程共用时可以单独抽取出来,形成一个子流程,被主流程调用。

以请假为例:把项目经理和项目总监审批抽取出来,形成子流程,在用户填报请假单后流转至该子流程。

此示例将展示一次部署多流程的方法,根据父流程查子流程的方法。

CallActivitiSub.bpmn

<process id="activitiSub" name="activitiSub" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="项目经理"></userTask>
    <userTask id="usertask2" name="项目总监"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
  </process>

OffWork.bpmn

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="用户填报"></userTask>
    <callActivity id="callactivity1" name="公共流程" calledElement="activitiSub"></callActivity>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="callactivity1"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="callactivity1" targetRef="endevent1"></sequenceFlow>
  </process>

测试类

// 部署流程
		Deployment deploye = rs.createDeployment().addClasspathResource("CallActivitiSub.bpmn").addClasspathResource("OffWork.bpmn").deploy();
		// 查询主流程的流程定义id
		ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).processDefinitionKey("myProcess").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());
		// 根据父亲pid 获取 子pid
		ProcessInstance  subPi = runS.createProcessInstanceQuery().superProcessInstanceId(pi.getId()).singleResult();
		System.out.println(subPi.getId());
		task = ts.createTaskQuery().processInstanceId(subPi.getId()).singleResult();
		System.out.println("当前节点是:"+task.getName());

运行结果:

392508
当前节点是:用户填报
392514
当前节点是:项目经理

代码解释:

 

第三种:adHocSubProcess

该子流程里面的节点没有流向,需要代码控制谁来审批

bpmn文件

<process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <adHocSubProcess id="eventsubprocess1" name="adhoc sub Process" ordering="Sequential">
      <userTask id="usertask1" name="task1"></userTask>
      <userTask id="usertask2" name="task2"></userTask>
    </adHocSubProcess>
    <userTask id="usertask3" name="task3"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="eventsubprocess1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="eventsubprocess1" targetRef="usertask3"></sequenceFlow>
    <sequenceFlow id="flow3" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
  </process>

测试类

// 部署流程
		Deployment deploye = rs.createDeployment().addClasspathResource("subprocess_adhoc.bpmn").deploy();
		ProcessDefinition  pdf = rs.createProcessDefinitionQuery().deploymentId(deploye.getId()).singleResult();
		
		ProcessInstance  pi = runS.startProcessInstanceById(pdf.getId());
		System.out.println(pi.getId());
		// 获取特别流exe id
		Execution exe = runS.createExecutionQuery().processInstanceId(pi.getId()).activityId("eventsubprocess1").singleResult();
		System.out.println(exe.getId());
		// 让特别流执行task2
		runS.executeActivityInAdhocSubProcess(exe.getId(), "usertask2");
		// 完成task2 的审批
		Task task = ts.createTaskQuery().processInstanceId(pi.getId()).taskDefinitionKey("usertask2").singleResult();
		ts.complete(task.getId());
		//结束特别流的审批
		runS.completeAdhocSubProcess(exe.getId());
		
		// 查询当前节点
		task = ts.createTaskQuery().processInstanceId(pi.getId()).singleResult();
		System.out.println(task.getName());
	}

运行结果

395005
395008
task3

代码解释

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

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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