Activiti6-TaskService
Posted xk920
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Activiti6-TaskService相关的知识,希望对你有一定的参考价值。
任务管理服务:
可以看出来,TaskService操作对象,主要针对于UserTask,
对于业务方来说,最重要的就是用户任务,可以对用户任务进行增删改查的管理、可以对相关流程的控制、也可以设置一些用户任务的权限信息、也可以针对用户审批中的一些附加信息。
对Task进行输出,看一下其内部的数据格式:
<?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" />--> <!--定时任务来启动流程--> <startEvent id="start"> <timerEventDefinition> <!-- 共执行5次,间隔为10S --> <timeCycle>R5/PT10S</timeCycle> </timerEventDefinition> </startEvent> <sequenceFlow id="flow1" sourceRef="start" targetRef="someTask" /> <!-- 进行修改,如下 --> <userTask id="someTask" name="Activiti is awesome!" activiti:candidateUsers="lyf,user1,user2"> <documentation> some Task ${message} </documentation> </userTask> <sequenceFlow id="flow2" sourceRef="someTask" targetRef="end" /> <endEvent id="end" /> </process> </definitions>
测试类中输出tostring后的Task,看下它的数据格式:
public class TaskServiceTest { private static final Logger LOGGER = LoggerFactory.getLogger(RepostoryServiceTest.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!!!-----"); //启动流程,传入这个变量 activitiRule .getRuntimeService() .startProcessInstanceByKey("my-process",variables); TaskService taskService = activitiRule.getTaskService(); Task task = taskService.createTaskQuery().singleResult(); //将task tostring并输出 LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE)); LOGGER.info("task.desctiption = {}",task.getDescription()); } }
打印输出结果如下:
task = {"owner":null,"assigneeUpdatedCount":0,"originalAssignee":null,"assignee":null,"delegationState":null,"parentTaskId":null,"name":"Activiti is awesome!","localizedName":null,"description":"some Task ---my test message!!!-----","localizedDescription":null,"priority":50,"createTime":"Thu Apr 11 14:50:55 CST 2019","dueDate":null,"suspensionState":1,"category":null,"isIdentityLinksInitialized":false,"taskIdentityLinkEntities":[],"executionId":"15007","execution":null,"processInstanceId":"15005","processInstance":null,"processDefinitionId":"my-process:3:15003","taskDefinitionKey":"someTask","formKey":null,"isDeleted":false,"isCanceled":false,"eventName":null,"currentActivitiListener":null,"tenantId":"","queryVariables":null,"forcedUpdate":false,"claimTime":null,"variableInstances":null,"usedVariablesCache":{},"transientVariabes":null,"cachedElContext":null,"id":"15010","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false} task.desctiption = some Task ---my test message!!!-----
驱动Task节点完成执行的操作:
taskService.complete(task.getId());
TaskService设置Task权限信息:
测试类:
@Test @Deployment(resources = {"my-process_task.bpmn20.xml"}) public void testTaskServiceUser(){ //构建启动流程传入的参数变量 Map<String,Object> variables = Maps.newHashMap(); variables.put("message","---my test message!!!-----"); //启动流程,传入这个变量 activitiRule .getRuntimeService() .startProcessInstanceByKey("my-process",variables); TaskService taskService = activitiRule.getTaskService(); Task task = taskService.createTaskQuery().singleResult(); //将task tostring并输出 LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE)); LOGGER.info("task.desctiption = {}",task.getDescription()); //指定owner taskService.setOwner(task.getId(),"user1"); //指定代办人,这种操作会使某个人的待办任务突然消失,被其他人替代 // taskService.setAssignee(task.getId(),"jimmy"); //通过查看已经指定了候选人,但并没有指定确认是某个人办理的情况, List<Task> taskList = taskService .createTaskQuery() .taskCandidateUser("lyf") .taskUnassigned() .listPage(0, 100); //遍历一下 for (Task task1 : taskList) { try{ taskService.claim(task1.getId(),"lyf"); }catch (Exception e){ LOGGER.error(e.getMessage(),e); } } //查询一下指定的task与多少用户相关 List<IdentityLink> identityLinksForTask = taskService.getIdentityLinksForTask(task.getId()); //返回的这个list是构建了一个用户或组与一个task 的关系 for (IdentityLink identityLink : identityLinksForTask) { LOGGER.info("identityLink = {}",identityLink); } }
测试输出结果:
task = {"owner":null,"assigneeUpdatedCount":0,"originalAssignee":null,"assignee":null,"delegationState":null,"parentTaskId":null,"name":"Activiti is awesome!","localizedName":null,"description":"some Task ---my test message!!!-----","localizedDescription":null,"priority":50,"createTime":"Thu Apr 11 15:54:39 CST 2019","dueDate":null,"suspensionState":1,"category":null,"isIdentityLinksInitialized":false,"taskIdentityLinkEntities":[],"executionId":"20007","execution":null,"processInstanceId":"20005","processInstance":null,"processDefinitionId":"my-process:3:20003","taskDefinitionKey":"someTask","formKey":null,"isDeleted":false,"isCanceled":false,"eventName":null,"currentActivitiListener":null,"tenantId":"","queryVariables":null,"forcedUpdate":false,"claimTime":null,"variableInstances":null,"usedVariablesCache":{},"transientVariabes":null,"cachedElContext":null,"id":"20010","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false} task.desctiption = some Task ---my test message!!!----- identityLink = IdentityLinkEntity[id=20011, type=candidate, userId=lyf, taskId=20010] identityLink = IdentityLinkEntity[id=20013, type=candidate, userId=user1, taskId=20010] identityLink = IdentityLinkEntity[id=20015, type=candidate, userId=user2, taskId=20010] identityLink = IdentityLinkEntity[id=null, type=assignee, userId=lyf, taskId=20010] identityLink = IdentityLinkEntity[id=null, type=owner, userId=user1, taskId=20010]
TaskService 设置Task附加信息:
什么情况会用到任务附件(Attachment)呢?
遇到了报销凭证、下班发车电子发票一类的。如果上传附件的话,他是通过一个二进制流存储起来。
任务评论(Comment)呢?
当我提交一个UserTask的时候,除了我们业务上需要的数据,可能还需要加一些备注、评论这些内容也可以基于Task去设置。
事件记录(Event)?
其实对于任务评论还是事件记录它都对应着数据库的一个comment表,只是它们标记的类型有些区别。
以上是关于Activiti6-TaskService的主要内容,如果未能解决你的问题,请参考以下文章