Activiti process instance management and activiti process instances
1. Start the process After completing the process definition deployment, start the process instance.
/*** 1 Start Process ** when the process reaches a node, one piece of data will be generated in the act_ru_execution table * if the current node is a user task node, in this case, the act_ru_task table will generate one piece of data (the task handler, the task creation time) */@ Test public void startProcess () throws Exception {/*** v1 myProcess: 2: 604 * v2 myProcess: 3: 704 * // runtimeService. startProcessInstanceById (processDefinitionId) // start the process with the key defined in the process. The process with the highest version ProcessInstance pi = runtimeService is started. startProcessInstanceByKey ("LeaveFlow"); System. out. println ("pid:" + pi. getId () + ", activitiId:" + pi. getActivityId ());}
2. view the process status
Public void queryProcessState () throws Exception {String processInstanceId = "1401"; // query process instance ProcessInstance pi = runtimeService by process instance ID. createProcessInstanceQuery (). processInstanceId (processInstanceId ). singleResult (); if (pi! = Null) {System. out. println ("the current process is in:" + pi. getActivityId ();} else {System. out. println ("the process has ended !! ");}}
3. view public tasks
Queries public tasks that can be accessed by a specified user.
Public void queryCommonTask () throws Exception {// create a task query object TaskQuery taskQuery = taskService. createTaskQuery (); // configure the query object String candidateUser = "zhangsan"; taskQuery // filter condition. taskCandidateUser (candidateUser) // paging condition //. listPage (firstResult, maxResults) // sorting condition. orderByTaskCreateTime (). desc (); // execute the query List <Task> tasks = taskQuery. list (); System. out. println ("=================================public task list for [" + candidateUser + "] ====== ============== "); for (Task task: tasks) {System. out. print ("id:" + task. getId () + ","); System. out. print ("name:" + task. getName () + ","); System. out. print ("createTime:" + task. getCreateTime () + ","); System. out. println ("assignee:" + task. getAssignee ());}}
Note:
1. The Query Task first uses TaskService to create a TaskQuery object.
2. On the query object, add the taskCandidateUser filter condition to indicate that the filtering task is selected as its own task.
3. Call the list method to return the list of accessible tasks of the specified user.
4. The assignee attribute of all public tasks is null.
4. view private tasks
Queries Incomplete personal tasks of the specified user.
Public void queryPersonalTask () throws Exception {// create a task query object TaskQuery taskQuery = taskService. createTaskQuery (); // configure the query object // String assignee = "user"; String assignee = ""; taskQuery // filter condition. taskAssignee (assignee) // paging condition //. listPage (firstResult, maxResults) // sorting condition. orderByTaskCreateTime (). desc (); // execute the query List <Task> tasks = taskQuery. list (); System. out. println ("=============================== [" + assignee + "] ============== "); for (Task task: tasks) {System. out. print ("id:" + task. getId () + ","); System. out. print ("name:" + task. getName () + ","); System. out. print ("createTime:" + task. getCreateTime () + ","); System. out. println ("assignee:" + task. getAssignee ());}}
Note:
1) because it is a task query, TaskService should be obtained from processEngine
2) use TaskService to obtain the task query object TaskQuery
3) add query filtering conditions for query objects. Use taskAssignee to specify candidates for the task (that is, query the agent tasks of the specified user) and add filtering conditions such as paging sorting.
4) Call the list method to perform the query and return the task list of the specified user as the handler.
5) The task ID, name, handler, and creation time can be found in the act_ru_task table.
6) for details about Execution and ProcessInstance, see section 5.6. In this case, ProcessInstance is equivalent to Execution
7) if the assignee attribute is department manager, the result is blank. Because the current process is only in the "fill in leave application" stage, the subsequent tasks are not executed, that is, there are no tasks that the department manager can handle in the database, so the query is not available.
8) when a Task node and Execution node are one-to-one, Execution _ is used in the task object to indicate the relationship between them.
9) The task ID corresponds to the "ID _" column in the database table act_ru_task.
5. claim a task
Generally, a public task has more than one candidate. If you want to handle it, you must first claim the task, that is, turn yourself into the owner of the task.
Public void takeTask () throws Exception {// claim String taskId = "1404"; String userId = ""; // Let the user of the specified userId claim the task taskService of the specified taskId. claim (taskId, userId); System. out. println ("claim operation completed !! ");}
Note:
1. For task-related operations, first obtain taskService
2. Determine the ID of the claimed task and the ID of the claimed person
3. Call the claim (claim) method of taskService to convert a public task to a private task of the specified user.
6. Handle tasks
Public void complete () throws Exception {String taskId = "1602"; // complete task taskService. complete (taskId );}
Note:
1) For task handling, TaskService is obtained from ProcessEngine.
2) When the code is executed and the query is executed as an employee, no data is found at this time.
3) activiti will delete the completed task from the act_ru_task table, And the next task will be inserted.
4) search by department manager to check the results. Because the process is executed to the department manager for approval of this node.
5) execute the task code again. After the execution, query the task as a "department manager" without any results.
6) Repeat steps 3rd and 4 until the process is completed.
7. The verification process has been completed.
During the process, the ID of the created process instance does not change throughout the process. When the process ends, the process instance will be deleted.
Note:
1) because it is a query process instance, first obtain runtimeService
2) create a process history query object and set instance ID filter parameters
3) because a process instance ID only corresponds to one instance, a unique result is returned when singleResult is used for query. If the number of results is greater than 1, an exception is thrown.
4) check whether the instance with the specified ID exists. If the result is null, the process ends and the instance has been deleted.
How does activiti terminate a process instance?
RuntimeService. suspendProcessInstanceById (processInstanceId) suspends the process instance. That is, freeze and pause. It can also be activated
How can Activiti query the business objects associated with the process instance?
When you design your process xml, you have already set <activiti: formProperty id = "tablename" name = "table name" type = "string" required = "true" readable = "true" writable = "true"> </activiti: formProperty> put this item in xml. Then you can find this item and you will know which business table to query.