How to query the position of a process instance?

我怕爱的太早我们不能终老 提交于 2019-12-04 17:38:31

You can get the current position of your process instance using the following code, which will also give you the name of the activity(ies) when the process waits in multiple position.

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

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!