Activiti6.0流程引擎学习——(22)activiti的任务管理服务(TaskService)

这里介绍activiti中的任务管理服务,也就是其中的TaskService。

TaskService功能:

1、对用户任务(UserTask)管理和流程的控制;

2、设置用户任务(UserTask)的权限信息(拥有者,候选人,办理人);

3、针对用户任务添加任务附件、任务评论和事件记录。

TaskService对Task管理与流程控制:

1、Task对象的创建,删除。但是很少使用TaskService手动创建Task,因为一般Task创建通过流程定义。

2、查询Task,并驱动Task节点完成执行。

3、Task相关参数变量(variable)设置。其中本地变量,只能基于TaskId获取,在整个流程过程中使不可变的。

 

接下去我们开始测试TaskService:

一、首先创建TaskServiceTest测试类与my-process-task.bpmn20.xml流程文件:

具体文件路径如下:

Activiti6.0流程引擎学习——(22)activiti的任务管理服务(TaskService)_第1张图片

其中my-process-task.bpmn20.xml内容如下:





	

		
		
		
		   
			some Task ${message}   
		
		

		

	

这个流程文件可以获取到someTask的描述信息,message就会根据上下文变量名传过来的message变量值替换。

 

二、TaskService对Task的查询与设置变量

TaskServiceTest测试类中的测试方法:

    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskService(){
        Map variables = Maps.newHashMap();
        variables.put("message","my test message . . .");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process",variables);   //启动流程
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();//获取唯一的当前流程暂停的结点
        LOGGER.info("task = [{}]" , ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));   //使用json格式输出
        LOGGER.info("task.description = [{}]" , task.getDescription());   //输出配置描述信息

        taskService.setVariable(task.getId(),"key1","value1");    //给task设置普通变量
        taskService.setVariableLocal(task.getId(),"localKey1","localValue1");   //给task设置本地变量

        Map taskServiceVariables = taskService.getVariables(task.getId());    //获取变量
        Map taskServiceVariablesLocal = taskService.getVariablesLocal(task.getId());    //获取本地变量

        Map processVariables = activitiRule.getRuntimeService().getVariables(task.getExecutionId());   //根据执行流获取变量

        LOGGER.info("taskServiceVariables = [{}]",taskServiceVariables);   //{key1=value1, localKey1=localValue1, message=my test message . . .}
        LOGGER.info("taskServiceVariablesLocal = [{}]",taskServiceVariablesLocal);   //{localKey1=localValue1}
        LOGGER.info("processVariables = [{}]",processVariables);   //{key1=value1, message=my test message . . .}

        Map completeVar = Maps.newConcurrentMap();
        completeVar.put("cKey1","cValue1");
        taskService.complete(task.getId());   //将流程驱动向下结点执行

        Task task1 = taskService.createTaskQuery().taskId(task.getId()).singleResult();    //测试看看流程有没有向下执行,若向下执行则为空
        LOGGER.info("Task = [{}]" , task1);    //null
    }

输出结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
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":"Sat Feb 16 12:21:25 CST 2019","dueDate":null,"suspensionState":1,"category":null,"isIdentityLinksInitialized":false,"taskIdentityLinkEntities":[],"executionId":"6","execution":null,"processInstanceId":"4","processInstance":null,"processDefinitionId":"my-process:1:3","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":"9","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]
task.description = [some Task my test message . . .]
taskServiceVariables = [{key1=value1, localKey1=localValue1, message=my test message . . .}]
taskServiceVariablesLocal = [{localKey1=localValue1}]
processVariables = [{key1=value1, message=my test message . . .}]
Task = [null]

Process finished with exit code 0

首先按照json格式输出task信息,然后输出了task的描述信息。在输出全部变量、本地变量、执行流获取的变量。最后将流程执行下去,输出是否还有没有Task事件。

 

三、TaskService对Task事件权限的配置

TaskService设置Task权限信息:

1、候选用户(candidateUser)和候选组(candidateGroup);

2、指定拥有人(Owner)和办理人(Assignee);

3、通过claim设置办理人。

单元测试方法如下:

    /**
     * Task事件权限的配置
     */
    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskServiceUser(){
        Map variables = Maps.newHashMap();
        variables.put("message","my test message . . .");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process",variables);   //启动流程
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();    //获取唯一的当前流程暂停的结点
        LOGGER.info("task = [{}]" , ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));   //使用json格式输出
        LOGGER.info("task.description = [{}]" , task.getDescription());   //输出配置描述信息

        taskService.setOwner(task.getId(),"user1");   //设置task的发起人owner,这里是user1用户
        //taskService.setAssignee(task.getId(),"jjf");      //设置task事件的代办人,但是没有对属性进行校验,会发生权限冲突,不推荐
        List taskList = taskService.createTaskQuery()
                .taskCandidateUser("jjf").taskUnassigned().listPage(0, 100);   //指定了 jjf 候选人,但是taskUnassigned没有被指定某人个

        for (Task task1:taskList){
            try{
                taskService.claim(task1.getId(),"jjf");   //claim时发现指定了代办人则会报错,所以要捕获异常
            }catch (Exception e){
                LOGGER.error(e.getMessage(),e);    //打印异常日志
            }
        }
        List identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());   //查询指定的task与多少用户相关
        for (IdentityLink identityLink:identityLinksForTask){
            LOGGER.info("identityLink = [{}]" , identityLink);
        }

        List tasks = taskService.createTaskQuery().taskAssignee("jjf").listPage(0, 100);  //将用户任务向下继续执行

        for (Task task1:tasks){
            Map vars = Maps.newHashMap();
            vars.put("skey1","cvalue1");
            taskService.complete(task1.getId(),vars);
        }

        LOGGER.info("task是否为空 [{}]", CollectionUtils.isEmpty(tasks));
        tasks = taskService.createTaskQuery().taskAssignee("jjf").listPage(0, 100);
        LOGGER.info("task是否为空 [{}]", CollectionUtils.isEmpty(tasks));

    }

测试结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
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 Dec 27 17:39:32 CST 2018","dueDate":null,"suspensionState":1,"category":null,"isIdentityLinksInitialized":false,"taskIdentityLinkEntities":[],"executionId":"6","execution":null,"processInstanceId":"4","processInstance":null,"processDefinitionId":"my-process:1:3","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":"9","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]
task.description = [some Task my test message . . .]
identityLink = [IdentityLinkEntity[id=10, type=candidate, userId=jjf, taskId=9]]
identityLink = [IdentityLinkEntity[id=12, type=candidate, userId=user1, taskId=9]]
identityLink = [IdentityLinkEntity[id=14, type=candidate, userId=user2, taskId=9]]
identityLink = [IdentityLinkEntity[id=null, type=assignee, userId=jjf, taskId=9]]
identityLink = [IdentityLinkEntity[id=null, type=owner, userId=user1, taskId=9]]
task是否为空 [false]
task是否为空 [true]

Process finished with exit code 0

identityLink有五条数据,上面三条有id分别为10,12,14,类型都是candidate候选人,这三条数据都是流程定义文件中设置的,当启动流程时就把这些设置内容序列化到数据库中,作为实体有指定的ID。后面的两条identityLink并没有id,类型是指定的代办人与所有者,就是测试方法中设置的身份。

 

四、测试Task添加附件

TaskService设置Task附加信息:

1、任务附件(Attachment)创建与查询,上传附件会基于二进制流存储起来;

2、任务评论(Comment)创建与查询;

3、事件记录(Event)创建与查询。

单元测试方法如下:

    /**
     * 测试Task添加附件(Attachment)
     */
    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskAttachment(){
        Map variables = Maps.newHashMap();
        variables.put("message","my test message . . .");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process",variables);   //启动流程
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();//获取唯一的当前流程暂停的结点
        taskService.createAttachment("url",task.getId(),task.getProcessInstanceId()
                ,"name","description描述","/url/test.png");    //创建一个附件

        List taskAttachments = taskService.getTaskAttachments(task.getId());

        for (Attachment taskAttachment : taskAttachments){
            LOGGER.info("taskAttachment = [{}]",
                    ToStringBuilder.reflectionToString(taskAttachment,ToStringStyle.JSON_STYLE));      //使用json格式输出
        }
    }

输出结果如下:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
taskAttachment = [{"name":"name","description":"description描述","type":"url","taskId":"9","processInstanceId":"4","url":"/url/test.png","contentId":null,"content":null,"userId":null,"time":"Fri Dec 28 10:09:52 CST 2018","id":"16","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false}]

Process finished with exit code 0

按照json格式输出附件内容。

 

五、测试Task添加评论

单元测试方法如下:

    /**
     * 测试Task添加评论(Comment)
     */
    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskComment(){
        Map variables = Maps.newHashMap();
        variables.put("message","my test message . . .");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process",variables);   //启动流程
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();//获取唯一的当前流程暂停的结点
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"comment 1");    //创建一个评论,参数为事件id  流程实例id  评论内容
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"comment 2");

        List taskComments = taskService.getTaskComments(task.getId());

        for (Comment taskComment : taskComments){
            LOGGER.info("taskComment = [{}]",
                    ToStringBuilder.reflectionToString(taskComment,ToStringStyle.JSON_STYLE));      //使用json格式输出任务评论
        }
    }

输出结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
taskComment = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:21:58 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 2","fullMessage":"comment 2","id":"17","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskComment = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:21:58 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 1","fullMessage":"comment 1","id":"16","isInserted":false,"isUpdated":false,"isDeleted":false}]

Process finished with exit code 0

 

六、Task的评论与事件记录比较

    /**
     * 测试Task添加事件记录(Event)
     */
    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskEvent(){
        Map variables = Maps.newHashMap();
        variables.put("message","my test message . . .");
        activitiRule.getRuntimeService().startProcessInstanceByKey("my-process",variables);   //启动流程
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();//获取唯一的当前流程暂停的结点
        taskService.setOwner(task.getId(),"user1");   //设置task的发起人owner,这里是user1用户
        taskService.setAssignee(task.getId(),"jjf");      //设置task事件的代办人
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"comment 1");    //创建一个评论,参数为事件id  流程实例id  评论内容
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"comment 2");

        List taskComments = taskService.getTaskComments(task.getId());

        for (Comment taskComment : taskComments){
            LOGGER.info("taskComment = [{}]",
                    ToStringBuilder.reflectionToString(taskComment,ToStringStyle.JSON_STYLE));      //使用json格式输出任务评论
        }

        List taskEvents = taskService.getTaskEvents(task.getId());    //获取事件记录
        for (Event taskEvent : taskEvents){
            LOGGER.info("taskEvent = [{}]",ToStringBuilder.reflectionToString(taskEvent,ToStringStyle.JSON_STYLE));
        }

    }

输出结果:

Loading XML bean definitions from class path resource [activiti.cfg.xml]
Activiti 5 compatibility handler implementation not found or error during instantiation : org.activiti.compatibility.DefaultActiviti5CompatibilityHandler. Activiti 5 backwards compatibility disabled.
performing create on engine with resource org/activiti/db/create/activiti.h2.create.engine.sql
performing create on history with resource org/activiti/db/create/activiti.h2.create.history.sql
performing create on identity with resource org/activiti/db/create/activiti.h2.create.identity.sql
ProcessEngine default created
taskComment = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 2","fullMessage":"comment 2","id":"19","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskComment = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 1","fullMessage":"comment 1","id":"18","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskEvent = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 2","fullMessage":"comment 2","id":"19","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskEvent = [{"type":"comment","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":"4","action":"AddComment","message":"comment 1","fullMessage":"comment 1","id":"18","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskEvent = [{"type":"event","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":null,"action":"AddUserLink","message":"jjf_|_assignee","fullMessage":null,"id":"17","isInserted":false,"isUpdated":false,"isDeleted":false}]
taskEvent = [{"type":"event","userId":null,"time":"Fri Dec 28 10:44:38 CST 2018","taskId":"9","processInstanceId":null,"action":"AddUserLink","message":"user1_|_owner","fullMessage":null,"id":"16","isInserted":false,"isUpdated":false,"isDeleted":false}]

Process finished with exit code 0

可以看出event的前两条数据与comment数据一样,但是后面两条comment并没有。其实我们可以对useTask做很多操作,操作的过程中event会记录下来,但是comment并不会记录,后两条因为我在测试方法中设置了代办人与拥有人,event都会记录下来。

你可能感兴趣的:(activiti)