Default Execution Listener on Process Start with Camunda

Ali Mehdi Erçetin
2 min readMay 23, 2021

In my previous article, I mentioned about adding default task listener triggered on create event with a real world example. Task listeners are designed to react task events. There are six task events which are create, update, assignment, complete, timeout, and delete. Camunda offers also execution listener concept to react to events fired as execution progresses through the diagram like starting a process instance or end of an activity. There are two execuiton events which are start and end. In this article, I will mention about how you can add execution listener on process instance start event at runtime.

Firstly we will develop an execution listener that reads extension properties defined on start event and creates variables for these extension properties.

public class ExtensionPropertiesExecutionListener implements ExecutionListener {

@Override
public void notify(DelegateExecution delegateExecution) throws Exception {
CamundaProperties camundaProperties = delegateExecution.getBpmnModelElementInstance().getExtensionElements().getElementsQuery().filterByType(CamundaProperties.class).singleResult();
for (CamundaProperty camundaProperty : camundaProperties.getCamundaProperties()) {
delegateExecution.setVariable(camundaProperty.getCamundaName(), camundaProperty.getCamundaValue());
}
}
}

ExecuitonListener interface is provided by Camunda Platform to use during execution listener development. This interface has notify method to store main listener business. ExtensionPropertiesExecutionListener firstly gets extension properties by using utilities that Camunda platform provides. delegationExecution property allows us to realize runtime operations and setVariable method, one of the methods that delegationExecution provides, adds process variable with given name value pair.

We have execution listener to realize what we aim but it should be triggered on process start event. For this, we will implement AddExecutionListenerParseListener.

public class AddExecutionListenerParseListener extends AbstractBpmnParseListener {

@Autowired
private ExtensionPropertiesExecutionListener extensionPropertiesExecutionListener;

@Override
public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
processDefinition.addBuiltInListener(ListenerConstants.START_EVENT_TYPE, extensionPropertiesExecutionListener);
}
}

As we did for AddTaskListenerParseListener, we extend AbstractBpmnParseListener but this time we will use parseProcess method instead of parseUserTask to bound with process definition. Execution listener that we implemented before can be added to execution listener list on start event of the process with processDefinition.addBuiltInListener(ListenerConstants.START_EVENT_TYPE, extensionPropertiesExecutionListener). We need a last component to register AddExecutionListenerParseListener to Camunda platform. AddParseListenerPlugin is implemented for this purpose.

public class AddParseListenerPlugin extends AbstractProcessEnginePlugin {

@Autowired
private AddExecutionListenerParseListener addExecutionListenerParseListener;

@Override
public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration) {
List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
if(preParseListeners == null) {
preParseListeners = new ArrayList<BpmnParseListener>();
processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
}
preParseListeners.add(addExecutionListenerParseListener);
}
}

AddParseListenerPlugin helps us to add custom BPMN parse listeners and AddExecutionListenerParseListener is registered to Camunda platform. Now ExtensionPropertiesExecutionListener will be triggered on start event of all processes and the name value pair defined in extension properties of start event will be added as process variable.

--

--

Ali Mehdi Erçetin

Software engineer bringing nearly 8 years in software design, development and integration.