AnsweredAssumed Answered

Process end time not available in process end listener

Question asked by naag on Jun 18, 2012
Latest reply on Jun 19, 2012 by naag
Hi all,

In my Activiti 5.9 project (with CDI, history = full), I've setup a global execution listener for the process end event via a custom BPMN parse listener. Inside the event handler, I try to load the current process instance via the HistoryService, but I see that historicProcessInstance.getEndTime() is always null. That's although Activiti ProcessInstanceEndHandler calls historicProcessInstance.markEnded() before.

I believe the reason is that ProcessInstanceEndHandler does not flush it's changes to the DB, and HistoryService only works against the DB. Is that correct? Is there any official API to get the transient entities instead of what's stored in the database? As a workaround, I use the HistoricProcessInstanceManager retrieved via the CommandContext (I was inspired by ProcessInstanceEndHandler here), but I'm not sure if it's reliable / stable because of bypassing the API.

The same problem happens with the last executed user task (it also has no end time available when retrieved via HistoryService). I suppose this just occurs if it's the last task before the end event?

My CustomBpmnParseListener:

public class CustomBpmnParseListener implements BpmnParseListener {

   protected static ExecutionListener PROCESS_END_ARCHIVE_LISTENER = new ProcessEndArchiveListener();

   public void parseProcess(Element processElement, ProcessDefinitionEntity processDefinition) {
      processDefinition.addExecutionListener(ExecutionListener.EVENTNAME_END, PROCESS_END_ARCHIVE_LISTENER);

   // …

Part of my original event handler ProcessEndArchiveListener:

HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery()

List<HistoricTaskInstance> taskInstances = historyService.createHistoricTaskInstanceQuery()

Part of my event handler ProcessEndArchiveListener with the workaround in place:

// Inspired by
// org.activiti.engine.impl.history.handler.ProcessInstanceEndHandler
HistoricProcessInstance processInstance = Context

HistoricTaskInstanceQueryImpl historicTaskInstanceQuery = new HistoricTaskInstanceQueryImpl();
List<HistoricTaskInstance> taskInstances = Context.getCommandContext().getHistoricTaskInstanceManager()
      .findHistoricTaskInstancesByQueryCriteria(historicTaskInstanceQuery, null);

Any advice on how to make this more solid would be much appreciated :-)