activiti 动态分配任务候选人

 

任务候选人是有权限对该任务进行操作的用户,
    可以使用TaskService.addCandidateUser() 或 addCandidateGroup()实现,
    可以在bpmn中进行配置,
    可以使用监听器方式(需要继承TaskListener)
    可以使用JUEL表达式实现,使用${}来引用,和EL表达式一样
 
下面我们使用JUEL表达式实现:
1. 在bpmn中引用类:
 
<process id="leaveBill" name="leaveBillprocess" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <endEvent id="endevent1" name="End"></endEvent>
    <userTask id="usertask1" name="User Task"     activiti:candidateUsers="${authService.getCandidateUsers()}"></userTask>
    <sequenceFlow id="flow3" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
    <userTask id="usertask2" name="User Task"></userTask>
    <sequenceFlow id="flow4" sourceRef="startevent1" targetRef="usertask2"></sequenceFlow>
    <sequenceFlow id="flow5" sourceRef="usertask2" targetRef="usertask1"></sequenceFlow>
  </process>
...
 
2. 定义获取用户的类,要序列化
public class AuthService implements Serializable  {
    public List<String> getCandidateUsers(){
        List<String> users=new ArrayList<>();
        users.add("user1");
        users.add("user2");
        return users;
    }
    
}
 
3. 在流程中传入引用类
 
RuntimeService runtimeService = engine.getRuntimeService();
Deployment de = rs.createDeployment().addClasspathResource("test.bpmn").deploy();
Map<String,Object> vars=new HashMap<>();
vars.put("authService", new AuthService());
ProcessInstance pi =     
    runtimeService.startProcessInstanceByKey(de.getKey(),vars);
List<Task> list2 = taskService.createTaskQuery().taskCandidateUser("user1").list();
List<Task> list2 = taskService.createTaskQuery().taskCandidateUser("user2").list();

 

activiti 动态分配任务候选人

https://blog.csdn.net/ldqchat/article/details/81866073

 

 

package com.hainei.samp.workflow;

import com.hainei.samp.workflow.AuthService;
import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import tk.mybatis.spring.annotation.MapperScan;

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

/**
 * Created with IntelliJ IDEA.
 * User:wq
 * Date:2023/5/9
 * Time: 10:00
 * Description: No Description
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class TestWorkflow {
    @Autowired
    private ProcessEngine processEngine;
    @Test
    public void testDeployment(){
            // 1、创建ProcessEngine
            //        ProcessEngine processEngine =
            //                ProcessEngines.getDefaultProcessEngine();
        // 2、得到RepositoryService实例
        RepositoryService repositoryService =
                processEngine.getRepositoryService();
        // 3、使用RepositoryService进行部署
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("bpmn/test-01.bpmn20.xml") // 添加bpmn资源
                //png资源命名是有规范的。Leave.[key].[png|jpg|gif|svg] 或者Leave. [png|jpg|gif|svg]
                .addClasspathResource("bpmn/test-01.bpmn20.xml") // 添加png资源
                .key("test-01")
                .name("测试流程test-01")
                .deploy();
        // 4、输出部署信息
        System.out.println("流程部署id:" + deployment.getId());
        System.out.println("流程部署名称:" + deployment.getName());
    }


    /**
     * 启动流程实例
     */
    @Test
    public void testStartProcess(){
        // 1、创建ProcessEngine
//        ProcessEngine processEngine =
//                ProcessEngines.getDefaultProcessEngine();
        // 2、获取RunTimeService
        RuntimeService runtimeService = processEngine.getRuntimeService();

        Map<String, Object> map = new HashMap<>();
        map.put("var","bajie");
        map.put("authService",new AuthService());
        map.put("user1","bajie");
        map.put("user2","wukong");
        // 3、根据流程定义Id启动流程
        ProcessInstance processInstance = runtimeService
                .startProcessInstanceByKey("test-01",map);
        // 输出内容
        System.out.println("流程定义id:" +
                processInstance.getProcessDefinitionId());
        System.out.println("流程实例id:" + processInstance.getId());
        System.out.println("当前活动Id:" + processInstance.getActivityId());
    }

    /**
     * 查询当前个人待执行的任务
     */
    @Test
    public void testFindPersonalTaskList() {
// 任务负责人
        String assignee = "bajie";
//        ProcessEngine processEngine =
//                ProcessEngines.getDefaultProcessEngine();
// 创建TaskService
        TaskService taskService = processEngine.getTaskService();
// 根据流程key 和 任务负责人 查询任务
        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("test-01") //流程Key
                .taskAssignee(assignee)//只查询该任务负责人的任务
                .list();
        for (Task task : list) {
            System.out.println("流程实例id:" + task.getProcessInstanceId());
            System.out.println("任务id:" + task.getId());
            System.out.println("任务负责人:" + task.getAssignee());
            System.out.println("任务名称:" + task.getName());
        }
    }

    @Test
    public void completeTask() {
        TaskService taskService = processEngine.getTaskService();
        String taskId = "09813e9ca93a459db974afa8721a373d" ;
       Map<String, Object> map = new HashMap<>();
       map.put("status","request");
        taskService.complete(taskId,map);
        System.out.println("任务完成");
    }

}
View Code

AuthService

package com.hainei.samp.workflow;

import lombok.Data;

import java.io.Serializable;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author : wq
 * Date:2023/9/7
 * Time: 9:31
 * Description: No Description
 */
@Data
public class AuthService implements Serializable {
    private String var;

    public String getCandidateUsers(String var){
        System.out.println(var);
        return var;
    }
}
View Code

 

posted @ 2023-09-07 09:28  wq9  阅读(26)  评论(0编辑  收藏  举报