activiti 常用方法封装及测试
Activiti 项目是一项新的基于Apache许可的开源BPM平台,从基础开始构建,旨在提供支持新的BPMN 2.0标准,包括支持对象管理组(OMG),提供技术实现。
对于开发者来说,Activiti 就是一套 BPMN2.0 标准的流程引擎,通过 Activiti 实现流程的驱动及各类操作。
这里对(简单场景)常用的方法进行了简单的封装与测试。
/**
* @Description 部署流程
* name:流程名称
* category:流程类别
* fileName:流程文件名称
*/
@Override
public String deploy(String name, String category, String fileName) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
.createDeployment()//创建一个部署对象
.name(name)//添加部署名称
.category(category)
.addClasspathResource("processes/" + fileName)//从classpath的资源中加载,一次只能加载一个文件
.deploy();//完成部署
System.out.println("<------ 流程部署成功 ------>");
System.out.println("部署ID:" + deployment.getId());
System.out.println("部署名称:" + deployment.getName());
System.out.println("部署时间:" + DateUtils.dateToStr(deployment.getDeploymentTime()));
return deployment.getId();
}
/**
* @Description 字符串方式部署流程
*/
@Override
public String deployStr(String name, String category, String fileName, String processStr) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
Deployment deployment =
processEngine.getRepositoryService().createDeployment().name(name).category(category).addString(fileName,
processStr).deploy();
System.out.println("部署ID:" + deployment.getId());
System.out.println("部署名称:" + deployment.getName());
System.out.println("部署时间:" + DateUtils.dateToStr(deployment.getDeploymentTime()));
return deployment.getId();
}
/**
* @Description 部署 camunda 画出的流程图
*/
@Override
public String deployCamunda(String name, String category, String fileName, String processStr) {
// 补充通用头
if (!processStr.contains("<bpmn:definitions ")) processStr = this.commonHeader + processStr;
// 替换 camunda 标签
while (processStr.contains("camunda")) processStr = processStr.replace("camunda", "activiti");
while (processStr.contains("Camunda")) processStr = processStr.replace("camunda", "activiti");
return this.deployStr(name, category, fileName, processStr);
}
/**
* @Description 启动一个流程且设置实例全局变量,并返回启动的流程实例的 ID
* processDefineId 流程定义 ID
* processName 流程实例命名
* globalParamsMap 流程实例的全局变量
*/
@Override
public String startProcess(String processDefineId, String processName, Map globalParamsMap) {
if (null == processDefineId || processDefineId.length() == 0)
throw new NullPointerException("processInstanceId is null!");
if (null == processName || processName.length() == 0)
throw new NullPointerException("processName is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
ProcessInstance instance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefineId,
globalParamsMap);
//为流程定义设置名称
processEngine.getRuntimeService().setProcessInstanceName(instance.getId(), processName);
return instance.getId();
}
/**
* @Description 根据流程实例 ID 获取当前环节
*/
@Override
public List<Task> getCurrentNode(String processInstanceId) {
if (null == processInstanceId || processInstanceId.length() == 0)
throw new NullPointerException("processInstanceId is null!");
if (this.isProcessFinish(processInstanceId))
throw new RuntimeException("instance : " + processInstanceId + " is over!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
List<Task> task =
processEngine.getTaskService().createTaskQuery().processInstanceId(processInstanceId).active().list();
return task;
}
/**
* @Description 完成任务并设置实例全局变量
* processInstanceId:流程实例中当前办理节点的 ID
* globalParamsMap:流程实例的全局变量
*/
@Override
public void completeNode(String taskId, Map globalParamsMap) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
String assignee = task.getAssignee();
if (null == assignee || assignee.length() == 0)
throw new RuntimeException("task : " + taskId + " has no assignee ! ");
taskService.complete(taskId, globalParamsMap);
}
/**
* @Description 查看流程是否已结束
* processInstanceId 流程实例 ID
*/
@Override
public boolean isProcessFinish(String processInstanceId) {
if (null == processInstanceId || processInstanceId.length() == 0)
throw new NullPointerException("processInstanceId is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
HistoricProcessInstance historicProcessInstance =
processEngine.getHistoryService().createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
if (Objects.isNull(historicProcessInstance)) throw new NullPointerException("processInstance is not exsits");
if (Objects.isNull(historicProcessInstance.getEndTime())) return false;
else return true;
}
/**
* @Description 为实例设置流程变量
*/
@Override
public void setInstanceParams(String processInstanceId, Map map) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
runtimeService.setVariables(processInstanceId, map);
}
/**
* @Description 获取实例中的流程变量
*/
@Override
public Map<String, Object> getInstanceParams(String processInstanceId) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = runtimeService.getVariables(processInstanceId);
System.out.println(" <------ 实例中的变量 ------> ");
variables.forEach((k, v) -> {
System.out.println(k + " " + v);
});
return variables;
}
/**
* @Description 设置环节节点的 local 变量
*/
@Override
public void setTaskLocalParams(String taskId, String k, String v) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
taskService.setVariableLocal(task.getId(), k, v);
System.out.println("设置 local 变量成功 : " + k + " : " + v);
}
/**
* @Description 获取环节节点的 local 变量
*/
@Override
public String getTaskLocalParams(String taskId, String k) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
String v = taskService.getVariableLocal(task.getId(), k).toString();
return v;
}
/**
* @Description 指定任务办理人
* assignee 不为空时,则为人员认领任务
* assignee 为空时,则为归还任务
*/
@Override
public void setAssignee(String taskId, String assignee) throws RuntimeException {
if (null == taskId || taskId.length() == 0) throw new NullPointerException("taskId is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
// 归还任务,办理人必须传 null,传 "" 也会被判定为有办理人
if (null == assignee || assignee.length() == 0) taskService.setAssignee(taskId, null);
else {
// 拾取任务
taskService.claim(taskId, assignee);
}
}
/**
* @Description 获取候选人列表,
* 注意 identityLink 中拿 processDefineId 和 instanceId 都为空!
*/
@Override
public List<IdentityLink> getIdentityLinks(String taskId) {
if (null == taskId || taskId.length() == 0) throw new NullPointerException("taskId is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<IdentityLink> list = taskService.getIdentityLinksForTask(taskId);
System.out.println(" >>>>>> " + taskId + " : candidateUsers : " + list.size() + " <<<<<< ");
for (int i = 0; i < list.size(); i++) {
IdentityLink identityLink = list.get(i);
System.out.println(" TaskId : " + identityLink.getTaskId());
System.out.println(" GroupId : " + identityLink.getGroupId());
System.out.println(" UserId : " + identityLink.getUserId());
System.out.println(" ------ ");
}
return list;
}
@Override
public List<Task> getNeedToDealTasksByCandidate(String userId) {
if (null == userId || userId.length() == 0) throw new NullPointerException("userId is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> list =
taskService.createTaskQuery().taskCandidateUser(userId).orderByTaskCreateTime().desc().list();
System.out.println(" >>>>>> " + userId + " : NeedToDealTasksByCandidate : " + list.size() + " <<<<<< ");
for (int i = 0; i < list.size(); i++) {
Task task = list.get(i);
System.out.println("TaskId : " + task.getId());
System.out.println("TaskName : " + task.getName());
System.out.println("Assignee : " + task.getAssignee());
System.out.println("CreateTime : " + DateUtils.dateToStr(task.getCreateTime()));
System.out.println("ClaimTime : " + DateUtils.dateToStr(task.getClaimTime()));
System.out.println(" ------ ");
}
return list;
}
/**
* @Description 根据办理人查询待办任务
*/
@Override
public List<Task> getNeedToDealTasksByAssignee(String userId) {
if (null == userId || userId.length() == 0) throw new NullPointerException("userId is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> list =
taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
System.out.println(" >>>>>> " + userId + " : NeedToDealTasksByAssignee : " + list.size() + " <<<<<< ");
for (int i = 0; i < list.size(); i++) {
Task task = list.get(i);
System.out.println("TaskId : " + task.getId());
System.out.println("TaskName : " + task.getName());
System.out.println("Assignee : " + task.getAssignee());
System.out.println("CreateTime : " + DateUtils.dateToStr(task.getCreateTime()));
System.out.println("ClaimTime : " + DateUtils.dateToStr(task.getClaimTime()));
System.out.println(" ------ ");
}
return list;
}
/**
* @Description 根据办理人获取已办列表
*/
@Override
public List<HistoricTaskInstance> getHisDoneTaskListByAssignee(String assignee) {
if (null == assignee || assignee.length() == 0) throw new NullPointerException("assignee is null!");
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
HistoryService hisService = processEngine.getHistoryService();
List<HistoricTaskInstance> list = hisService.createHistoricTaskInstanceQuery().taskAssignee(assignee).
finished().orderByHistoricTaskInstanceEndTime().desc().list();
System.out.println(" >>>>>>" + assignee + " : getHisDoneTaskListByAssignee : " + list.size() + " <<<<<<");
for (int i = 0; i < list.size(); i++) {
HistoricTaskInstance historicTaskInstance = list.get(i);
System.out.println(" >>>>>> his: " + String.valueOf(i + 1) + " <<<<<< ");
System.out.println("ProcessInstanceId : " + historicTaskInstance.getProcessInstanceId());
System.out.println("TaskId : " + historicTaskInstance.getId());
System.out.println("TaskName : " + historicTaskInstance.getName());
System.out.println("Assignee : " + historicTaskInstance.getAssignee());
System.out.println("CreateTime : " + DateUtils.dateToStr(historicTaskInstance.getCreateTime()));
System.out.println("StartTime : " + DateUtils.dateToStr(historicTaskInstance.getStartTime()));
System.out.println("ClaimTime : " + DateUtils.dateToStr(historicTaskInstance.getClaimTime()));
System.out.println("EndTime : " + DateUtils.dateToStr(historicTaskInstance.getEndTime()));
}
return list;
}
// 历史业务
/**
* @Description 根据任务实例 ID 查询已完成的用户任务节点
*/
@Override
public List<HistoricActivityInstance> getHisCompletedNodes(String instanceId) {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 获取流程历史中已执行节点,并按照节点在流程中执行先后顺序排序
List<HistoricActivityInstance> historicActivityInstanceList =
processEngine.getHistoryService().createHistoricActivityInstanceQuery()
.processInstanceId(instanceId)
.activityType("userTask")//用户任务
.finished()//已经执行的任务节点
.orderByHistoricActivityInstanceEndTime()
.asc()
.list();
for (int i = 0; i < historicActivityInstanceList.size(); i++) {
HistoricActivityInstance historicActivityInstance = historicActivityInstanceList.get(i);
System.out.println(" >>>>>> 已办理完成的历史节点 " + i + " <<<<<< ");
System.out.println("ActivityId : " + historicActivityInstance.getActivityId());
System.out.println("ActivityName : " + historicActivityInstance.getActivityName());
System.out.println("ActivityType : " + historicActivityInstance.getActivityType());
System.out.println("Assignee : " + historicActivityInstance.getAssignee());
System.out.println("StartTime : " + DateUtils.dateToStr(historicActivityInstance.getStartTime()));
System.out.println("EndTime : " + DateUtils.dateToStr(historicActivityInstance.getEndTime()));
System.out.println("TaskId : " + historicActivityInstance.getTaskId());
System.out.println("ExecutionId : " + historicActivityInstance.getExecutionId());
}
return historicActivityInstanceList;
}
画个流程测试下,流程图如下:
单一网关的判断条件:
条件分支的判断条件:
对其中的环节 3 配置了上面的示例监听器
测试方法,部署流程
//部署流程 @Test public void testDeploy() { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service .createDeployment()//创建一个部署对象 .name("请假流程")//添加部署名称 .category("测试流程类别") .addClasspathResource("processes/MyProcess.bpmn20.xml")//从classpath的资源中加载,一次只能加载一个文件 .deploy();//完成部署 System.out.println("部署ID:" + deployment.getId()); System.out.println("部署名称:" + deployment.getName()); }
测试方法,启动流程并不断执行任务,知道流程办结
@Test public void tst() { String processDefineId = "Process_1qa45id"; Map params = new HashMap(); params.put("type", "1"); params.put("level5", "1"); String instanceId = activitiService.startProcess(processDefineId, "测试流程实例1", params); System.out.println("流程启动成功,实例 ID:" + instanceId); activitiService.getInstanceParams(instanceId); while (!activitiService.isProcessFinish(instanceId)) { dealOneLevel(instanceId); } System.out.println(activitiService.isProcessFinish(instanceId) ? "已办结" : "未办结"); } public void dealOneLevel(String instanceId) { System.out.println(""); List<Task> taskList = activitiService.getCurrentNode(instanceId); for (int i = 0; i < taskList.size(); i++) { Task task = taskList.get(i); System.out.println(" <------ 环节办理 ------> "); System.out.println("当前环节ID:" + task.getId()); System.out.println("当前环节名称:" + task.getName()); System.out.println("流程开始时间:" + DateUtils.dateToStr(task.getCreateTime())); System.out.println("受理人/办理人:" + task.getAssignee()); System.out.println("委托人:" + task.getOwner()); activitiService.completeNode(task.getId(), new HashMap()); } }
流程的执行过程:
流程启动成功,实例 ID:800001 <------ 实例中的变量 ------> level5 1 type 1 <------ 环节办理 ------> 当前环节ID:800009 当前环节名称:环节1 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节1办理人 委托人:null <------ 环节办理 ------> 当前环节ID:800016 当前环节名称:环节2.1 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节2办理人1 委托人:null <------ 环节办理 ------> 当前环节ID:800019 当前环节名称:环节2.2 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节2办理人2 委托人:null <------ 环节办理 ------> 当前环节ID:800022 当前环节名称:环节2.3 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节2.3办理人 委托人:null <------ 环节办理 ------> 当前环节ID:800025 当前环节名称:环节2.4 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节2.4办理人 委托人:null <------ 环节办理 ------> 当前环节ID:800032 当前环节名称:环节3 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节3办理人 委托人:null >>>>>> complete <<<<<< creatTime: 2022-10-26 16:49:38 processDefinitionId: Process_1qa45id:11:797504 processInstanceId: 800001 executionId: 800014 taskId: 800032 taskName: 环节3 description: null Priority 0-100: 50 <------ 环节办理 ------> 当前环节ID:800036 当前环节名称:环节4.1 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节4.1办理人 委托人:null <------ 环节办理 ------> 当前环节ID:800039 当前环节名称:环节5 流程开始时间:2022-10-26 16:49:38 受理人/办理人:环节5办理人 委托人:null 已办结
当你看清人们的真相,于是你知道了,你可以忍受孤独