Activiti process variable and activiti Process
Process Variables play an important role in the entire workflow.
For example, some parameters in the leave process include the number of days for leave and the reason for leave. The scope of a process variable is a process instance. That is to say, the Process Variables of each process instance do not affect each other.
Add process variable
1. You can add process variables when starting a process instance.
Public void startProcess () throws Exception {String processDefinitionKey = "varTest"; // create the process variable Map <String, Object> variables = new HashMap <String, Object> (); variables. put ("", "Feng Xiaogang"); // set the process variable ProcessInstance pi = processEngine at startup. getRuntimeService (). startProcessInstanceByKey (processDefinitionKey, variables); System. out. println ("pid:" + pi. getId ());}
Note:
1) when starting a process instance, you can load the process variable by reloading startProcessInstanceByKey.
2) The second parameter must be of the Map <String, Object> type, meaning that multiple process variables can be added.
3) after the code is executed, two rows of records will be added to the database table act_ru_variable.
2. When handling a task
Sometimes, after a task is completed, some information must be transmitted to the system. In this case, you can use the TaskService class to add process instances.
Public void setVar () throws Exception {String processInstanceId = "1901"; Task task = taskService. createTaskQuery (). taskAssignee ("manager "). processInstanceId (processInstanceId ). singleResult (); // String taskId/*** set the process variable * // taskService. setVariable (taskId, variableName, value) // set a flow variable Map <String, Object> variables = new HashMap <> (); // variables. put ("leave time", "2013-11-22"); // variables. pu T ("days for leave", "3"); variables. put ("days for leave", 3); taskService. setVariables (task. getId (), variables);/*** set the process variable when the task is completed * // taskService. complete (taskId) // Map <String, Object> variables = new HashMap <> (); // variables. put ("reason for asking for leave", "appointment !! "); // TaskService. complete (task. getId (), variables );}
Note:
1) Use the setVariables method to add a batch of process variables during task handling.
2) use the setVariable method to add a process variable during task handling.
3) TaskService has an overloaded complete method.
3. Set process variables directly on Execution when executing a process instance
Because the scope of the process variables is the process instance, you only need to set it, regardless of the stage.
Public void setVarByExecution () throws Exception {String processInstanceId = "1901"; Task task = taskService. createTaskQuery (). taskAssignee ("manager "). processInstanceId (processInstanceId ). singleResult (); // String taskId/*** set the process variable * // taskService. setVariable (taskId, variableName, value) // set a flow variable Map <String, Object> variables = new HashMap <> (); variables. put ("annotation", "Good appointment, pay attention to safety !! "); RuntimeService. setVariables (task. getExecutionId (), variables);/*** set the process variable when the task is completed * // taskService. complete (taskId) // Map <String, Object> variables = new HashMap <> (); // variables. put ("reason for asking for leave", "appointment !! "); // TaskService. complete (task. getId (), variables );}
Another way to set variables:
Public void setVarByObj () throws Exception {String processInstanceId = "1901"; Task task = taskService. createTaskQuery (). taskAssignee ("manager "). processInstanceId (processInstanceId ). singleResult (); Form form = new Form (); // this javabean implements the Serializable interface.
Form. setName ("form name ");
Form. setContent ("I am Michael, I want to ask for leave for 3 days"); taskService. setVariable (task. getId (), "form", form );}
Get Process Variables
You can use runTimeService to obtain process variables.
Public void viewVar () throws Exception {String processInstanceId = "1901"; Task task = taskService. createTaskQuery (). taskAssignee ("user "). processInstanceId (processInstanceId ). singleResult (); System. out. println ("taskName:" + task. getName (); // String variableName = ""; // String val = (String) taskService. getVariable (task. getId (), variableName); Map <String, Object> vars = taskService. getVariables (task. getId (); for (String variableName: vars. keySet () {String val = (String) vars. get (variableName); System. out. println (variableName + "=" + val );}}
Note: These process variables are read from the act_ru_variable table.
How does ACTIVITI obtain the next node?
First, obtain the current task according to the process ID :??
Java code ????
List <Task ?? Tasks ?? = ?? TaskService. createTaskQuery (). processInstanceId (procInstanceId). list ();????
Then obtain the process definition of the current process according to the current task, and then obtain all the nodes according to the process definition :??
Java code ????
ProcessDefinitionEntity ?? Def ?? = ?? (ProcessDefinitionEntity )?? (RepositoryServiceImpl) rs). getDeployedProcessDefinition (task. getProcessDefinitionId ());???????? List <ActivityImpl ?? ActivitiList ?? = ?? Def. getActivities ();????
// Rs refers to the RepositoryService instance ???? Obtain the execution ID of the current process, the execution instance, and the ID of the current process node according to the task :??
Java code ????
String ?? ExcId ?? = ?? Task. getExecutionId ();????
ExecutionEntity ?? Execution ?? = ?? (ExecutionEntity )?? Runtimeservice.createexecutionquery(cmd.exe cutionId (excId). singleResult ();????
String ?? ActivitiId ?? = ?? Execution. getActivityId ();????
Then cyclically activitiList and judge the nodes in the current process. Then, obtain the current node instance, obtain all the paths from the current node based on the node instance, and then obtain the next node instance according to the path :??
Java code ???? For (ActivityImpl ?? ActivityImpl: activitiList ){????
String ?? Id ?? = ?? ActivityImpl. getId ();???? If (activitiId. equals (id )){????
System. out. println (
"Current task :"
+ ActivityImpl. getProperty ("name "));?? // Output a certain attribute of a node ???? List <PvmTransition ?? OutTransitions ?? = ?? ActivityImpl. getOutgoingTransitions ();
// Obtain all lines from a node ???? For (PvmTransition ?? Tr: outTransitions ){????
PvmActivity ?? Ac ?? = ?? Tr. getDestination ();??
// Obtain the End Node of the line ???? System. out. println (
"Next task :"
How does activiti terminate a process instance?
RuntimeService. suspendProcessInstanceById (processInstanceId) suspends the process instance. That is, freeze and pause. It can also be activated