Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query running instances of a process definition?

Does the camunda engine provides an API to query all running instances of a certain process? Does this query includes suspended instances too?

like image 694
mschoe Avatar asked Apr 24 '14 12:04

mschoe


People also ask

What is an instance of a process?

A process instance is a specific occurrence or execution of a business process. For example, if making a cake is a process, the recipe is the process model. A process instance occurs each time a person makes a cake using this recipe.


1 Answers

You can query all running process instance of a process using the following code:

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;
  }

}

If you want to include even suspended process instance, then just delete the .active() line.

like image 124
Hawky4s Avatar answered Oct 26 '22 04:10

Hawky4s