一、前言
本章继续学习activiti6.0的核心api之TaskService 博客已经转至简书 (csdn写博客太痛苦了)
二、activiti6.0的核心api之TaskService
1、复制 testProcess.bpmn20.xml 流程定义文件,并将其改名为 testProcess-task.bpmn20.xml
2、修改 testProcess-task.bpmn20.xml中内容如下:
<process id="myProcess" isClosed="false" isExecutable="true" name="测试流程" processType="None">
<startEvent id="startevent1" name="Start"/>
<endEvent id="endevent1" name="End"/>
<userTask activiti:candidateUsers="张三,李四,王五" activiti:exclusive="true" id="test" name="用户">
<documentation>test task ${message}
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="test"/>
<sequenceFlow id="flow2" sourceRef="test" targetRef="endevent1"/>
</process>
3、测试代码如下:
/**
*
* 测试taskService Task为任务,通常就是我们所说的代办任务
* taskService作用:
* 1、对用户任务(UserTask)进行管理和流程的控制
* a)Task对象的创建和删除
* b)查询Task,并驱动Task节点完成执行
* c)Task相关参数变量(variable)设置
* 2、设置用户任务(UserTask)的权限信息(拥有者,候选人,办理人)
* 3、针对用户任务添加任务附件、任务评论和事件记录
* a)任务附件(Attachment)创建与查询
* b)任务评论(Comment)创建与查询
* c)事件记录(Event)创建与查询
* @author chaoge
* @since 2018/10/8 15:00
*/
public class TaskServiceTest {
private static final LoggerLOGGER = LoggerFactory.getLogger(RuntimeServiceTest.class);
@Rule
public ActivitiRulerule =new ActivitiRule();
/**
* 测试taskService设置和获取变量,完成任务
*/
@Test
@Deployment(resources ="processes/testProcess-task.bpmn20.xml")
public void testTaskService(){
Map variables = Maps.newHashMap();
//可以在此处传递参数,在流程文件中通过${message}的方式接收参数,流程引擎会自动将其替换成设置的参数
variables.put("message","测试信息");
ProcessInstance myProcess =rule.getRuntimeService().startProcessInstanceByKey("myProcess",variables);
TaskService taskService =rule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}",ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
LOGGER.info("task name = {}",task.getName());
LOGGER.info("task des = {}",task.getDescription());
System.out.println("task = " + ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
//设置和获取变量 taskService.getVariables(task.getId())能获取到流程变量和本地变量,即第一种包含了第二种和第三种的变量
Map taskServiceVariables = taskService.getVariables(task.getId());
Map taskServiceVariablesLocal = taskService.getVariablesLocal(task.getId());
Map processVariables =rule.getRuntimeService().getVariables(task.getExecutionId());
LOGGER.info("taskServiceVariables = {}",ToStringBuilder.reflectionToString(taskServiceVariables,ToStringStyle.JSON_STYLE));
LOGGER.info("taskServiceVariablesLocal = {}",ToStringBuilder.reflectionToString(taskServiceVariablesLocal,ToStringStyle.JSON_STYLE));
LOGGER.info("processVariables = {}",ToStringBuilder.reflectionToString(processVariables,ToStringStyle.JSON_STYLE));
//当前流程节点设置完成并走向下一个节点,同时设置参数
Map variables2 = Maps.newHashMap();
variables2.put("user1","chaoge");
taskService.complete(task.getId(),variables2);
Task task1 = taskService.createTaskQuery().taskId(task.getId()).singleResult();
LOGGER.info("task1 = {}",task1);
}
/**
* 其中有两个字段:OWNER_,ASSIGNEE_
* 这两个字段的意义是:
* 1,ASSIGNEE_(受理人):task任务的受理人,就是执行TASK的人,这个又分两种情况(有值,NULL)
* 1),有值的情况:XML流程里面定义的受理人,TASK会直接填入这个人;
* 2),NULL:XML没有指定受理人或者只指定了候选组;
* 没有值的时候,可以使用签收功能去指定受理人,就是候选组里面谁签收谁就成了受理人
* 2,OWNER_(委托人):受理人委托其他人操作该TASK的时候,受理人就成了委托人OWNER_,其他人就成了受理人ASSIGNEE_
*/
@Test
@Deployment(resources ="processes/testProcess-task.bpmn20.xml")
public void testTaskServiceUser(){
Map variables = Maps.newHashMap();
//可以在此处传递参数,在流程文件中通过${message}的方式接收参数,流程引擎会自动将其替换成设置的参数
variables.put("message","测试信息");
ProcessInstance myProcess =rule.getRuntimeService().startProcessInstanceByKey("myProcess",variables);
TaskService taskService =rule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}",ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
LOGGER.info("task des = {}",task.getDescription());
taskService.setOwner(task.getId(),"user1");//owner通常是流程的发起人
//taskService.setAssignee(task.getId(),"张三");//设置代办人,此种方式会覆盖掉原来的代办人,不推荐使用
List taskList = taskService.createTaskQuery()
.taskCandidateUser("张三")//设置了办理用户是张三
.taskUnassigned()//没有设置代办人
.listPage(1, 100);
try {
//当用户已经指定一个代办人的时候claim获取是会报错的
taskList.forEach(task1->taskService.claim(task1.getId(),"张三"));//设置代办人
}catch (Exception e) {
LOGGER.error(e.getMessage(),e);
}
//获取任务相关的办理人,包括owner、candidateUsers
List identityLinks = taskService.getIdentityLinksForTask(task.getId());
identityLinks.forEach(identityLink->LOGGER.info("identityLink = {}",identityLink));
//获取代办人是张三的任务
List tasks = taskService.createTaskQuery().taskAssignee("张三").listPage(0, 100);
tasks.forEach(task1 ->taskService.complete(task1.getId()));
List tasks2 = taskService.createTaskQuery().taskAssignee("张三").listPage(0, 100);
tasks2.forEach(task1 ->LOGGER.info("张三的代办任务task = {}",task1) );
}
/**
* 创建Attachment
*/
@Test
@Deployment(resources ="processes/testProcess-task.bpmn20.xml")
public void testTaskAttachment(){
Map variables = Maps.newHashMap();
//可以在此处传递参数,在流程文件中通过${message}的方式接收参数,流程引擎会自动将其替换成设置的参数
variables.put("message","测试信息");
ProcessInstance myProcess =rule.getRuntimeService().startProcessInstanceByKey("myProcess",variables);
TaskService taskService =rule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}",ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
LOGGER.info("task des = {}",task.getDescription());
taskService.createAttachment("url",task.getId(),task.getProcessInstanceId(),"name","desc","/url/test.png");
List taskAttachments = taskService.getTaskAttachments(task.getId());
taskAttachments.forEach(taskAttach->LOGGER.info("taskAttach = {}",ToStringBuilder.reflectionToString(taskAttach,ToStringStyle.JSON_STYLE)));
}
/**
* 创建Comment 查询事件信息
*/
@Test
@Deployment(resources ="processes/testProcess-task.bpmn20.xml")
public void testTaskComment(){
Map variables = Maps.newHashMap();
//可以在此处传递参数,在流程文件中通过${message}的方式接收参数,流程引擎会自动将其替换成设置的参数
variables.put("message","测试信息");
ProcessInstance myProcess =rule.getRuntimeService().startProcessInstanceByKey("myProcess",variables);
TaskService taskService =rule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}",ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
LOGGER.info("task des = {}",task.getDescription());
//添加评论
taskService.addComment(task.getId(),task.getProcessInstanceId(),"这是我的评论");
taskService.addComment(task.getId(),task.getProcessInstanceId(),"这还是我的评论");
//查询评论
List taskComments = taskService.getTaskComments(task.getId());
taskComments.forEach(taskComment->LOGGER.info("taskComment = {}",ToStringBuilder.reflectionToString(taskComment,ToStringStyle.JSON_STYLE)));
//查询事件
List taskEvents = taskService.getTaskEvents(task.getId());
taskEvents.forEach(taskEvent->LOGGER.info("taskEvent = {}",ToStringBuilder.reflectionToString(taskEvent,ToStringStyle.JSON_STYLE)));
}
}
网友评论