Execution Listener

Hi! I’m writing a custom execution listener to manage executions for each event type (start, end, event)

It works fine triggering each event types but I noticed that each Event have been processed more than one time differing for activityInstanceState

What about activityInstanceState value? How can I process one time each execution event type?

Hi @devj87,

could you tell us more about where you added the listener or upload the xml of your process?

regards,
Dominik

Thanks for reply @dominikh I’m using a shared process engine so I added my listener defining a plugin implementing ProcessApplicationEventListenerPlugin

The event parse listener class override addEndEventListener and addStartEventListener, for example:

@Override
protected void addEndEventListener(ScopeImpl activity) {
	activity.addListener(ExecutionListener.EVENTNAME_END, EXECUTION_LISTENER);
}

It works fine my doubt is the double catch which requires to process it twice

Hi @devj87,

sorry - don’t know why it has this behavior. Maybe someone from Camunda can help? @thorben

Why don’t you use the “default” ProcessApplicationEventListenerPlugin like described on that page

Just to be sure, this issue happens for every single element in your processes?

regards,
Dominik

1 Like

I agree with Dominik: There is no need to subclass ProcessApplicationEventListenerPlugin. Just let your process application return a custom execution listener via ProcessApplicationInterface#getExecutionListener.

It is correct that you will receive two events for every activity instance: one start and one end event. If you are only interested in one of them, then why not simply ignore one of the events like this:

public class MyListener implements ExecutionListener
{
  public void notify(DelegateExecution execution)
  {
    if (ExecutionListener.EVENTNAME_END.equals(execution.getEvent())
    {
      return;
    }

    // do stuff
  }
}
1 Like

Hi @thorben I’m using a plugin to use it for all executions to catch Event for all deployed process

I’ve customized it to provide my implementation providing a custom EXECUTION_LISTENER

However catch event (Start | Take | End) is not a problem, my problem is Start triggered twice as different activityInstanceState (maybe) or other but I need to catch it once to avoid duplicated actions

The process engine has at least once execution semantics. That means, a start listener for an activity instance is guaranteed to be invoked at least once. During regular, exception-free operation, the behavior is exactly once (i.e. one invocation per activity instance). However, if the transaction in which the process instance is executed rolls back, the process engine may invoke the listener a second (or third, etc.) time. To be on the safe side, your listener implementation should either a) participate in the engine’s transaction (such that its effects are rolled back when the engine transaction rolls back) or b) be idempotent.

In addition you could implement a ProcessApplication that extends the ServletProcessApplication and returns the ExecutonListener you want like mentioned before. You could extend it in every ProcessApp you create instead of extending the ServletProcessApplication. With this approach you would not have to use your own ProcessApplicationEventListenerPlugin.

regards,
Dominik