• 配置多用户alt
idGROUP_IDTYPEUSER_IDTASK_IDPROC_INST_ID_
350061candidatexd35005
350071participantxd35001
350081candidatexf35005
350091participantxf35001
350101candidatexe35005
350111participantxe35001
350121candidatexa35005
350131participantxa35001

查询组任务列表

        /*
            查询组任务列表
        */
		@Test
		public void findGroupList(){
			String userId = "xa";
			List<Task> list = proEng.getTaskService()//
			                .createTaskQuery()//
			                .taskCandidateUser(userId)//指定组任务查询
			                .list();
			for(Task task:list ){
				System.out.println("id="+task.getId());
				System.out.println("name="+task.getName());
				System.out.println("assinee="+task.getAssignee());
				System.out.println("createTime ="+task.getCreateTime());
				System.out.println("executionId="+task.getExecutionId());
				System.out.println("##################################");
				
			}
		}

查询组任务成员列表


		/**
		 *  
		 * @Title: findGroupUser   
		 * @Description: TODO(查询组任务成员列表)   
		 * @param:       
		 * @return: void      
		 * @throws
		 */
		@Test
		public void findGroupUser(){
			String taskId = "35005";
			List<IdentityLink> list = proEng.getTaskService()//
			                .getIdentityLinksForTask(taskId);
	         //List<IdentityLink> list = processEngine.getRuntimeService()//
			//				.getIdentityLinksForProcessInstance(instanceId);
			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("######################");
			}
		}
		

查询组任务成员历史列表

/**
		 * 
		 * @Title: findGroupHisUser   
		 * @Description: TODO 查询组任务成员历史列表    
		 * @param:       
		 * @return: void      
		 * @throws
		 */
		@Test
		public void findGroupHisUser(){
			String taskId = "35005";
			List<HistoricIdentityLink> list = proEng.getHistoryService()//
							.getHistoricIdentityLinksForTask(taskId);
	        // List<HistoricIdentityLink> list = processEngine.getHistoryService()//
	          // .getHistoricIdentityLinksForProcessInstance(processInstanceId);
			for(HistoricIdentityLink identityLink:list ){
				System.out.println("userId="+identityLink.getUserId());
				System.out.println("taskId="+identityLink.getTaskId());
				System.out.println("piId="+identityLink.getProcessInstanceId());
				System.out.println("######################");
			}
		}
		

分配组任务

  /**将组任务分配给个人任务,拾取任务*/
		//由1个人去完成任务
		@Test
		public void claim(){
			//任务ID
			String taskId = "35005";
			//分配的办理人
			String userId = "xb";
			proEng.getTaskService()//
						.claim(taskId, userId);
		}
		
		/**
		 * 
		 * @Title: claim   
		 * @Description: TODO(拾取后回退)   
		 * @param:       
		 * @return: void      
		 * @throws
		 */
		@Test
		public void claimRollback(){
			//任务ID
			String taskId = "35005";
			 
 			proEng.getTaskService()//
						.claim(taskId, null);
		}

说明:

  1. 小A,小B,小C,小D是组任务的办理人
  2. 但是这样分配组任务的办理人不够灵活,因为项目开发中任务的办理人不要放置XML文件中。
  3. act_ru_identitylink表存放任务的办理人,包括个人任务和组任务,表示正在执行的任务
  4. act_hi_identitylink表存放任务的办理人,包括个人任务和组任务,表示历史任务
  • 区别在于:
    • 如果是个人任务TYPE的类型表示participant(参与者)
    • 如果是组任务TYPE的类型表示candidate(候选者)和participant(参与者)

使用流程变量

  • candidate Users ${userIds}
// 2 启动流程
		//启动流程实例,同时设置流程变量,用来指定组任务的办理人
		Map<String, Object> variables = new HashMap<String, Object>();
		variables.put("userIDs", "大大,小小,中中");
		ProcessInstance pi = processEngine.getRuntimeService()//
						.startProcessInstanceByKey("taskProcess",variables);
		System.out.println("pid:" + pi.getId());

使用监听器

// TaskListenerImpl类,用来设置任务的办理人
public class TaskListenerImpl implements TaskListener {

	/**指定个人任务和组任务的办理人*/
	@Override
	public void notify(DelegateTask delegateTask) {
		String userId1 = "孙悟空";
		String userId2 = "猪八戒";
		//指定组任务
		delegateTask.addCandidateUser(userId1);
		delegateTask.addCandidateUser(userId2);
	}

}

使用角色组

  • candidateGroups
  <process id="gr_0903" name="group_candid" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="提交申请" activiti:candidateGroups="admin_0903"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
  </process>
package com.osvue.bpmn.activitiapp.bpmn.identity;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

/**
 * 
 * @ClassName:  GroupTaskManage   
 * @Description:use Identity 
 * @author: hzq 
 * @date:   2023-9-3 8:11:15    
 * @Copyright: 2023
 */
public class GroupTaskManage {

ProcessEngine proEng = ProcessEngines.getDefaultProcessEngine();
	
	@Test
	public void init() {
		ProcessEngine proEng = ProcessEngines.getDefaultProcessEngine();
	}
	
	/**
	 * 
	 * @Title: deploy   
	 * @Description: TODO(部署流程)   
	 * @param:       
	 * @return: void      
	 * @throws
	 */
	@Test
	public void deploy() {
//		部署流程
		Deployment dep = proEng.getRepositoryService()
				.createDeployment()
				.name("groupName_0903")
				.addClasspathResource("diagrams/dentity/groupm.bpmn")
				.addClasspathResource("diagrams/dentity/groupm.png").deploy();
			
		System.out.println(dep.getId());
		
	}
	
	/**
	 * 
	 * @Title: createGroupUser   
	 * @Description: TODO(创建用户和组)   
	 * @param:       
	 * @return: void      
	 * @throws
	 */
	@Test
	public void createGroupUser() {
		IdentityService idse = proEng.getIdentityService();
		Group group = new GroupEntity("boss_0903");
		idse.saveGroup(group);
		idse.saveGroup(new GroupEntity("admin_0903"));
		idse.saveGroup(new GroupEntity("user_0903"));
		idse.saveGroup(new GroupEntity("cw_0903"));
		
		
		User user = new UserEntity("1");
		idse.saveUser(user);
		idse.saveUser(new UserEntity("2"));
		idse.saveUser(new UserEntity("3"));
		idse.saveUser(new UserEntity("4"));
		idse.saveUser(new UserEntity("5"));
		idse.saveUser(new UserEntity("6"));
		
//		idse.createMembership(userId, groupId);
		
		idse.createMembership("1", "boss_0903");
		idse.createMembership("2", "admin_0903");
		idse.createMembership("3", "admin_0903");
		idse.createMembership("4", "admin_0903");
		idse.createMembership("5", "cw_0903");
		idse.createMembership("6", "user_0903");
 	}
	/**
	 * 启动流程
	 */
	@Test
	public void startProcess() {
		
		String processDefinitionId = "";
		Map<String,Object> param = new HashMap<>();
		 
		ProcessInstance proces = proEng.getRuntimeService()
//		.startProcessInstanceById(processDefinitionId)
				
		.startProcessInstanceByKey("gr_0903" );
		
		System.out.println(proces.getName());
	}

	/**
	 * 查询个人任务
	 */
	@Test
	public void findTask() {
		TaskService taskService = proEng.getTaskService();
//		String assignee="张三";
// 		String assignee="李四";
 		String assignee="xb";
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
		if(null!=list&&list.size()>0) {
			for (Task task : list) {
				System.out.println("任务ID:"+task.getId());
				System.out.println("流程实例ID:"+task.getProcessInstanceId());
				System.out.println("执行实例ID:"+task.getExecutionId());
				System.out.println("流程定义ID:"+task.getProcessDefinitionId());
				System.out.println("任务名称:"+task.getName());
				System.out.println("任务办理人:"+task.getAssignee());
				System.out.println("################################");
			}
		}
	}
	
	
		// 查询组任务列表
		@Test
		public void findGroupList(){
			String userId = "4";
			List<Task> list = proEng.getTaskService()//
			                .createTaskQuery()//
			                .taskCandidateUser(userId)//指定组任务查询
			                .list();
			for(Task task:list ){
				System.out.println("任务id="+task.getId());
				System.out.println("任务name="+task.getName());
				System.out.println("assinee="+task.getAssignee());
				System.out.println("createTime ="+task.getCreateTime());
				System.out.println("executionId="+task.getExecutionId());
				System.out.println("##################################");
				
			}
		}
		
		
		
		/**
		 *  
		 * @Title: findGroupUser   
		 * @Description: TODO(查询组任务成员列表)   
		 * @param:       
		 * @return: void      
		 * @throws
		 */
		@Test
		public void findGroupUser(){
			String taskId = "47504";
			List<IdentityLink> list = proEng.getTaskService()//
			                .getIdentityLinksForTask(taskId);
	         //List<IdentityLink> list = processEngine.getRuntimeService()//
			//				.getIdentityLinksForProcessInstance(instanceId);
			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("######################");
			}
		}
		
		
		/**
		 * 
		 * @Title: findGroupHisUser   
		 * @Description: TODO 查询组任务成员历史列表    
		 * @param:       
		 * @return: void      
		 * @throws
		 */
		@Test
		public void findGroupHisUser(){
			String taskId = "47504";
			List<HistoricIdentityLink> list = proEng.getHistoryService()//
							.getHistoricIdentityLinksForTask(taskId);
	        // List<HistoricIdentityLink> list = processEngine.getHistoryService()//
	          // .getHistoricIdentityLinksForProcessInstance(processInstanceId);
			for(HistoricIdentityLink identityLink:list ){
				System.out.println("userId="+identityLink.getUserId());
				System.out.println("taskId="+identityLink.getTaskId());
				System.out.println("piId="+identityLink.getProcessInstanceId());
				System.out.println("piId="+identityLink.getGroupId());
				System.out.println("######################");
			}
		}
		
		 
	/**
	 * 办理任务
	 */
	@Test
	public void doTask(){
		TaskService taskService = proEng.getTaskService();
		String taskId="47504";
		taskService.setVariable (taskId, "userid", "sgrdbslocal_17778179517");
//		taskService.
		taskService.complete(taskId);
		System.out.println("任务完成");
	}
}