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表,只是它们标记的类型有些区别。

任务附件测试类:

@Test
    @Deployment(resources = {"my-process_task.bpmn20.xml"})
    public void testTaskAttachment(){
        //构建启动流程传入的参数变量
        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();

        //这里createAttachment()有两种重载,一种是需要输入流,上传附件,另一种是String,url、备注等。
        taskService.createAttachment("url",
                task.getId(),task.getProcessInstanceId(),
                "name","desc","/url/test.png");

        //可以做查询操作,从数据库查询出我们附件相关的信息,(ps:task本身的查询没有分页的功能,所以不要给task上传太多附件,增加了压力)
        List<Attachment> taskAttachments = taskService.getTaskAttachments(task.getId());
        for (Attachment taskAttachment : taskAttachments) {
            LOGGER.info("taskAttachment = {}",ToStringBuilder.reflectionToString(taskAttachment,ToStringStyle.JSON_STYLE));
        }
    }

输出结果如下:

taskAttachment = {"name":"name","description":"desc","type":"url","taskId":"22510","processInstanceId":"22505","url":"/url/test.png","contentId":null,"content":null,"userId":null,"time":"Thu Apr 11 16:31:07 CST 2019","id":"22517","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}

所以可以看到,任务附件这个功能只是对一个实体对象的增加和查询。

下面是测试评论相关的测试类:

 @Test
    @Deployment(resources = {"my-process_task.bpmn20.xml"})
    public void testTaskComment(){
        //构建启动流程传入的参数变量
        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();

        //与附件不同的是 任务评论 这里是addComment方法
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"hello 1");
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"hello 2");

        //做完记录操作以后,就可以进行读取操作了
        List<Comment> taskComments = taskService.getTaskComments(task.getId());
        for (Comment taskComment : taskComments) {
            LOGGER.info("taskComment = {}",ToStringBuilder.reflectionToString(taskComment,ToStringStyle.JSON_STYLE));
        }

        //事件记录相关
        //这里直接获取一个事件列表,查看一下它的结构先
        List<Event> taskEvents = taskService.getTaskEvents(task.getId());
        for (Event taskEvent : taskEvents) {
            LOGGER.info("taskEvent = {}",ToStringBuilder.reflectionToString(taskEvent,ToStringStyle.JSON_STYLE));
        }
    }

输出结果:

taskComment = {"type":"comment","userId":null,"time":"Thu Apr 11 17:00:08 CST 2019","taskId":"27510","processInstanceId":"27505","action":"AddComment","message":"hello 1","fullMessage":"hello 1","id":"27517","isInserted":false,"isUpdated":false,"isDeleted":false}
taskComment = {"type":"comment","userId":null,"time":"Thu Apr 11 17:00:08 CST 2019","taskId":"27510","processInstanceId":"27505","action":"AddComment","message":"hello 2","fullMessage":"hello 2","id":"27518","isInserted":false,"isUpdated":false,"isDeleted":false}
taskEvent = {"type":"comment","userId":null,"time":"Thu Apr 11 17:00:08 CST 2019","taskId":"27510","processInstanceId":"27505","action":"AddComment","message":"hello 1","fullMessage":"hello 1","id":"27517","isInserted":false,"isUpdated":false,"isDeleted":false}
taskEvent = {"type":"comment","userId":null,"time":"Thu Apr 11 17:00:08 CST 2019","taskId":"27510","processInstanceId":"27505","action":"AddComment","message":"hello 2","fullMessage":"hello 2","id":"27518","isInserted":false,"isUpdated":false,"isDeleted":false}

所以可以看出:

对于TaskService有很多操作,在做操作中TaskEvent会去做记录,而TaskComment不会记录,因为在这个单元测试里面,我们只是做了一个评论操作,所以二者输出内容几乎一模一样,但是若用TaskService做一些其他操作,那么TaskEvent会明显比TaskComment 记录的多很多。Task的每一个变化,TaskEvent都会悄悄记录起来。

原文地址:https://www.cnblogs.com/xk920/p/10690015.html