Activiti6:调用另外一个文件的流程(把文件当成子流程调用,并传递和获取对应流程参数)

本文档详细介绍了如何在Activiti中使用CallActivity调用子流程,并通过示例展示了子流程间的参数传递和获取。通过创建两个独立的BPMN文件,分别作为主流程和子流程,并通过process id进行关联。在主流程中启动实例,传递参数到子流程,再由子流程返回结果。测试结果显示参数传递成功,强调了正确获取运行时参数的方法。
摘要由CSDN通过智能技术生成

1.声明

当前内容主要为本人学习和测试之用,主要内容为一个流程中调用另外一个文件的子流程(主要使用Call Activity)

当前创建pbmn文件使用的是eclipse的activiti插件(例子还是员工请假的例子),安装插件请参考这篇博文:eclipse中安装activit的插件

2.创建流程文件

1.创建主要流程请假主体文件:CallSubProcessEventTest.bpmn
在这里插入图片描述
其中调用子流程使用的是:
在这里插入图片描述

2.创建另外一个文件:SubProcess.bpmn
在这里插入图片描述

3.让主流程文件关联另外一个文件的流程

当前关联主要是通过两个文件的process中的id方式进行关联,并且需要加载两个流程文件才行!

开始配置关联:
在这里插入图片描述

此时配置完毕

4.主要测试

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.history.HistoryLevel;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 
 * @author hy
 * @createTime 2021-04-03 14:49:02
 * @description 当前内容主要为测试和使用当前的呼叫另外一个文件的子流程的测试操作
 *
 */
public class CallSubProcessEventTest {
	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").setAsyncExecutorActivate(false)
				.setHistoryLevel(HistoryLevel.FULL) // 开启所有历史数据保存的功能,不开启直接查询显示0
				.buildProcessEngine();

		RepositoryService repositoryService = processEngine.getRepositoryService();
		repositoryService.createDeployment().addClasspathResource("\\test\\CallSubProcessEventTest.bpmn")
				.addClasspathResource("\\test\\SubProcess.bpmn").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 processInstance1 = runtimeService.startProcessInstanceByKey("CallSubProcessEventTest");

		// 1. 填写表单
		Task nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		// 完成填写表单的任务
		taskService.complete(nextTask.getId());

		// 2. 直接进入子流程了
		nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		processEngine.close();
	}

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

测试结果

SLF4J: Found binding in [jar:file:/D:/JavaSoftware/mavenReposity/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/JavaSoftware/mavenReposity/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Number of process definitions: 2
当前任务为:12,填写申请
当前任务为:18,人事审批

测试成功

5.参数的相互传递和获取

  1. 主流程向子流程传递参数
  2. 子流程将结果参数传递给主流程

1.配置参数传递
在这里插入图片描述

主要获取方式:由于是运行时的参数,所以使用runtimeService即可,提供相应的流程id和相应的执行id就可以获取了(任务中有执行id)

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.history.HistoryLevel;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

/**
 * 
 * @author hy
 * @createTime 2021-04-03 14:49:02
 * @description 当前内容主要为测试和使用当前的呼叫另外一个文件的子流程的测试操作
 * 	并实现参数的相互传递和获取的操作
 *
 */
public class CallSubProcessEventTest {
	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").setAsyncExecutorActivate(false)
				.setHistoryLevel(HistoryLevel.FULL) // 开启所有历史数据保存的功能,不开启直接查询显示0
				.buildProcessEngine();

		RepositoryService repositoryService = processEngine.getRepositoryService();
		repositoryService.createDeployment().addClasspathResource("\\test\\CallSubProcessEventTest.bpmn")
				.addClasspathResource("\\test\\SubProcess.bpmn").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("CallSubProcessEventTest");

		// 1. 填写表单
		Task nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("main_username", "张三");
		variables.put("main_offWorkDays", 3); // 张三发起请假三天的申请
		// 完成填写表单的任务
		taskService.complete(nextTask.getId(), variables);

		// 2. 直接进入子流程了
		nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);

		// 开始获取实际子流程的变量
		ProcessInstance subProcessInstance = runtimeService.createProcessInstanceQuery()
				.processDefinitionKey("SubProcess").singleResult();
		String username = (String) runtimeService.getVariable(subProcessInstance.getId(), "sub_username");
		Integer offWorkDays = (Integer) runtimeService.getVariable(subProcessInstance.getId(), "sub_offWorkDays");
		System.out.println(nextTask.getName() + "正在审批==>" + username + "请假" + offWorkDays + "天的请求!");

		// 人事完成审批不通过
		Map<String, Object> varibales1 = new HashMap<String, Object>();
		varibales1.put("sub_isSuccess", false);
		taskService.complete(nextTask.getId(), varibales1);

		// 项目组长审批
		nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		Boolean isSuccess = (Boolean)runtimeService.getVariable(nextTask.getExecutionId(), "sub_isSuccess");
		if (!isSuccess) {
			// 前面审批后不通过,则不继续审批
			Map<String, Object> varibales2 = new HashMap<String, Object>();
			varibales2.put("sub_isSuccess", isSuccess);
			taskService.complete(nextTask.getId(), varibales2);
		}
		
		// 最后通过主流程获取isSuccess的变量
		nextTask = taskService.createTaskQuery().singleResult();
		printTask(nextTask);
		Object object = runtimeService.getVariable(nextTask.getExecutionId(),"main_isSuccess");
		System.out.println("最后张三是否通过了当前请假申请:" + object);
		processEngine.close();
	}

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

测试结果:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/JavaSoftware/mavenReposity/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/JavaSoftware/mavenReposity/ch/qos/logback/logback-classic/1.1.3/logback-classic-1.1.3.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.activiti.engine.impl.cfg.ProcessEngineConfigurationImpl).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
Number of process definitions: 2
当前任务为:12,请假申请
当前任务为:26,人事审批
人事审批正在审批==>张三请假3天的请求!
当前任务为:30,项目组长审批
当前任务为:36,查询参数操作
最后张三是否通过了当前请假申请:false

结果获取成功

6.总结

1.以前都是直接在页面上画好子流程,现在可以将子流程分开为特定的文件,然后通过Call Activity方式调用即可,更加层次分明了

2.传递参数只需要在complete的时候传递即可,但是获取的时候需要子流程id方式以及对应的execution的id方式才可以获取,如果操作不当就会获取到null值

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值