Parse Listener Service task events

Is it possible to add listener to service tasks in parse listener, similar to user tasks? Particularly i want o add some logic on creation of service task.

@Override
public void parseUserTask(Element userTaskElement, ScopeImpl scope, ActivityImpl activity) {
ActivityBehavior activityBehavior = activity.getActivityBehavior();
if (activityBehavior instanceof UserTaskActivityBehavior) {
UserTaskActivityBehavior userTaskActivityBehavior = (UserTaskActivityBehavior) activityBehavior;
userTaskActivityBehavior
.getTaskDefinition()
.addTaskListener(“create”, UserTaskExecutionListener.getInstance());
}
}

@bom Yes, it’s possible. In camunda, every bpmn notation(eg., UserTask, ServiceTask, SequenceFlow, EventBasedGateway, BoundaryEvent, ConditionalStartEventForEventSubprocess, etc) is called as activity.


Refer this Service Task BpmnParseListener example:


You can add listener BpmnParseListener to parse every activity in the bpmn process by registering it to process engine configuration like below:

public class ProgressLoggingSupportParseListenerPlugin extends AbstractProcessEnginePlugin {

  @Override
  public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
  
    // get all existing preParseListeners
    List<BpmnParseListener> preParseListeners = processEngineConfiguration.getPreParseListeners();
    
    if(preParseListeners == null) {
    
      // if no preParseListener exists, create new list
      preParseListeners = new ArrayList<BpmnParseListener>();
      processEngineConfiguration.setPreParseListeners(preParseListeners);
    }
    
    // add new BPMN Parse Listener
    preParseListeners.add(new ProgressLoggingSupportParseListener());
  }
}

Note: ProgressLoggingSupportParseListener should extends AbstractBpmnParseListener.

ProgressLoggingExecutionListener progressLoggingExecutionListener = new ProgressLoggingExecutionListener(value);

activity.addExecutionListener(ExecutionListener.EVENTNAME_END, progressLoggingExecutionListener);

Note: ProgressLoggingExecutionListener should implement ExecutionListener, not the TaskListener.


In case if you would like to add listener based on type of implementation provided for the service task then follow this:

To execute the bpmn parse listener to only the specific type of task, you can conditional statement the parse listener which you are writing. For example, if you want to execute the code only for service task which has Implementation=JavaDelegate, then:

image

image

public class ServiceTaskParseListener extends AbstractBpmnParseListener {

  @Override
  public void parseServiceTask(Element serviceTaskElement, ScopeImpl scope, ActivityImpl activity) {
    ActivityBehavior activityBehavior = activity.getActivityBehavior();
    if (activityBehavior instanceof ServiceTaskJavaDelegateActivityBehavior) {
      ServiceTaskJavaDelegateActivityBehavior serviceTaskActivityBehavior = (ServiceTaskJavaDelegateActivityBehavior) activityBehavior;
      scope.setProperty("name", "value");
      Element extensionElement = serviceTaskElement.element("extensionElements");
      if (Objects.nonNull(extensionElement)) {
        Element propertiesElement = extensionElement.element("properties");
        if (Objects.nonNull(propertiesElement)) {
          List<Element> propertyList = propertiesElement.elements("property");
          propertyList.forEach(System.out::println);
        }
      }
    }
  }

}