工作流3-API(2)
文章目录
TaskService
参考:https://blog.****.net/luckyzhoustar/article/details/48630555
- 新建一个
userTask bpmn
,设置两个候选人:
<?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:xsd="http://www.w3.org/2001/XMLSchema" 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="mySimple1" name="我的申请1" isExecutable="true">
<startEvent id="startEvent" name="开始"></startEvent>
<userTask id="userApply" name="测试1" activiti:candidateUsers="whb,user1">
<documentation>some task ${message}</documentation>
</userTask>
<sequenceFlow id="flow1" sourceRef="startEvent" targetRef="userApply"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="userApply" targetRef="endevent1"></sequenceFlow>
</process>
</definitions>
在userTask上配置了一个变量占位符 ${message}
。
//流程引擎对象
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//TaskService
@Test
public void testTaskService(){
//1.部署流程定义
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagrams/Simple1.bpmn")
.deploy();
logger.info("deployment信息id:{}",deployment.getId());//deployment信息id:45001
Map<String,Object> variables = Maps.newHashMap();
variables.put("message","testMessage!");
//2.启动流程实例,携带变量
ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey("mySimple1",variables);
logger.info("流程实例ID:{}", processInstance.getId());//流程实例ID:45004
logger.info("流程定义ID:{}",processInstance.getProcessDefinitionId());//流程定义ID:mySimple1:1:45003
//3.查询task
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
logger.info("task:{}",ToStringBuilder.reflectionToString(task,ToStringStyle.JSON_STYLE));
//流程中的${message}变量替换了
//task:{"revision":1,"owner":null,"assignee":null,"initialAssignee":null,"delegationState":null,"parentTaskId":null,"name":"测试1","localizedName":null,"description":"some task testMessage!","localizedDescription":null,"priority":50,"createTime":"Thu Mar 14 16:49:44 CST 2019","dueDate":null,"suspensionState":1,"category":null,"isIdentityLinksInitialized":false,"taskIdentityLinkEntities":[],"executionId":"52504","execution":null,"processInstanceId":"52504","processInstance":null,"processDefinitionId":"mySimple1:4:52503","taskDefinition":null,"taskDefinitionKey":"userApply","formKey":null,"isDeleted":false,"eventName":null,"tenantId":"","queryVariables":null,"forcedUpdate":false,"variableInstances":null,"usedVariablesCache":{},"cachedElContext":null,"id":"52508"}
//4.设置变量,并查看本地变量等
taskService.setVariable(task.getId(),"变量名1","值1");
taskService.setVariableLocal(task.getId(),"本地变量名1","本地值1");
Map<String, Object> variables1 = taskService.getVariables(task.getId());
Map<String, Object> variablesLocal = taskService.getVariablesLocal(task.getId());
Map<String, Object> processVariables = processEngine.getRuntimeService().getVariables(task.getExecutionId());
//三种变量的范围
logger.info("-- variables1:{}",variables1);//范围大:task变量都能获得
logger.info("-- variablesLocal:{}",variablesLocal);//只能获取设置到的local变量
logger.info("-- processVariables:{}",processVariables);//除了local
//-- variables1:{变量名1=值1, 本地变量名1=本地值1, message=testMessage!}
//-- variablesLocal:{本地变量名1=本地值1}
//-- processVariables:{变量名1=值1, message=testMessage!}
//当taskComplete后,task将为null
taskService.complete(task.getId());
}