Soleferry
Soleferry

Reputation: 33

Camunda : get active tasks

How can i get a list of active tasks in the code i use in Camunda ?

I already look at this answer "How to query the position of a process instance?" but i can't understand what is "processInstanceId" and where i am supposed to get it to make this method works.

Here's the code i'm currently trying :

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

}

Thanks in advance for your help.

Upvotes: 2

Views: 11511

Answers (2)

Zelldon
Zelldon

Reputation: 5516

The following statement will return all active tasks, which can be also standalone tasks.

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

If you want to get all active tasks corresponding to a specific running process you have to add the .processInstanceId(processInstanceId) to the statement before the .list(). The processInstanceId is the id which identifies the process instance. If you start a process/workflow a process instance will be created with an unique id. To get these id you can for example execute the following statement.

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

The variable processDefinitionKey is the id which is set in the process definition xml.

Upvotes: 4

Greg Harley
Greg Harley

Reputation: 3240

A process instance is a currently executing "instance" of a process definition. Put another way, every time you start a process you create a process "instance".

So, for any given process definition, you may have a number of "instances" and each "instance" may have one of more active "tasks" (sometimes service tasks, sometimes user tasks and sometimes other wait states - e.g. timer).

Before I can answer your question, I need to understand what it is you are trying to retrieve.

Are you looking for all tasks for a a given process definition? If so, this equates to all of the tasks for all of the "instances". For this, you would query all instances for a process definition and then all tasks for each instance.

Or, are you looking for only the tasks for a single instance. The code you posted will retrieve the tasks for a single instance, but you need to pass in the instance id retrieved by querying the runtimeService for active instances than match your required query parameters (e.g. business data, process variables or simply by process definition).

Hope this helps, let us know specifically what you are trying to achieve and I'm sure we can help.

Regards, Greg

Upvotes: 0

Related Questions