An overview,

Description: In Activiti tasks, there are three types of task-related tasks, which are defined as follows:

  • Candidate (group) : Represents the person who has the authority to operate the task.
  • Holder: The performer of the current task who currently holds the task.
  • Agent: when a person is not convenient to handle the task, you can point the task to another person, the person referred to is the agent.

Ii. Candidates

The relationship between tasks and candidates is stored in an intermediate table (act_ru_identitylink). All tasks and candidates are many-to-many.

  1. The coding tests are as follows:
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

 TaskService taskService = processEngine.getTaskService();

 // Create a task here. The new created here cannot be used in production because the task is bound to the process. This is just a demo, just new a task
 String taskId = UUID.randomUUID().toString();
 Task task = taskService.newTask(taskId);
 task.setName("Test task");
 taskService.saveTask(task);

 // Create a user, because the candidate user group does not support task query by user group, so set the candidate for the task here.
 IdentityService identityService = processEngine.getIdentityService();

 String userId = UUID.randomUUID().toString();
 User user = identityService.newUser(userId);
 user.setFirstName("Old");
 user.setLastName("The king");
 identityService.saveUser(user);

 // Set the candidates for the task
 taskService.addCandidateUser(taskId,userId);

 // Reverse query task: Query the tasks owned by the user
 List<Task> list = taskService.createTaskQuery().taskCandidateUser(userId).list();
 System.out.println(user.getFirstName() + "" + user.getLastName() + "The user has permission to perform the following tasks:);
 list.forEach(task1 -> System.out.println(task1.getName()));


 / / close
 processEngine.close();
 System.exit(0);
Copy the code
  1. The results view
    • User table view:
    • Task table view:
    • Relational tables:
    • Running results:

Iii. Holder

Description: A person can hold multiple tasks, a task can only have one holder, so the relationship between the holder and the task is one-to-many. The OWNER relationship is represented in the OWNER field of the act_RU_task task table.

  1. Coding test:
// The rest of the code is the same as the last candidate
// Set the task holder
taskService.setOwner(taskId,userId);

// Reverse query task: Query tasks based on the owner
List<Task> list = taskService.createTaskQuery().taskOwner(userId).list();
Copy the code
  1. The results of

Iv. Agent

Introduction: A person can be an agent for multiple tasks. A task can have only one agent. Therefore, the relationship between an agent and a task is one-to-many

Difference: TaskService.setassignee () and taskservice.claim () can both assign proxies to tasks, the difference being:

  • The setAssignee method may designate proxies for tasks at any time and arbitrarily (which can be invoked multiple times),
  • And method of the claim to the task assigned a agent, if again to invoke the claim to specify the agent, will throw an exception: ActivitiTaskAlreadyClaimedException: Task ‘b7ec485a-a4cd-4bdc-8117-8beb76a81c2f’ is already claimed by someone else.
  1. Coding test
A / / users
String userId = UUID.randomUUID().toString();
User user = identityService.newUser(userId);
user.setFirstName("Small");
user.setLastName("Ming");
identityService.saveUser(user);

/ / users
String userId2 = UUID.randomUUID().toString();
User user2 = identityService.newUser(userId2);
user2.setFirstName("Big");
user2.setLastName("Ming");
identityService.saveUser(user2);

//1. Assign a proxy to a task using the taskService.setassignee () method
// taskService.setAssignee(taskId,userId);
// taskService.setAssignee(taskId,userId2);

//2. Use the taskService.claim() method to set up an agent for the task
taskService.claim(taskId,userId);
taskService.claim(taskId,userId2);

// Complete the task
//taskService.complete(taskId);
Copy the code
  1. Database view:
  2. The following error occurs when you call claim twice: