Workflow Learning: Activiti Process Example and Task Management

Keywords: Database Attribute

I. Preface

 

In the last article, we introduced the knowledge of Activiti process definition management. In this article, we continue to learn about Activiti process instances and task management.

 

Two, text

 

Process Instance

 

The relationship between Process Instance and Process Definition in the previous article is somewhat similar to that between class and instance objects. Process Definition is the description of the whole process step and Process Instance is the largest execution route of process definition from start to end.  

 

Execution object

 

Referring to Process Instance, there is another noun, Execution, which is the current route of execution according to the rules of Process Definition.

 

If there is only one execution route for Process Definition, then Execution and Process Instance are exactly the same. If there are multiple execution routes in Process Definition, Execution and Process Instance may or may not be the same. So it is concluded that there is and can only be one Process Instance in a process, while Execution can exist more than one.

 

Task

 

Task should be well understood, that is, the task information produced when the process is executed to a certain step or link.

 

In the last article, we introduced how to draw flow charts, how to deploy process definitions, and the relationship between process definitions and process instances. Now it's time to start process instances:


Code

 

Start process instance

  1. /** 
  2.  * Start process instance 
  3.  */  
  4. @Test  
  5. public void startProcessInstance() {  
  6.     //key of process definition  
  7.     String processDefinitionKey = "HelloWorld";  
  8.     ProcessInstance pi = processEngine.getRuntimeService()//Service s related to executing process instances and executing objects  
  9.             .startProcessInstanceByKey(processDefinitionKey);//Start the process instance with the key defined by the process, which corresponds to the attribute value of id in the HelloWorld.bpmn file. Start with the key value. Start with the latest version of the process definition by default.  
  10.     System.out.println("Process instance ID:" + pi.getId());  
  11.     System.out.println("Process definition ID:" + pi.getProcessDefinitionId());  
  12. }  

Operation results:


Process instance ID:501

Process Definition ID:HelloWorld:2:404

 

Explain:

 

1) in data base Insert a record into the table of execution objects being executed by act_ru_execution

2) Insert a record in the history table of the act_hi_procinst procedure instance in the database

3) Insert a record in the history table of the act_hi_act active node in the database

4) The nodes in our diagram are task nodes, so a record is added to the history table of the act_ru_task process instance.

5) A record is also inserted into the act_hi_taskinst task history table of the database.

 

 


Examples of query history process

    

After the process instance is started, we can also query how many times a process instance has been executed altogether, because we just have a process in this case, so we can only find one process at present:

  1. /** 
  2.  * Examples of query history process 
  3.  */  
  4. @Test  
  5. public void findHistoryProcessInstance(){  
  6.     String processInstanceId="501";  
  7.     HistoricProcessInstance hpi = processEngine.getHistoryService()  
  8.             .createHistoricProcessInstanceQuery()  
  9.             .processInstanceId(processInstanceId)  
  10.             .singleResult();  
  11.     System.out.println(hpi.getId() +"    "+hpi.getProcessDefinitionId()+"   "+ hpi.getStartTime()+"   "+hpi.getDurationInMillis());  
  12. }  

Operation results:


    501    HelloWorld:2:404   Fri Jun 26 09:34:51 CST 2015   null

 

 

 


Query current personal tasks


After the process is started, because the node is a task node, a record of the task is inserted in the task table. Now we can query the task through the operator:

  1. /** 
  2.  * Query current personal tasks 
  3.  */  
  4. @Test  
  5. public void findMyPersonTask() {  
  6.     String assignee = "Zhang San"// TODO  
  7.     List<Task> list = processEngine.getTaskService()//Services related to ongoing task management  
  8.             .createTaskQuery()//Create task query objects  
  9.             //Query conditions  
  10.             .taskAssignee(assignee)//Designated Personal Task Query, Designated Translator  
  11.             //TakCandidate Group ("")// Transactor Query for Group Tasks  
  12.             //Process Definition Id (")// Use Process Definition ID Queries  
  13.             //ProceInstanceId (")// Query using process instance ID  
  14.             //Execution Id // Query with Execution Object ID  
  15.             /** Sort*/  
  16.             .orderByTaskCreateTime().asc()//Use the ascending order of creation time  
  17.             //Return result set  
  18.             //singleResult()// Returns a unique result set  
  19.             //count()// Number of result sets returned  
  20.             //ListPage (first Result, maxResults)// Paging Query  
  21.             .list();//Return list  
  22.     if (list != null && list.size() > 0) {  
  23.         for (Task task : list) {  
  24.             System.out.println("task ID: " + task.getId());  
  25.             System.out.println("Task name:" + task.getName());  
  26.             System.out.println("Task creation time:" + task.getCreateTime());  
  27.             System.out.println("Task Manager:" + task.getAssignee());  
  28.             System.out.println("Process instance ID:" + task.getProcessInstanceId());  
  29.             System.out.println("target of execution ID:" + task.getExecutionId());  
  30.             System.out.println("Process definition ID:" + task.getProcessDefinitionId());  
  31.             System.out  
  32.                     .println("##################################################");  
  33.         }  

Operation results:


Task ID: 504

Task Name: Application submission

Task creation time: Fri Jun 2609:34:51 CST 2015

Manager of the task: Zhang San

Process instance ID:501

Execution object ID:501

Process Definition ID:HelloWorld:2:404

    ##################################################

 

Explain:


1) Task Service should be obtained from process Engine because it is a task query

2) Task Query Object Retrieved by TaskService

3) Adding query filtering conditions to query objects, using task Assignee to specify task handlers (i.e. agent tasks for querying specified users), and adding filtering conditions such as paging sorting.

4) Call the list method to execute the query and return the task list for the specified user

5) Task ID, name, transactor and creation time can be found in the act_ru_task table.

6) In this case, Process Instance is equivalent to Execution

7) When a Task node and Execution node are one-to-one, the relationship between them is represented by Execution_in the task object

8) Task ID corresponds to the "ID_" column in the database table act_ru_task

 


Finish the task


After querying the task, we will complete the task id 504:

  1. /** 
  2.  * Complete my task 
  3.  */  
  4. @Test  
  5. public void compliteMyPersonTask() {  
  6.     //Task ID  
  7.     String taskId = "504";  
  8.     processEngine.getTaskService().complete(taskId);  
  9.     ;  
  10.     System.out.println("Completing tasks: tasks ID:" + taskId);  
  11. }  

Operation results:


Complete tasks: Task ID:504

 

Explain:


1) Complete the task, so Task Service is what you get from Process Engine.

2) When the code is executed and the query is executed as an employee, it will be found that there is no data at this time, because there is no data in the task being executed.

3) For a completed task, activiti will delete the task from the act_ru_task table, and the next task will be inserted.

4) The results can be found by inquiring as "department manager". Because the process is executed to the Department Manager to approve the node.

5) Re-execute the processing task code, and query as "department manager" after execution, with no result.

Repeat steps 3 and 4 until the process is completed.

 


Query History Task


Employee Zhang San's task has been completed. Now the task is to Department Manager Li Si. If we still query Zhang San's task now, we can't find it. Only by querying Li Si can we find it. However, we can query the historical task through process instance id. By querying the historical task, we can query both the tasks that have been handled and the tasks that are being performed. Come out:

  1. /** 
  2.  * Query History Task 
  3.  */  
  4. @Test  
  5. public void findHistoryTask(){  
  6.     String processInstanceId="501";  
  7.     List<HistoricTaskInstance> list = processEngine.getHistoryService()//Services related to historical data (historical tables)  
  8.             .createHistoricTaskInstanceQuery()//Creating Historical Task Instance Queries  
  9.             .processInstanceId(processInstanceId)  
  10. //taskAssignee(taskAssignee)//Handler assigned to historical tasks  
  11.             .list();  
  12.     if(list!=null && list.size()>0){  
  13.         for(HistoricTaskInstance hti:list){  
  14.             System.out.println(hti.getId()+"    "+hti.getName()+"    "+hti.getProcessInstanceId()+"   "+hti.getStartTime()+"   "+hti.getEndTime()+"   "+hti.getDurationInMillis());  
  15.             System.out.println("################################");  
  16.         }  
  17.     }     
  18.   
  19. }  

Operation results:


504) Application submission 501 Fri Jun 2609:34:51 CST 2015 Fri Jun 2609:50 CST 2015 959867

    ################################

602) Approval [Department Manager] 501 Fri Jun 2609:50:51 CST 2015 null

    ################################

 


Does the query process end?


We can also query the current status of a process through the process instance id, whether it is still in the process of execution or whether the process execution is over:

 

  1. /** 
  2.  * Query process status (determine whether the process is executing or ending) 
  3.  */  
  4. @Test  
  5. public void isProcessEnd(){  
  6.     String processInstanceId =  "501";  
  7.     ProcessInstance pi = processEngine.getRuntimeService()//Represents the process instance and execution object being executed  
  8.             .createProcessInstanceQuery()//Create process instance queries  
  9.             .processInstanceId(processInstanceId)//Use process instance ID to query  
  10.             .singleResult();  
  11.       
  12.     if(pi==null){  
  13.         System.out.println("The process is over");  
  14.     }  
  15.     else{  
  16.         System.out.println("The process is not over");  
  17.     }  
  18.       
  19. }  

Operation results:


The process is not over

 

Three, summary

 

In this article, we mainly study process instances, executing objects, tasks and their relationships. At the same time, we also introduce how to start and query process instances, judge whether the process instances are finished, view and handle tasks, and query historical tasks.

http://blog.csdn.net/zwk626542417/article/details/46646565

Posted by tserbis on Thu, 04 Apr 2019 15:33:30 -0700