The article directories

    • A single task
      • Single user mode
          • 1. In the flowchart, configure task nodes and directly specify task users
          • 2. Configure task nodes in the flow chart and specify the user who receives the task in the form of flow variables
          • 3. In the process definition, the assignee of the userTask node is unmatched by specifying a task listener that is dynamically specified in the listener
    • Many tasks
      • 1. Multi-person task allocation
        • Candidate -users= “little A, little B, little C, little D”
        • 2. In the BPMN process definition file, write candidate-users = “#{userIDs}”, the value of the variable should be String. Use process variables to specify transactors
        • 3. Use the TaskListener interface and implement the interface using a class
      • Second, group task allocation
        • Start by establishing group relationships
        • Example Query the group task list
        • Example Query the task member list of a group
      • extension

Activiti sets up node handlers in three ways:

  • Assignment: Assign the only one.
  • Candidate users: Candidates who can specify more than one person.
  • Candidate Groups: Candidate groups that can be specified.

A single task

Single user mode

1. In the flowchart, configure task nodes and directly specify task users

Explicitly specified directly in the BPMN file

The userTask is assigned by zhang sanfeng

Act_ru_task table

Disadvantages: hard coding, task assignment user write dead


2. Configure task nodes in the flow chart and specify the user who receives the task in the form of flow variables

You can specify the processing user for the task at process start or at other times

        // Start the process
        ProcessEngine processEngine = getProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();

        // Set the process variable when the process starts
        Map<String.Object> variables = new HashMap<String.Object> (); variables.put("userId"."Zhou Zhiruo");
        runtimeService.startProcessInstanceByKey("testUserAssignee",variables);
Copy the code

View assignee of task nodes in the database

Act_ru_task table


3. In the process definition, the assignee of the userTask node is unmatched by specifying a task listener that is dynamically specified in the listener
  • Task listeners need to implement the TaskListener interface

BPMN process configuration

   <userTask activiti:exclusive="true" id="userTask" name="Approval">
      <extensionElements>
        <activiti:taskListener class="cn.cf.activiti.atguigu.AssigneeTaskUser" event="create"/>
      </extensionElements>
    </userTask>
Copy the code

Listening to the class

public class AssigneeTaskUser implements TaskListener {
    public void notify(DelegateTask delegateTask) {
        delegateTask.setAssignee(The Golden Lion); }}Copy the code

Start the process to view the handler of the task node in the database

act_ru_task.png

Taskservice.setassignee (taskId,userId) can be used, if the task has already been assigned to a specified user; Re-designate transactor


Many tasks

There will be a concept of a sign-in task. As long as the task has not been signed, any user in the candidate group can sign for the task; Once a task is signed in, no other users in the candidate group can view and sign in the task.

1. Multi-person task allocation

The so-called group task is the candidate of the task. The user in the candidate has the right to claim the task and complete the task. After the task is claimed, other candidates cannot see the task again. When a user claims a task, the user can still claim the task even if the user is not in the candidate list

The database tables involved are:

  • The act_ru_identitylink table stores the handlers of tasks, including individual and group tasks, that are in progress
  • The act_hi_identitylink table stores the handlers of tasks, including individual and group tasks, and represents historical tasks

The TYPE_ field in the table is used to distinguish whether a user is a handler or a candidate. In individual tasks (specified by assignee), the value of this field is participant (participant), and in group tasks (specified by candidateUser or candidateGroup), TYPE_ can be participant or candidate.

  • Assignment mode The same as the preceding three configuration modes

Candidate -users= “little A, little B, little C, little D”

2. In the BPMN process definition file, write candidate-users = “#{userIDs}”, the value of the variable should be String. Use process variables to specify transactors

 Map<String.Object> variables = new HashMap<String.Object> (); variables.put("userIDs"."Big, small, medium.");
Copy the code

3. Use the TaskListener interface and implement the interface using a class

// Add candidates for user tasks (multiple candidates can be added consecutively)
delegateTask.addCandidateUser(userId1);
delegateTask.addCandidateUser(userId2);
Copy the code

Second, group task allocation

Use the candidate Groups attribute to specify task groups, where the department manager represents the role of a user group

Start by establishing group relationships

Set the set of tasks to deal with people, to the Activity for groups and users of information in the table IdentityService IdentityService = processEngine. GetIdentityService ();// Authentication: Saves group and user information
		identityService.saveGroup(new GroupEntity("Department manager"));/ / group is established
		identityService.saveGroup(new GroupEntity("General manager"));/ / group is established
		identityService.saveUser(new UserEntity("Zhang"));
		identityService.saveUser(new UserEntity("Xiao li"));
		identityService.saveUser(new UserEntity("Wang"));
		identityService.createMembership("Zhang"."Department manager");// Establish a group and user relationship
		identityService.createMembership("Xiao li"."Department manager");// Establish a group and user relationship
		identityService.createMembership("Wang"."General manager");// Establish a group and user relationship
Copy the code

Table Structure introduction

Act_id_group: role group table

Act_id_user: user table

Act_id_membership: user role table

  • Create a user

    User user = identityService.newUser(userId);
    user.setFirstName("Deniro");
    user.setLastName("Lee");
    user.setEmail("[email protected]");
    identityService.saveUser(user);
    Copy the code
  • Create a group

    // Create a group object
    Group group = identityService.newGroup(groupId);
    group.setName("Department head");
    group.setType("assignment");
    
    / / save
    identityService.saveGroup(group);
    Copy the code
  • Associate groups and users

    // Add the user to the specified group
    identityService.createMembership(userId, groupId);
    
    // Query the users in the specified group
    User userInGroup = identityService.createUserQuery().memberOfGroup(groupId).singleResult();
    
    // Query the groups under the specified user
    Group groupContainUser = identityService.createGroupQuery().groupMember(userId).singleResult();
    Copy the code

Example Query the group task list

String userId = "Xiao li";// Xiao Zhang, Xiao Li can check the result, but Xiao Wang can't, because he is not the department manager
		List<Task> list = processEngine.getTaskService()//
		                .createTaskQuery()//
		                .taskCandidateUser(userId)// Query the task of the specified group
		                .list();
Copy the code

Example Query the task member list of a group

	String taskId = "4408";
		List<IdentityLink> list = processEngine.getTaskService()//
		                .getIdentityLinksForTask(taskId);
		for(IdentityLink identityLink:list ){
			System.out.println("userId="+identityLink.getUserId());
			System.out.println("taskId="+identityLink.getTaskId());
			System.out.println("piId="+identityLink.getProcessInstanceId());
			System.out.println("# # # # # # # # # # # # # # # # # # # # # #");
		}
Copy the code

extension

Query the task of the group participant: processEngine.getTaskService(a).createTaskQuery(a).taskCandidateUser(assignee)// participant, group task query group task assigned to individual task (claim task) : processEngine.getTaskService(a).claim(taskId, userId); Individual tasks are assigned to group tasks: tasks claimed can be pulled back to group tasks (prerequisite: the previous task was a group task), and other people can claim: processEngine.getTaskService(). setAssignee(taskId, null); Adding people to group tasks: Group tasks can continue to add group participants: processEngine.getTaskService(a).addCandidateUser(taskId, userId); Deleting people to group tasks: Group tasks can delete participating people: processEngine.getTaskService(a).deleteCandidateUser(taskId, userId); Query the List of group tasks being executed: List<IdentityLink> List = processEngine.getTaskService(a).getIdentityLinksForTask(taskId);
Copy the code

Supplement: The difference between claim and setAssignee is that others shall not collect claim after it is obtained, otherwise an error will be reported, while setAssignee will not


Keep warm together and make progress together

🍎QQ group [837324215] 🍎 pay attention to my public number [Java Factory interview officer], learn together 🍎🍎🍎 🍎 personal vx [Lakernote]