如何查询正在运行的进程定义实例?

camunda 引擎是否提供 API 来查询某个进程的所有正在运行的实例?此查询是否也包括挂起的实例?


答案 1

您可以使用以下代码查询流程的所有正在运行的流程实例:

package org.camunda.bpm;

import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import java.util.List;

public class AllRunningProcessInstances {

  public List<ProcessInstance> getAllRunningProcessInstances(String processDefinitionName) {
    // get process engine and services
    ProcessEngine processEngine = BpmPlatform.getDefaultProcessEngine();
    RuntimeService runtimeService = processEngine.getRuntimeService();
    RepositoryService repositoryService = processEngine.getRepositoryService();

    // query for latest process definition with given name
    ProcessDefinition myProcessDefinition =
        repositoryService.createProcessDefinitionQuery()
            .processDefinitionName(processDefinitionName)
            .latestVersion()
            .singleResult();

    // list all running/unsuspended instances of the process
    List<ProcessInstance> processInstances =
        runtimeService.createProcessInstanceQuery()
            .processDefinitionId(myProcessDefinition.getId())
            .active() // we only want the unsuspended process instances
            .list();

    return processInstances;
  }

}

如果要包含挂起的流程实例,则只需删除 .active() 行即可。


答案 2

推荐