这里我们已知流程已经部署,我的需求是获取当前流程的所有任务节点,我使用instanceof关键字来进行匹配

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
private List<UserTask> getProcessUserTasks(String processInstanceId) {
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel( processInstance.getProcessDefinitionId());
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processInstance.getProcessDefinitionId()).singleResult();
//process中包含所有的节点信息,包括流程线
Process process = bpmnModel.getProcessById(processDefinition.getKey());
//获取流程所有节点信息
Collection<FlowElement> flowElements = process.getFlowElements();
List<UserTask> userTaskList = new ArrayList<>();
//筛选流程中所有的UserTask任务节点
for (FlowElement flowElement : flowElements) {
if (flowElement instanceof UserTask) {
UserTask userTask = ((UserTask) flowElement);
userTaskList.add(userTask);
}
}
return userTaskList;
}