如何查询流程定义的运行实例?

时间:2014-04-24 12:36:24

标签: java bpm bpmn camunda

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

1 个答案:

答案 0 :(得分:16)

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

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()行。