Process End EventListener

Greetings fellow human beings,

I am trying to execute code via a spring EventListener on a process end event using the the following:

		@EventListener(condition = "#execution.eventName.equals('end')")
		public void onEnd(DelegateExecution execution)
		{
          String procInstId = execution.getProcessInstanceId();
          // do stuff based on processInstanceId of just finished process
        }

It seems to execute after every Activity, thus I am probably listening to the wrong event, one that fires at the end of every activity.

Is there a way to listen explicitly to process end events?

Thanks in advance
Jannis

Does anybody have some insights they might share?

Hi @jannisEis

Usually if you want to trigger something on an event or a task you can use execution listeners. Delegation Code | docs.camunda.org

Have you thought about using that option?

Hi @Niall , thank you for your reply!

Unfortunately that option is not particularly desirable, as we have a multitude of processes and every process should perform the same actions on the EndEvent. So it would be quite bothersome and error prone to manually add the Listener to each an every EndEvent, particularly for future processes.

Yup, thats a very good point, listeners in that case would cause more hassle.

I’m not sure off the top of my head about how to get this to be triggered just by end events, but i’ll go have a look and try to get back to you.

I have a working solution by creating the endEvent Listener on parsing of the Bpmn.

EDIT: using CoreModelElement#addListener instead of the deprecated ScopeImpl#addExecutionListener

@Component
public class EndEventListenerEnginePlugin implements ProcessEnginePlugin
{
	@Override
	public void preInit(ProcessEngineConfigurationImpl processEngineConfiguration)
	{
		List<BpmnParseListener> preParseListeners = processEngineConfiguration.getCustomPreBPMNParseListeners();
		if (preParseListeners == null)
		{
			preParseListeners = new ArrayList<>();
			processEngineConfiguration.setCustomPreBPMNParseListeners(preParseListeners);
		}
		preParseListeners.add(new AbstractBpmnParseListener()
		{
			@Override
			public void parseEndEvent(Element endEventElement, ScopeImpl scope, ActivityImpl activity)
			{
				ExecutionListener listener = new ExecutionListener()
				{
					@Override
					public void notify(DelegateExecution execution) throws Exception
					{
						doStuff();
					}
				};
				activity.addListener(ExecutionListener.EVENTNAME_END, listener);
			}
		});
	}

	@Override
	public void postInit(ProcessEngineConfigurationImpl processEngineConfiguration)
	{
		//
	}

	@Override
	public void postProcessEngineBuild(ProcessEngine processEngine)
	{}
}