Execution Listener on Sequence Flow - Generate History Event

Looking for some ideas on best practices for creating a ActivityInstance History Event for when a Sequence Flow execution listener is executed.

This is my current working sample using Javascript Execution Listener, but could easily be transferred to Groovy as well:

var Date = Java.type("java.util.Date")

// https://docs.camunda.org/javadoc/camunda-bpm-platform/7.9/org/camunda/bpm/engine/impl/history/event/HistoricActivityInstanceEventEntity.html
var HistoricActivityInstanceEventEntity = Java.type('org.camunda.bpm.engine.impl.history.event.HistoricActivityInstanceEventEntity')

var historicActivityInstance = new HistoricActivityInstanceEventEntity()

historicActivityInstance.setActivityId(execution.getCurrentTransitionId())
historicActivityInstance.setExecutionId(execution.getId())
historicActivityInstance.setActivityType('sequenceFlow')
historicActivityInstance.setStartTime(new Date())
historicActivityInstance.setEndTime(new Date())
historicActivityInstance.setDurationInMillis(0)
historicActivityInstance.setProcessDefinitionId(execution.getProcessDefinitionId())
historicActivityInstance.setProcessInstanceId(execution.getProcessInstanceId())

// https://docs.camunda.org/javadoc/camunda-bpm-platform/7.9/org/camunda/bpm/engine/impl/history/handler/DbHistoryEventHandler.html
var historyHandler = Java.type('org.camunda.bpm.engine.impl.history.handler.DbHistoryEventHandler');
(new historyHandler).handleEvent(historicActivityInstance)

The DbHistoryEventHandler usage feels a little hacky, but could not find another class that was as easy is use.

@thorben, any suggestions?

Thanks!

Hi Stephen,

I think this will be hacky anyway as it uses internal API heavily. That said, one improvement could be using the engine’s configured history handler instead of always instantiating a new one. You can find it via execution#getProcessEngineConfiguration (confer class ProcessEngineConfigurationImpl). Another idea is to ask the engine’s history level if this event should be produced before giving it to the handler.

Cheers,
Thorben

Thanks @thorben. Thats perfect. And yes it was by-design to be hacky ;). Its purpose is solely to add data for BPMN Coverage testing.

Thanks @thorben

Script has been updated to:

var Date = Java.type("java.util.Date")

// https://docs.camunda.org/javadoc/camunda-bpm-platform/7.9/org/camunda/bpm/engine/impl/history/event/HistoricActivityInstanceEventEntity.html
var HistoricActivityInstanceEventEntity = Java.type('org.camunda.bpm.engine.impl.history.event.HistoricActivityInstanceEventEntity')

var historicActivityInstance = new HistoricActivityInstanceEventEntity()

historicActivityInstance.setActivityId(execution.getCurrentTransitionId())
historicActivityInstance.setExecutionId(execution.getId())
historicActivityInstance.setActivityType('sequenceFlow')
historicActivityInstance.setStartTime(new Date())
historicActivityInstance.setEndTime(new Date())
historicActivityInstance.setDurationInMillis(0)
historicActivityInstance.setProcessDefinitionId(execution.getProcessDefinitionId())
historicActivityInstance.setProcessInstanceId(execution.getProcessInstanceId())

var historyHandler = execution.getProcessEngineServices().getProcessEngineConfiguration().getHistoryEventHandler()
historyHandler.handleEvent(historicActivityInstance)