Is there an easy way to find out if the process is

2019-09-05 00:47发布

问题:

This question is about the Camunda BPM engine.

I would like to implement an ExecutionListener that I can attach to any process events. This listener should send process state messages to a message queue. The process message should contain a state that would be "PENDING" for the process if the process is waiting in a UserTask somewhere.

Now I wonder if there is an easy way to find out if the process is waiting (somewhere) in a UserTask inside the Delegation Code (by using provided DelegateExecution object of the delegation code method). Could not find one so far.

回答1:

For example:

import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.delegate.DelegateExecution;
import org.camunda.bpm.engine.delegate.ExecutionListener;
import org.camunda.bpm.engine.runtime.ActivityInstance;

public class ExampleExecutionListener implements ExecutionListener {

  public void notify(DelegateExecution execution) throws Exception {
    RuntimeService runtimeService = execution.getProcessEngineServices().getRuntimeService();
    ActivityInstance activityInstance = runtimeService.getActivityInstance(execution.getProcessInstanceId());

    boolean isInAnyUserTask = isInAnyUserTask(activityInstance);
  }

  protected boolean isInAnyUserTask(ActivityInstance activityInstance) {
    if ("userTask".equals(activityInstance.getActivityType())) {
      return true;
    }
    else {
      for (ActivityInstance child : activityInstance.getChildActivityInstances()) {
        boolean isChildInUserTask = isInAnyUserTask(child);
        if (isChildInUserTask) {
          return true;
        }
      }

      return false;
    }
  }
}

Note that this does not consider called process instances.



回答2:

DelegateExecution does not have all the information that you need. You will have to use the task query to see whether it returns at least 1 result on the process instance that is currently running.



回答3:

There is an interface TaskListener. You can implement it by yourself and append your own TaskListener to each UserTask in the BPMN code. You can also define on which event type your own TaskListenershould be executed (create, assignment, complete, delete).

The notify-method is called with a DelegateTask which contains more specific information about the concrete UserTask. You could extract the information and send these information into your queue (when you call your implementation of the TaskListeneron the create event).

Otherwise you can use the TaskService to create a query to retrieve all open tasks. For a working query you need the process instance id of the current execution, which you can retrieve from the delegate execution. To make things short take this code snippet: taskService.createTaskQuery().processInstanceId(delegateExecution.ge‌​tProcessInstanceId()‌​).list().isEmpty().



标签: java camunda