Why can't I see the end event during a process instance end event listener?

I have a model which defines a process end event listener. This listener is supposed to be executed when the instance is about to be finished.
When the event listeners retrieves the list of all activities of the processInstance, i get the list of all activities BUT the endEvent.
Do you know how to get the endEvent in this list of activities?

NOTE: What I would like to obtain is the whole list of activities executed during the processInstance. I understand the endEvent must be in this list. In my opinion, the endEvent should be in a finished/completed state when an executionListener attached to the end of the process executes. At least it should be present, even in a ‘running’ state

Hi @mabertran,

How do you retrieve the list of all activities? Do you perform any kind of query?

Cheers,
Roman

The query I use to retrieve the list of all activities:

historyService.createHistoricActivityInstanceQuery().processInstanceId(execution.processInstanceId).list()

Am I doing right?
In the result of this query, the endEvent is missing (camunda 7.5.0)

Best,
Manu

Hi @mabertran,

Thanks for sharing the additional information.

That the endEvent is not in the result of the query, is the expected behavior. When the process engine continues the flow of the process instance from the last transaction boundary until the end of the process instance, then these changes are not flushed to the database, when the process end execution listener is invoked.

You could try to use transaction listener:

public void notify(DelegateExecution execution) {
    
    Context.getCommandContext().getTransactionContext()
      .addTransactionListener(TransactionState.COMMITTED, new TransactionListener() {

      @Override
      public void execute(CommandContext commandContext) {
        // perform your query to get all executed activities
        
      }
    });
  }

This means, when the changes are committed to the database, then the transaction listener is invoked. In that case the query will also return the endEvent inside the results.

Disclaimer: This is internal API, meaning we do not guarantee any stability with upcoming releases.

Cheers,
Roman

2 Likes

Thanks a lot. The suggested solution worked perfectly.
The only thing is I changed TransactionState.COMMITTED to TransactionState.COMMITTING because I want RuntimeExceptions to cause a rollback to the current transaction. I tested it and I’m getting the endEvent as well, so it’s solved for me.

By the way: are there plans to make those transactionListeners available on the public API? I think we would use this quite a lot, since it’s not the first time we experience (what we find) unexpected behaviour during execution of scripts or listeners.

Actually (and this is a personal opinion), isn’t it a bit strange for listeners to get query results from outside the transaction it is executing in? I suppose there is a full issue behind this question, but if I think of end listeners to be executed during the transaction that is actually ending a processInstence, I would expect to get results reflecting the state of processEngine in such not commited transaction.

I suppose the reason for all this is that the listener does not execute as part of the transaction…?