代码
/** * 通过流程变量指定办理人 * * @author Jamin * @date 2019年7月17日下午4:18:30 * @return void */ @org.junit.Test public void test() { // 部署 Deployment deploy = repositoryService.createDeployment() .addClasspathResource("personVariables/personVariables.bpmn") .addClasspathResource("personVariables/personVariables.png").deploy(); // 启动并设定流程变量,指定组办理人 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("userId", "张三"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("personVariables", map); System.out.println("流程实例:" + processInstance.getId()); // 查询个人任务 List<Task> list = taskService.createTaskQuery().taskAssignee("张三").list(); for (Task task : list) { System.out.println(task.getId()); System.out.println(task.getName()); System.out.println(task.getExecutionId()); System.out.println(task.getAssignee()); } }
通过类指定办理人
创建一个实现了TaskListener的类
package com.cjrj.bootactividemo; import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.TaskListener; /** * 实现TaskListener接口完成指定办理人 * * @author Jamin * @date 2019年7月17日 下午4:23:30 * */ public class TaskListenerImpl implements TaskListener { @Override public void notify(DelegateTask delegateTask) { // 指定办理人 String person = "张三"; // 设置办理人 delegateTask.setAssignee(person); } }bpmn图绑定监听
测试代码
/** * 通过类指定办理人 * * @author Jamin * @date 2019年7月17日下午4:20:31 * @return void */ @org.junit.Test public void test2() { TaskListenerImpl taskListenerImpl = new TaskListenerImpl(); // 部署 Deployment deploy = repositoryService.createDeployment() .addClasspathResource("personVariables/personClass.bpmn") .addClasspathResource("personVariables/personClass.png").deploy(); // 启动并设定流程变量,指定组办理人 HashMap<String, Object> map = new HashMap<String, Object>(); map.put("userId", "张三"); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("personVariables", map); System.out.println("流程实例:" + processInstance.getId()); // 查询个人任务 Task task = taskService.createTaskQuery().taskAssignee("张三") .processInstanceId(processInstance.getId()).singleResult(); System.out.println("张三任务:" + task.getName()); // 转交任务 taskService.setAssignee(task.getId(), "李四"); Task task1 = taskService.createTaskQuery().taskAssignee("李四") .processInstanceId(processInstance.getId()).singleResult(); System.out.println("李四的任务:" + task1.getName()); }
组任务
直接指定
使用流程变量
使用类
代码
package com.cjrj.bootactividemo; import java.util.List; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricIdentityLink; import org.activiti.engine.repository.Deployment; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.IdentityLink; import org.activiti.engine.task.Task; /** * 组任务 * * @author Jamin * @date 2019年7月17日 下午5:12:21 * */ public class Test7 { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); RuntimeService runtimeService = processEngine.getRuntimeService(); TaskService taskService = processEngine.getTaskService(); /** * 直接指定 * * @author Jamin * @date 2019年7月17日下午5:13:02 * @return void */ @org.junit.Test public void test() { // 第一种 /* * // 部署 repositoryService.createDeployment().addClasspathResource("group/first.bpmn") * .addClasspathResource("group/first.png").deploy(); // 启动 ProcessInstance processInstance = * runtimeService.startProcessInstanceByKey("first"); System.out.println("流程实例id:" + * processInstance.getId()); */ // 第二种 /* * Deployment deploy = * repositoryService.createDeployment().addClasspathResource("group/second.bpmn") * .addClasspathResource("group/second.png").deploy(); HashMap<String, Object> map = new * HashMap<String, Object>(); map.put("userId", "张三,李四,王五"); ProcessInstance processInstance = * runtimeService.startProcessInstanceByKey("second", map); */ // 第三种 Deployment deploy = repositoryService.createDeployment() .addClasspathResource("group/third.bpmn").addClasspathResource("group/third.png").deploy(); ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("third"); System.out.println("流程实例id" + processInstance.getId()); // 查询组任务 List<Task> list = taskService.createTaskQuery().processInstanceId(processInstance.getId()) .taskCandidateUser("张三").list(); System.out.println("组任务:" + list); String taskId = null; for (Task task : list) { System.out.println(task.getName()); System.out.println(task.getAssignee()); System.out.println(task.getId()); taskId = task.getId(); } // 查询组成员 List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(taskId); for (IdentityLink identityLink : identityLinksForTask) { System.out.println(identityLink.getGroupId()); System.out.println(identityLink.getUserId()); } // 查询历史组成员列表 List<HistoricIdentityLink> historicIdentityLinksForTask = processEngine.getHistoryService().getHistoricIdentityLinksForTask(taskId); for (HistoricIdentityLink historicIdentityLink : historicIdentityLinksForTask) { System.out.println(historicIdentityLink.getUserId()); System.out.println(historicIdentityLink.getGroupId()); } // 组任务转个人可以是任何人 taskService.claim(taskId, "张11"); Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()) .taskAssignee("张11").singleResult(); System.out.println("组任务转个人任务名:" + task.getName()); // 个人任务转组(必须任务以前是组任务) taskService.setAssignee(task.getId(), null); // 检测是否转组任务成功, List<Task> list1 = taskService.createTaskQuery().processInstanceId(processInstance.getId()) .taskCandidateUser("张三").list(); String taskId1 = null; for (Task task1 : list) { System.out.println("转组任务:" + task1.getName()); System.out.println("转组任务:" + task1.getAssignee()); System.out.println("转组任务:" + task1.getId()); taskId1 = task.getId(); } // 添加人员 taskService.addCandidateUser(taskId1, "qiqi"); // 查询参与人员 List<IdentityLink> identityLinksForTask2 = taskService.getIdentityLinksForTask(taskId1); for (IdentityLink identityLink : identityLinksForTask2) { System.out.println("添加人员后" + identityLink.getUserId()); } // 删除人员张三 taskService.deleteCandidateUser(taskId1, "张三"); List<IdentityLink> identityLinksForTask3 = taskService.getIdentityLinksForTask(taskId1); for (IdentityLink identityLink : identityLinksForTask3) { System.out.println("删除人员后:" + identityLink.getUserId()); } // 完成任务 taskService.complete(taskId); } }
分配任务给一个角色
bpmn图