TaskService
TaskService为任务管理服务
1.提供的功能:
-
对用户任务(UserTask)管理和流程的控制
-
设置用户任务(UserTask)的权限信息(拥有者,候选人,办理人)
-
针对用户任务添加任务附件、任务评论和事件记录
2.TaskService如何对Task管理与流程控制
-
Task对象的创建,删除
-
查询Task,并驱动Task节点完成执行
-
Task相关参数变量(variable)设置
流程定义文件
我们在流程定义文件中定义了一个用户任务someTask
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema"
expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="my-process">
<startEvent id="start" />
<sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" />
<userTask id="someTask" name="Activiti is awesome!"
activiti:candidateUsers="syc, user1, user2">
<!--documentation对userTask进行描述,message根据上下文传入的变量值进行替换-->
<documentation>some Task ${message}</documentation>
</userTask>
<sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" />
<endEvent id="end" />
</process>
</definitions>
TaskService对Task的创建,查询并驱动Task结点完成执行,Task相关参数变量设置
public class TaskServiceTest {
private static final Logger LOGGER = LoggerFactory.getLogger(TaskServiceTest.class);
@Rule
public ActivitiRule activitiRule = new ActivitiRule();
@Test
@Deployment(resources = {"my-process-task.bpmn20.xml"})
public void testTaskService(){
Map<String, Object> variables = Maps.newHashMap();
variables.put("message", "my test message !!!");
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
TaskService taskService = activitiRule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
LOGGER.info("task.description = {}", task.getDescription());
//对变量进行操作
taskService.setVariable(task.getId(),"key1","value1");
taskService.setVariableLocal(task.getId(), "localkey1", "localvalue1");
//获取变量的三种方式
Map<String, Object> taskServiceVariables = taskService.getVariables(task.getId());
Map<String, Object> taskServiceVariablesLocal = taskService.getVariablesLocal(task.getId());
Map<String, Object> processVariables1 = activitiRule.getRuntimeService().getVariables(task.getExecutionId());
LOGGER.info("taskServiceVariables = {}",taskServiceVariables);
LOGGER.info("taskServiceVariablesLocal = {}", taskServiceVariablesLocal);
LOGGER.info("processVariables1 = {}",processVariables1);
//task完成后,打印输出的结果为null
Map<String, Object> completeVar = Maps.newConcurrentMap();
completeVar.put("ckey1", "cValue1");
taskService.complete(task.getId(), completeVar);
Task task1 = taskService.createTaskQuery().taskId(task.getId()).singleResult();
LOGGER.info("task1 = {}", task1);
}
}
实现流程与权限的设置的相关操作
TaskService设置Task权限信息
-
候选用户(candidateUser)和候选组(candidateGroup)
-
指定拥有人(Ower)和办理人(Assignee)
-
通过claim设置办理人(也可以使用setAssignee方式,但是不检查权限直接修改)
@Test
@Deployment(resources = {"my-process-task.bpmn20.xml"})
public void testTaskServiceUser(){
Map<String, Object> variables = Maps.newHashMap();
variables.put("message", "my test message !!!");//这个message对应的值my test message就是流程定义文件中的message值
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
TaskService taskService = activitiRule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
LOGGER.info("task.description = {}", task.getDescription());//描述为documentation的内容
taskService.setOwner(task.getId(), "user1");//办理人,流程的发起人
//taskService.setAssignee(task.getId(), "syc");//代办人,不检查权限直接修改
List<Task> taskList = taskService.createTaskQuery().taskCandidateUser("syc").taskUnassigned().listPage(0, 100);//指定自己为候选人但这个task没有指定代办人
for(Task task1 : taskList){
try {
taskService.claim(task1.getId(), "syc");//设置代办人
} catch (Exception e) {
LOGGER.error(e.getMessage(),e);
}
}
//查询task与用户相关的关系
List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
for(IdentityLink identityLink : identityLinksForTask){
LOGGER.info("identityLink = {}",identityLink);
}
//继续向后执行,执行自己有权限的task
List<Task> sycstask = taskService.createTaskQuery().taskAssignee("syc").listPage(0, 100);//当前办理人是自己的流程
for(Task syc : sycstask){//让流程完成
Map<String, Object> vars = Maps.newHashMap();
vars.put("ckey1", "cvalue1");
taskService.complete(syc.getId(), vars);
LOGGER.info("syc = {}",syc);
}
sycstask = taskService.createTaskQuery().taskAssignee("syc").listPage(0, 100);
LOGGER.info("是否存在 {}", CollectionUtils.isEmpty(sycstask));
}
TaskService设置Task附加信息
-
任务附件(Attachment)创建与查询;
-
任务评论(Comment)创建与查询;
-
事件记录(Event)创建与查询;
任务附件
@Test
@Deployment(resources = {"my-process-task.bpmn20.xml"})
public void testTaskAttachment() {
Map<String, Object> variables = Maps.newHashMap();
variables.put("message", "my test message!");
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
TaskService taskService = activitiRule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
taskService.createAttachment("url", task.getId(), task.getProcessInstanceId(), "name", "desc", "url/test.png");
List<Attachment> taskAttachments = taskService.getTaskAttachments(task.getId());
for(Attachment taskAttachment : taskAttachments){
LOGGER.info("taskAttachment = {}", ToStringBuilder.reflectionToString(taskAttachment, ToStringStyle.JSON_STYLE));
}
}
评论与事件记录
@Test
@Deployment(resources = {"my-process-task.bpmn20.xml"})
public void testTaskComment() {
Map<String, Object> variables = Maps.newHashMap();
variables.put("message", "my test message!");
ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
TaskService taskService = activitiRule.getTaskService();
Task task = taskService.createTaskQuery().singleResult();
//taskService.createAttachment("url", task.getId(), task.getProcessInstanceId(), "name", "desc", "url/test.png");
taskService.setOwner(task.getId(), "user1");
taskService.setAssignee(task.getId(), "syc");
taskService.addComment(task.getId(),task.getProcessInstanceId(),"message 1");
taskService.addComment(task.getId(),task.getProcessInstanceId(),"message 2");
List<Comment> taskComments = taskService.getTaskComments(task.getId());
for(Comment taskComment : taskComments){
LOGGER.info("comment1 = {}", ToStringBuilder.reflectionToString(taskComment));
LOGGER.info("comment2 = {}", ToStringBuilder.reflectionToString(taskComment));
}
List<Event> taskEvents = taskService.getTaskEvents(task.getId());
for(Event taskEvent : taskEvents){
LOGGER.info("taskEvent = {}", ToStringBuilder.reflectionToString(taskEvent));
}
}