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.配置参数传递
主要获取方式:由于是运行时的参数,所以使用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值