gpt4 book ai didi

java - 卡蒙达 : get active tasks

转载 作者:行者123 更新时间:2023-11-30 06:52:04 26 4
gpt4 key购买 nike

我如何在 Camunda 中使用的代码中获取 Activity 任务列表?

我已经看过这个答案“How to query the position of a process instance?”,但我不明白什么是“processInstanceId”以及我应该从哪里得到它才能使这个方法起作用。

这是我目前正在尝试的代码:

package org.camunda.bpm;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.model.bpmn.BpmnModelInstance;
import org.camunda.bpm.model.xml.instance.ModelElementInstance;

public class AllActiveActivities {

public Map<String, String> getAllActiveActivities(String processInstanceId) {
// get engine services
ProcessEngine processEngine = BpmPlatform.getDefaultProcessEngine()
RuntimeService runtimeService = processEngine.getRuntimeService();
RepositoryService repositoryService = processEngine.getRepositoryService();

// get the process instance
ProcessInstance processInstance =
runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();

HashMap<String, String> activityNameByActivityId = new HashMap<String, String>();

// get all active activities of the process instance
List<String> activeActivityIds =
runtimeService.getActiveActivityIds(processInstance.getId());

// get bpmn model of the process instance
BpmnModelInstance bpmnModelInstance =
repositoryService.getBpmnModelInstance(processInstance.getProcessDefinitionId());

for (String activeActivityId : activeActivityIds) {
// get the speaking name of each activity in the diagram
ModelElementInstance modelElementById =
bpmnModelInstance.getModelElementById(activeActivityId);
String activityName = modelElementById.getAttributeValue("name");

activityNameByActivityId.put(activeActivityId, activityName);
}

// map contains now all active activities
return activityNameByActivityId;
}

}

预先感谢您的帮助。

最佳答案

以下语句将返回所有 Activity 任务,也可以是独立任务。

processEngine.getTaskService().createTaskQuery().active().list()

如果要获取与特定运行进程对应的所有 Activity 任务,您必须将 .processInstanceId(processInstanceId) 添加到 .list() 之前的语句中. processInstanceId 是标识流程实例的 id。如果您启动一个流程/工作流,将创建一个具有唯一 ID 的流程实例。要获取这些 id,您可以执行以下语句。

int processInstanceId = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processDefinitionKey(processDefinitionKey)
.singleResult()
.getId()

变量processDefinitionKey是在流程定义xml中设置的id。

关于java - 卡蒙达 : get active tasks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39640993/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com