Activitti工作流流程变量
1、什么是流程变量?如请假超过三天需要总经理审批。流程运行中需要动态改变流程时就需要通过一个变量来控制流程走向,这就是流程变量。
2、POJO要存入流程变量需要实现序列化接口,设置serializable接口和设置serialVersionUID
3、流程变量作用域:流程实例(全局的)、任务实例(local变量)、一个执行实例。
4、流程变量用法,通过assign来设置UEL表达式,在连线上设置condition:${holiday>8}
实战案例,部门员工创建请假单,部门经理审核,请假小于3天,人事直接存档,请假大于3天,总经理审核后,人事存档
package com.xiaofeiyang.activiti.processvariables; import org.activiti.engine.*; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.task.Task; import java.util.*; /** * @author: yangchun * @description: * @date: Created in 2020-06-09 10:58 */ public class ProcessVariablesTest { public static void main(String[] args) { //deploy(); //startProcess(); completeTask("shengqin"); //deleteDeploy(); } public static void deploy(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("processvariables.bpmn") .addClasspathResource("processvariables.png").name("processvariables") .deploy(); } // 流程变量存在act_ru_variables,act_get_bytearray public static void startProcess(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map<String,Object> variables = new HashMap<>(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); variables.put("holiday",holiday); variables.put("assign0","yangchun"); variables.put("assign1","dingji"); variables.put("assign2","sparky"); variables.put("assign3","shengqin"); runtimeService.startProcessInstanceByKey("processvariable","1",variables); } public static void completeTask(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("processvariable").taskAssignee(name).singleResult(); if(task!=null){ taskService.complete(task.getId()); } } public static void deleteDeploy(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().processDefinitionKey("processvariable"). orderByProcessDefinitionVersion().desc().list(); for(ProcessDefinition processDefinition:processDefinitions){ System.out.println(processDefinition.getDeploymentId()); System.out.println(processDefinition.getId()); System.out.println(processDefinition.getName()); System.out.println(processDefinition.getKey()); //第二个参数强制删除未完成的流程 repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true); } } }
改进版本
package com.xiaofeiyang.activiti.processvariables; import org.activiti.engine.*; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import java.util.*; /** * @author: yangchun * @description: * @date: Created in 2020-06-09 10:58 */ public class ProcessVariablesTest { public static void main(String[] args) { //deploy(); //startProcess(); //completeTask("dingji"); //completeTaskVariables("dingji"); //deleteDeploy(); } public static void deploy(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("processvariables.bpmn") .addClasspathResource("processvariables.png").name("processvariables") .deploy(); } // 流程变量存在act_ru_variables,act_get_bytearray public static void startProcess(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map<String,Object> variables = new HashMap<>(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); //variables.put("holiday",holiday); variables.put("assign0","yangchun"); variables.put("assign1","dingji"); variables.put("assign2","sparky"); variables.put("assign3","shengqin"); runtimeService.startProcessInstanceByKey("processvariable","1",variables); } public static void completeTask(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("processvariable").taskAssignee(name).singleResult(); if(task!=null){ taskService.complete(task.getId()); } } public static void completeTaskVariables(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("processvariable").taskAssignee(name).singleResult(); if(task!=null){ Map<String,Object> variables = new HashMap<>(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); variables.put("holiday",holiday); taskService.complete(task.getId(),variables); } } public static void deleteDeploy(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().processDefinitionKey("processvariable"). orderByProcessDefinitionVersion().desc().list(); for(ProcessDefinition processDefinition:processDefinitions){ System.out.println(processDefinition.getDeploymentId()); System.out.println(processDefinition.getId()); System.out.println(processDefinition.getName()); System.out.println(processDefinition.getKey()); //第二个参数强制删除未完成的流程 repositoryService.deleteDeployment(processDefinition.getDeploymentId(),true); } } public static void processInstanceVariables(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); List<ProcessInstance> processInstances = runtimeService.createProcessInstanceQuery().processDefinitionKey("processvariable") .list(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); for(ProcessInstance processInstance:processInstances){ if(processInstance.isEnded()){ continue; } runtimeService.setVariable(processInstance.getId(),"holiday",holiday); // Map<String,Object> variables = new HashMap<>(); // variables.put("holiday",holiday); // runtimeService.setVariables(processInstance.getId(),variables); } } public static void taskVariables(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); List<Task> taskList = taskService.createTaskQuery().processDefinitionKey("processvariable") .list(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); for(Task task:taskList){ taskService.setVariable(task.getId(),"holiday",holiday); // Map<String,Object> variables = new HashMap<>(); // variables.put("holiday",holiday); // taskService.setVariables(task.getId(),variables); } } }
5、局部变量局部变量只在任务没有结束时有用。只作用于当前节点。
6、Cadidate-user使用场景就是有几个人具体做这个工作,也就是组任务,
1、查询组任务
2、拾取任务,将组任务变成个人任务
3、查询个人任务
4、办理任务
package com.xiaofeiyang.activiti.cadidate; import org.activiti.engine.*; import org.activiti.engine.repository.ProcessDefinition; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.task.Task; import java.util.*; /** * @author: yangchun * @description: * @date: Created in 2020-06-09 10:58 */ public class ProcessVariablesTest { public static void main(String[] args) { //deploy(); //startProcess(); //queryCandidateTask("yangchun"); // assignCandidateTask("yangchun"); //returnCandidateTask("yangchun"); transCandidateTask("yangchun","lidan"); //completeTask("dingji"); //completeTaskVariables("dingji"); //deleteDeploy(); } public static void deploy(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RepositoryService repositoryService = processEngine.getRepositoryService(); repositoryService.createDeployment().addClasspathResource("candidate.bpmn") .addClasspathResource("candidate.png").name("candidate") .deploy(); } // 流程变量存在act_ru_variables,act_get_bytearray public static void startProcess(){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); RuntimeService runtimeService = processEngine.getRuntimeService(); Map<String,Object> variables = new HashMap<>(); Holiday holiday = new Holiday(); holiday.setBegin(new Date()); Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_YEAR,3); holiday.setEnd(calendar.getTime()); holiday.setHolidayName("请假回家"); holiday.setId(1); holiday.setNum(2.5f); holiday.setReason("请假回家玩"); holiday.setType("调休假"); variables.put("holiday",holiday); List<String> user = Arrays.asList(new String[]{"yangchun","lidan"}); List<String> user1 = Arrays.asList(new String[]{"yuyuping","yangzhongming"}); List<String> user2 = Arrays.asList(new String[]{"xiakexiu","likefu"}); List<String> user3 = Arrays.asList(new String[]{"yanghuan","zhangwei"}); variables.put("users0",user); variables.put("users1",user1); variables.put("users2",user2); variables.put("users3",user3); runtimeService.startProcessInstanceByKey("cadidate","1",variables); } public static void queryCandidateTask(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("cadidate").taskCandidateUser(name).singleResult(); System.out.println(task.getId()); System.out.println(task.getAssignee()); } public static void assignCandidateTask(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("cadidate").taskCandidateUser(name).singleResult(); taskService.setAssignee(task.getId(),"yangchun"); System.out.println(task.getId()); System.out.println(task.getAssignee()); } public static void returnCandidateTask(String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("cadidate").taskAssignee(name).singleResult(); taskService.setAssignee(task.getId(), null); System.out.println(task.getId()); System.out.println(task.getAssignee()); } public static void transCandidateTask(String name1,String name){ ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); TaskService taskService = processEngine.getTaskService(); Task task = taskService.createTaskQuery().processDefinitionKey("cadidate").taskAssignee(name1).singleResult(); taskService.setAssignee(task.getId(), name); task = taskService.createTaskQuery().processDefinitionKey("cadidate").taskAssignee(name).singleResult(); System.out.println(task.getId()); System.out.println(task.getAssignee()); } }