The Activiti process starts
Once the process definition is deployed, the business process can be managed through the workflow, which means that the travel request process deployed above can be used. For this process, starting a process means issuing a new business trip request. This is equivalent to the relationship between Java classes and Java objects. Once a class is defined, it needs to create one object for use, or multiple objects can be created. For the business trip application process, a process instance needs to be started for John to initiate a business trip application, and a process instance needs to be started for John to initiate a business trip application
There are two main ways for an Activiti process to start, processDefinitionKey or processDefinitionId
1. Start according to processDefinitionKey
ProcessDefinitionKey is the value of the KEY_ field of the ACT_RE_procdef table, which is the key of the corresponding process definition
@Test
public void testStartProcess(a) {
// 1. Create ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. Obtain RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
// 3. Start process according to processDefinitionKey
ProcessInstance instance = runtimeService.startProcessInstanceByKey("evection");
}
Copy the code
2. Start with processDefinitionId
ProcessDefinitionId is the primary key ID of act_re_procdef. For example, evection:1:22503
@Test
public void testStartProcess(a) {
// 1. Create ProcessEngine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. Obtain RuntimeService
RuntimeService runtimeService = processEngine.getRuntimeService();
// 3. Start process according to processDefinitionKey
ProcessInstance instance = runtimeService.startProcessInstanceById("evection:1:22503");
}
Copy the code
Activiti individual task queries
After the process starts, the person in charge of a task can query the tasks that need to be handled. The queried tasks are all the user’s to-do tasks
public void testFindPersonTaskList(a) {
// 1. Obtain the flow engine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. Obtain taskService
TaskService taskService = processEngine.getTaskService();
// 3. Obtain the process key and the responsible person of the task, and query the task
List<Task> taskList = taskService.createTaskQuery()
.processDefinitionKey("evection") / / process the key
.taskAssignee("zhangsan") // The person in charge of the query
.list();
/ / 4. Output
for (Task task : taskList) {
System.out.println("Process instance ID =" + task.getProcessInstanceId());
System.out.println("Task id =" + task.getId());
System.out.println("Task Leader =" + task.getAssignee());
System.out.println("Task Name ="+ task.getName()); }}Copy the code
Activiti completes individual tasks
Based on the task ID, that is, find the task and process it
public void completeTask(a) {
// 1. Obtain the flow engine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. Obtain TaskService
TaskService taskService = processEngine.getTaskService();
// 3. Complete the task based on the task ID
taskService.complete("25005");
}
Copy the code
It is very troublesome to find the task ID every time. Generally speaking, the task leader queries the to-do task, selects the task for processing, and completes the task. Then we can find and complete the task according to the task leader and task key
public void completeTask(a) {
// 1. Obtain the flow engine
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
// 2. Obtain TaskService
TaskService taskService = processEngine.getTaskService();
// 3. Obtain the corresponding task of Jerry-Evection
Task task = taskService.createTaskQuery()
.processDefinitionKey("evection")
.taskAssignee("jerry")
.singleResult();
// 4. Complete the task based on the task ID
taskService.complete(task.getId());
}
Copy the code
And so on until the entire business trip process is complete