如何查询流程定义的已完成实例?

时间:2014-04-24 13:00:12

标签: java bpm bpmn camunda

camunda引擎是否提供API来查询流程的所有已完成实例?

1 个答案:

答案 0 :(得分:5)

您可以使用以下代码获取流程的所有已完成实例:

package org.camunda.bpm;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.ProcessEngine;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import java.util.List;

public class AllFinishedProcessInstances {

  public List<HistoricProcessInstance> getAllFinishedProcessInstances(String processDefinitionName) {
    ProcessEngine processEngine = BpmPlatform.getDefaultProcessEngine();
    HistoryService historyService = processEngine.getHistoryService();;
    RepositoryService repositoryService = processEngine.getRepositoryService();

    ProcessDefinition myProcessDefinition =
        repositoryService.createProcessDefinitionQuery()
            .processDefinitionName(processDefinitionName)
            .latestVersion() // we are only interested in the latest version
            .singleResult();

    List<HistoricProcessInstance> processInstances =
        historyService.createHistoricProcessInstanceQuery()
            .processDefinitionId(myProcessDefinition.getId())
            .finished() // we only want the finished process instances
            .list();

    return processInstances;
  }

}