Activiti Learning Notes Part 6: Process Instances, Task Execution

Keywords: Database Attribute

1. flow chart

2. Deployment process definition

/**1. Deployment process definition*/
@Test
public void deployZIP() throws Exception {
	// Input process for obtaining uploaded files
	InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/leave.zip");
	ZipInputStream zipInputStream = new ZipInputStream(in);
	// Get warehouse services and complete deployment from the classpath
	Deployment deployment = processEngine.getRepositoryService()//
										.createDeployment()//
										.name("Leave process")//Add display aliases for deployment rules
										.addZipInputStream(zipInputStream)//Deployment using zip input stream
										.deploy();//Complete release
	System.out.println(deployment.getId()+"           "+deployment.getName());
}

3. Start process example

/**
 * 2 Startup process
 * 		When a process reaches a node, it will act_ru_execution Generate one piece of data in the table
 * 		If the current node is the user task node, then the act_ru_task The table produces one piece of data (task handler, task creation time)
 * /
@Test
public void startProcess() throws Exception {
	// Start with id defined by the process: myProcess:2:604
	// runtimeService.startProcessInstanceById(processDefinitionId)
	// Starting the process with the key defined by the process will start the version-max process
	ProcessInstance pi = ProcessEngines.getDefaultProcessEngine()
								.getRuntimeService()//Get the Service being executed
								.startProcessInstanceByKey("myProcess");// Start the process instance according to the key defined by the process by default according to the latest version
	System.out.println("pid:"+pi.getId()+",activitiId:"+pi.getActivityId()+",pdId:"+pi.getProcessDefinitionId());
}

Explain:

  1. The act_ru_execution table that operates on the database, if it is a user task node, also adds a record to act_ru_task

4. Query my personal tasks

// 2.1 View Personal Tasks
@Test
public void queryPersonalTask() throws Exception {
	// Configuring Query Objects
	String assignee = "Zhang San";
	// Create Task Query Objects and Query Personal Tasks
	List<Task> list = processEngine.getTaskService()
										.createTaskQuery()
										.taskAssignee(assignee)//Assigned Personal Task Manager Query Task
										.orderByTaskCreateTime().desc()//Create ascending order of tasks
										.list();//All records of query tasks
	System.out.println("==============["+assignee+"]Personal Task List=============");
	for (Task task : list) {
		System.out.println("id:"+task.getId()+",");
		System.out.println("name:"+task.getName()+",");
		System.out.println("createTime:"+task.getCreateTime()+",");
		System.out.println("assignee:"+task.getAssignee());
	}
}

Explain:

  1. Because it's a task query, you should get TaskService from the process Engine
  2. Getting TaskQuery Object by TaskService
  3. To add query filtering conditions for query objects, task Assignee is used to specify the operator of the task (that is, the agent task of the query specified user), and filtering conditions such as paging sort can be added.
  4. Call the list method to execute the query and return the task list for the specified user
  5. Task ID, name, operator, and creation time can be found in the act_ru_task table.
  6. Execution and Process Instance are described in chapters 5.6 and 5.7. In this case, Process Instance is equivalent to Execution
  7. If the assignee attribute is the Department manager, the result is empty. Because the process has only reached the stage of "filling in leave application", the later task has not been implemented, that is, there is no task that department manager can handle in the database, so the query can not be made.
  8. When a Task node and Execution node are one to one, the relationship between them is represented by Execution_in the task object
  9. Task ID corresponds to the "ID_" column in the database table act_ru_task

Additional:
In activiti task, there are two main types of query tasks (personal task and group task):
1. The assignment of the agent is specified exactly. This assignment will become the assignor's private assignment, i.e. personal assignment.
2. It is impossible to assign a specific person to perform tasks. Tasks can be assigned to several people or to one or more groups, so that users within this scope can selectively handle such tasks, i.e. group tasks, if they have free time.
First know the inquiry and processing of personal tasks, and then talk about the operation of group tasks.

5. Managing tasks

// 4. Managing tasks
@Test
public void complete() throws Exception {
	String taskId = "604";
	// Finish the task
	processEngine.getTaskService()
					.complete(taskId);
}

Explain:

  1. It's for tasks, so what you get from Process Engine is Task Service.
  2. When this code is executed and the query is executed as an employee, you will find that there is no data at this time, because there is no data in the task being executed.
  3. For completed tasks, activiti deletes the task from the act_ru_task table, and the next task is 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. Then execute the processing task code, and query as "department manager" after execution, without result.
  6. Repeat steps 3 and 4 until the process is completed.

6. Query process status (determine whether the process is executing or ending)

// 5. View process status (determine whether the process is executing or ending)
@Test
public void queryProcessState() throws Exception{
	String processInstanceId = "601";
	// Query process instances through process instance ID
	ProcessInstance pi = processEngine.getRuntimeService()
								.createProcessInstanceQuery()//Create process instance queries to query executing process instances
								.processInstanceId(processInstanceId)//Query by process instance ID
								.singleResult();//Returns a unique result set
	if(pi != null){
		System.out.println("The current process is:"+pi.getActivityId());
	}else{
		System.out.println("The process is over!!");
	}
}

In the process of process execution, the process instance ID created will not change during the whole process. When the process is finished, the process instance will be deleted in the executing object table (act_ru_execution).
Explain:

  1. Because it's a query process instance, get the runtimeService first
  2. Create process instance query object and set instance ID filtering parameters
  3. Because a process instance ID corresponds to only one instance, using singleResult to execute a query returns a unique result and throws an exception if the number of results is greater than 1
  4. Determines whether an instance of the specified ID exists, and if the result is empty, it represents the end of the process. The instance has been deleted in the execution object table being executed and converted into historical data.

7. Additional Functions: Query History Tasks (later)

//Query History Task
@Test
public void queryHistoryTask() throws Exception{
	//History Task Manager
	String taskAssignee = "Zhang San";
	// Query process instance through process instance ID
	List<HistoricTaskInstance> list = processEngine.getHistoryService()
										.createHistoricTaskInstanceQuery()//Create historical task queries
										.taskAssignee(taskAssignee)//Designated Agent Inquiry History Task
										.list();
	if(list != null && list.size() > 0){
		for(HistoricTaskInstance task : list) {
			System.out.println("task ID:"+task.getId());
			System.out.println("Process instance ID:"+task.getProcessInstanceId());
			System.out.println("Task Manager:"+task.getAssignee());
			System.out.println("Execution object:"+task.getExecutionId());
			System.out.println(task.getStartTime()+"  "+task.getEndTime()+"  "+task.getDurationInMillis());
		}
	}
}

8. Additional functions: querying historical process instances (later)

//Examples of query history process
@Test
public void queryHistoryProcessInstance() throws Exception {
	String processInstanceId = "601";
	HistoricProcessInstance hpi = processEngine.getHistoryService()
								.createHistoricProcessInstanceQuery()//Create historical process instance queries
								.processInstanceId(processInstanceId)//Use process instance ID to query
								.singleResult();
	System.out.println("Process definition ID:"+hpi.getProcessDefinitionId());
	System.out.println("Process instance ID:"+hpi.getId());
	System.out.println(hpi.getStartTime()+"         "+hpi.getEndTime()+"             "+hpi.getDurationInMillis());
}

9. summary

Execution execution object
Execute a process according to the rules defined in the process.
The corresponding table:
act_ru_execution: Information being executed
act_hi_procinst: Executed historical process instance information
act_hi_actinst: Stores all completed activities in history
Process Instance process instance
Specially refers to the largest execution branch of a process from start to end. In an execution process, there is only one process instance.

Be careful
(1) If it is a singleton process, the execution object ID is the process instance ID.
(2) If a process has branching and aggregation, then the execution object ID and process instance ID are different.
(3) In a process, there is only one instance of the process, and there can be multiple execution objects.

Task task
Task information generated when executing a task link.
The corresponding table:
act_ru_task: Task information being executed
act_hi_taskinst: Information on completed historical tasks

Posted by m00ch0 on Wed, 01 May 2019 16:30:36 -0700