Workflow learning-Activiti process instance, task management, and activiti
I. Preface
In the previous article, we introduced Activiti process definition management. In this article, we continue to learn about Activiti process instances and task management.
Ii. Text
Process instance)
The relationship between a process instance (ProcessInstance) and a process definition (ProcessDefinition) in the previous article is a bit like the relationship between classes and instance objects, processDefinition is the description of the entire process step, and ProcessInstance is the maximum execution route from the beginning to the end of the process definition.
Execution object (Execution)
When ProcessInstance is mentioned, another term will appear, that is, the Execution object (Execution). Execution is the current route for Execution according to the ProcessDefinition rules,
If ProcessDefinition has only one Execution route, the Execution and ProcessInstance are exactly the same. If ProcessDefinition has multiple Execution routes, the Execution and ProcessInstance may be the same or different. Therefore, it is concluded that there can be only one ProcessInstance in a process, and multiple Execution instances can exist.
Task)
Tasks are the Task information generated when a process is executed in a certain step or stage.
In the previous article, we will introduce how to draw a flowchart and how to deploy the process definition. The relationship between the process definition and the process instance has just been introduced. Now we should start the process instance:
Code
Start Process instance
/*** START process instance */@ Testpublic void startProcessInstance () {// process-defined keyString processDefinitionKey = "HelloWorld"; ProcessInstance pi = processEngine. getRuntimeService () // The Service related to the ongoing process instance and execution object. startProcessInstanceByKey (processDefinitionKey); // use the key defined in the process to start the process instance. The key corresponds to HelloWorld. the value of the id attribute in the bpmn file, which is started with the key value. By default, the System is started according to the latest version of the process definition. out. println ("process instance ID:" + pi. getId (); System. out. println ("process definition ID:" + pi. getProcessDefinitionId ());}
Running result:
Process instance ID: 501
Process Definition ID: HelloWorld: 2: 404
Note:
1) insert a record in the execution object table in which the database act_ru_execution is being executed
2) Insert a record in the History Table of the database's act_hi_procinst instance
3) insert a record in the History Table of the database's act_hi_actinst Active Node
4) the nodes in the figure are all task nodes, so a record will be added to the History Table of the act_ru_task process instance.
5) insert a record in the database's act_hi_taskinst task history table.
Query Historical Process instances
After the process instance is started, we can also query the total number of processes executed by a process instance, because we have just started a process in this example, so currently, only one process can be found:
/*** Query the historical process instance */@ Testpublic void findHistoryProcessInstance () {String processInstanceId = "501"; HistoricProcessInstance hpi = processEngine. getHistoryService (). createHistoricProcessInstanceQuery (). processInstanceId (processInstanceId ). singleResult (); System. out. println (hpi. getId () + "" + hpi. getProcessDefinitionId () + "" + hpi. getStartTime () + "" + hpi. getDurationInMillis ());}
Running result:
501 HelloWorld: 2: 404 Fri Jun 26 09:34:51 CST 2015 null
Query the current personal task
After the above process is started, a task record is inserted in the task table because the node is a task node. Now we can query the task by the handler:
/*** Query the current personal Task */@ Testpublic void findMyPersonTask () {String assignee = "zhangsan"; // TODOList <Task> list = processEngine. getTaskService () // The service related to the ongoing task management. createTaskQuery () // create a task query object // query conditions. taskAssignee (assignee) // specify the individual task query and the handler //. taskCandidateGroup ("") // query the group task handler //. processDefinitionId ("") // use the process definition ID to query //. processInstanceId ("") // query by process instance ID //. executionId (executionId) // query by execution Object ID/** sort */. order ByTaskCreateTime (). asc () // sort by creation time in ascending order // return result set //. singleResult () // returns a unique result set //. count () // number of returned result sets //. listPage (firstResult, maxResults) // query by page. list (); // returns the list if (list! = Null & list. size ()> 0) {for (Task task: list) {System. out. println ("task ID:" + task. getId (); System. out. println ("task Name:" + task. getName (); System. out. println ("task creation time:" + task. getCreateTime (); System. out. println ("task handler:" + task. getAssignee (); System. out. println ("process instance ID:" + task. getProcessInstanceId (); System. out. println ("execution Object ID:" + task. getExecutionId (); System. out. println ("process definition ID:" + task. getProcessDefinitionId (); System. out. println ("##################################### #############");}
Running result:
Task ID: 504
Task Name: submit an application
Task Creation Time: Fri Jun 2609: 34: 51 CST 2015
Task handler: James
Process instance ID: 501
Execution Object ID: 501
Process Definition ID: HelloWorld: 2: 404
######################################## ##########
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 the query object. Use taskAssignee to specify the handler of the task (that is, query the agent task 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) in this case, ProcessInstance is equivalent to Execution.
7) when a Task node and Execution node are 1-to-1, Execution _ is used in the task object to represent the relationship between them.
8) the task ID corresponds to the "ID _" column in the database table act_ru_task.
Complete task
After the task is queried, complete the task with the task id 504:
/*** Complete my task */@ Testpublic void compliteMyPersonTask () {// task IDString taskId = "504"; processEngine. getTaskService (). complete (taskId); System. out. println ("completed task: task ID:" + taskId );}
Running result:
Task completed: task ID: 504
Note:
1) the task is completed. Therefore, TaskService is obtained from ProcessEngine.
2) After executing this code and executing the query as an employee, you will find that there is no data at this time because there is no data in the ongoing task.
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.
Query Historical Tasks
Employee Zhang San's task has been completed, and now the task is sent to department manager Li Si. If we still query the task of Zhang San, we cannot find it. Only Li Si can query the task, however, you can use the process instance id to query historical tasks. You can query both the tasks that have been processed and the tasks that are being executed:
/*** Query historical tasks */@ Testpublic void findHistoryTask () {String processInstanceId = "501"; List <HistoricTaskInstance> list = processEngine. getHistoryService () // service related to historical data (History Table. createHistoricTaskInstanceQuery () // create a historical task instance query. processInstanceId (processInstanceId )//. taskAssignee (taskAssignee) // the handler for the specified historical task. 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 ("################################");}}}
Running result:
504 submit application 501 Fri Jun 26 09:34:51 CST2015 Fri Jun 26 09:50:50 CST 2015 959867
################################
602 approval [department manager] 501 Fri Jun 26 09:50:51 CST2015 null
################################
Check whether the process is complete
We can also query the current status of a process through the process instance id, whether it is still in progress or whether the process execution has ended:
/*** Query the Process status (judge whether the process is being executed or ended) */@ Testpublic void isProcessEnd () {String processInstanceId = "501"; ProcessInstance pi = processEngine. getRuntimeService () // indicates the process instance and execution object being executed. createProcessInstanceQuery () // create a process instance for query. processInstanceId (processInstanceId) // use the process instance ID to query. singleResult (); if (pi = null) {System. out. println ("the process has ended");} else {System. out. println ("the process is not finished ");}}
Running result:
Process not completed
Iii. Summary
This article mainly describes process examples, execution objects, tasks, and their relationships, at the same time, we will also introduce how to start and Query Process instances, determine whether process instances are completed, view and process tasks, and query historical tasks.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.