Setting parameters:
public class ProcessVariablesTest { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /**Deployment process definition (from InputStream)*/ @Test public void deploymentProcessDefinition_inputStream(){ InputStream inputStreambpmn = this.getClass().getResourceAsStream("/process/processVariables.bpmn"); InputStream inputStreampng = this.getClass().getResourceAsStream("/process/processVariables.png"); Deployment deployment = processEngine.getRepositoryService()//Service related to process definition and deployment objects .createDeployment()//Create a deployment object .name("Process definition")//Add the name of the deployment .addInputStream("processVariables.bpmn", inputStreambpmn)//Use the name of the resource file (requirement: the same as the name of the resource file) and complete the deployment with the input stream .addInputStream("processVariables.png", inputStreampng)//Use the name of the resource file (requirement: the same as the name of the resource file) and complete the deployment with the input stream .deploy();//Complete deployment System.out.println("deploy ID: "+deployment.getId());// System.out.println("Deployment name:"+deployment.getName());// } /**Start process instance*/ @Test public void startProcessInstance(){ //key of process definition String processDefinitionKey = "processVariables"; ProcessInstance pi = processEngine.getRuntimeService()//Service related to executing process instances and executing objects .startProcessInstanceByKey(processDefinitionKey);//Start the process instance with the key defined by the process. The key corresponds to the attribute value of id in the helloworld.bpmn file. Start with the key value. By default, start according to the latest version of the process definition. System.out.println("Process instance ID:"+pi.getId());//Process instance ID System.out.println("Process definition ID:"+pi.getProcessDefinitionId());//Process Definition ID } /**Setting process variables*/ @Test public void setVariables(){ /**With tasks (ongoing)*/ TaskService taskService = processEngine.getTaskService(); //Task ID String taskId = "2104"; /**First: Setting process variables and using basic data types*/ // taskService.setVariableLocal(taskId,'days off', 5); //Binding to task ID // taskService.setVariable(taskId,'leave date', new Date()); // taskService.setVariable(taskId,'cause of leave','Go home and visit relatives, have a meal together'); /**2: Setting process variables, using javabean type*/ /** * When a javabean (implementation serial number) is placed in a process variable, the attributes of the javabean are required not to change again * * If a change occurs, when retrieved, an exception is thrown * * Solution: Add: * private static final long serialVersionUID = 6757393795687480331L; * Simultaneously implement Serializable * */ Person p = new Person(); p.setId(20); p.setName("Cui Hua"); taskService.setVariable(taskId, "Personnel information(Add Fixed Version)", p); System.out.println("Success in setting process variables!"); } /**Getting process variables*/ @Test public void getVariables(){ /**With tasks (ongoing)*/ TaskService taskService = processEngine.getTaskService(); //Task ID String taskId = "2104"; /**First: Get process variables and use basic data types*/ // Integer days = Integer taskService. getVariable (taskId, number of days off); // Date date = (Date) taskService.getVariable(taskId, "leave date"); // String resean = (String) taskService.getVariable(taskId, "reasons for leave"); // System.out.println("days of leave:"+days); // System.out.println("leave date:"+date); // System.out.println("Leave reasons:"+resean); /**Second: Get process variables, using javabean type*/ Person p = (Person)taskService.getVariable(taskId, "Personnel information(Add Fixed Version)"); System.out.println(p.getId()+" "+p.getName()); } /**Simulate scenarios for setting and acquiring process variables*/ public void setAndGetVariables(){ /**With process instances, execution objects (executing)*/ RuntimeService runtimeService = processEngine.getRuntimeService(); /**With tasks (ongoing)*/ TaskService taskService = processEngine.getTaskService(); /**Setting process variables*/ // RuntimeService. setVariable (execution Id, variableName, value) // denotes setting the value of the process variable (only one value at a time) using the execution object ID and the name of the process variable. // RuntimeService. setVariables (execution Id, variables)// denotes the use of execution object ID and the setting of process variables with Map sets. The key of map sets is the name of process variables, and the value of map sets is the value of process variables (setting multiple values at a time). // taskService.setVariable(taskId, variableName, value) // indicates the use of task ID, and the name of the process variable, setting the value of the process variable (only one value at a time) // taskService.setVariables(taskId, variables)// denotes setting process variables with task ID and Map set. The key of map set is the name of process variables, and the value of map set is the value of process variables (setting multiple values at a time). // RuntimeService.startProcessInstanceByKey (process Definition Key, variables); //Start process instances at the same time, you can set process variables, using Map sets // taskService.complete(taskId, variables) // / While completing the task, set the process variables and use the Map set /**Getting process variables*/ // runtimeService.getVariable(executionId, variableName); // Get the value of the process variable using the execution object ID and the name of the process variable // RuntimeService. getVariables (execution Id); // Use the execution object ID to get all process variables and place them in the Map set. The key of the map set is the name of the process variables, and the value of the map set is the value of the process variables. // RuntimeService.getVariables (execution Id, variableNames); //Use the execution object ID to get the value of the process variable, store the name of the process variable in the collection by setting the name of the process variable, get the value of the process variable with the name of the specified process variable, and store the value in the Map collection. // taskService.getVariable(taskId, variableName); // Get the value of the process variable using the task ID and the name of the process variable // taskService.getVariables(taskId); // Use task ID to get all process variables and place them in Map set. The key of map set is the name of process variables, and the value of map set is the value of process variables. // taskService.getVariables(taskId, variableNames); //Use task ID to get the value of process variables, store the name of process variables in the collection by setting the name of process variables, get the value of process variables with the name of specified process variables, and store the value in the Map collection. } /**Complete my task*/ @Test public void completeMyPersonalTask(){ //Task ID String taskId = "2402"; processEngine.getTaskService()//Service related to ongoing task management .complete(taskId); System.out.println("Completing tasks: tasks ID: "+taskId); } /**Query the history table of process variables*/ @Test public void findHistoryProcessVariables(){ List<HistoricVariableInstance> list = processEngine.getHistoryService()// .createHistoricVariableInstanceQuery()//Create a historical process variable query object .variableName("Days of leave") .list(); if(list!=null && list.size()>0){ for(HistoricVariableInstance hvi:list){ System.out.println(hvi.getId()+" "+hvi.getProcessInstanceId()+" "+hvi.getVariableName()+" "+hvi.getVariableTypeName()+" "+hvi.getValue()); System.out.println("###############################################"); } } } }
Explain:
- The scope of process variables is process instances, so just set them up, regardless of the stage.
- The basic type sets process variables, uses task ID in task service, defines the name of process variables, and sets the value of process variables.
- Javabean type sets process variables, requiring this JavaBean to implement the Serializable interface
- When setting process variables, add data to act_ru_variable table
- The acquisition of process variables is different for each process instance (i.e. one process).
- Use basic types to get process variables, use task ID in task service, name of process variables, and get the value of process variables.
- Javabean type settings acquire process variables. In addition to the need for this JavaBean to implement the Serializable interface, it also requires that the properties of the process variable object cannot be changed, otherwise an exception is thrown. Solution, Fixed Serialization ID
public class HistoryQueryTest { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); /**Examples of query history process*/ @Test public void findHistoryProcessInstance(){ String processInstanceId = "2101"; HistoricProcessInstance hpi = processEngine.getHistoryService()//Service related to historical data (history table) .createHistoricProcessInstanceQuery()//Create historical process instance queries .processInstanceId(processInstanceId)//Use process instance ID to query .orderByProcessInstanceStartTime().asc() .singleResult(); System.out.println(hpi.getId()+" "+hpi.getProcessDefinitionId()+" "+hpi.getStartTime()+" "+hpi.getEndTime()+" "+hpi.getDurationInMillis()); } /**Inquiry History Activities*/ @Test public void findHistoryActiviti(){ String processInstanceId = "2101"; List<HistoricActivityInstance> list = processEngine.getHistoryService()// .createHistoricActivityInstanceQuery()//Queries for Creating Historical Activity Instances .processInstanceId(processInstanceId)// .orderByHistoricActivityInstanceStartTime().asc()// .list(); if(list!=null && list.size()>0){ for(HistoricActivityInstance hai:list){ System.out.println(hai.getId()+" "+hai.getProcessInstanceId()+" "+hai.getActivityType()+" "+hai.getStartTime()+" "+hai.getEndTime()+" "+hai.getDurationInMillis()); System.out.println("#####################"); } } } /**Query History Task*/ @Test public void findHistoryTask(){ String processInstanceId = "2101"; List<HistoricTaskInstance> list = processEngine.getHistoryService()//Service related to historical data (history table) .createHistoricTaskInstanceQuery()//Create historical task instance queries .processInstanceId(processInstanceId)// .orderByHistoricTaskInstanceStartTime().asc() .list(); if(list!=null && list.size()>0){ for(HistoricTaskInstance hti:list){ System.out.println(hti.getId()+" "+hti.getName()+" "+hti.getProcessInstanceId()+" "+hti.getStartTime()+" "+hti.getEndTime()+" "+hti.getDurationInMillis()); System.out.println("################################"); } } } /**Query history process variables*/ @Test public void findHistoryProcessVariables(){ String processInstanceId = "2101"; List<HistoricVariableInstance> list = processEngine.getHistoryService()// .createHistoricVariableInstanceQuery()//Create a historical process variable query object .processInstanceId(processInstanceId)// .list(); if(list!=null && list.size()>0){ for(HistoricVariableInstance hvi:list){ System.out.println(hvi.getId()+" "+hvi.getProcessInstanceId()+" "+hvi.getVariableName()+" "+hvi.getVariableTypeName()+" "+hvi.getValue()); System.out.println("###############################################"); } } } }