Access process key from FlowableEventListener

Hi all,

I need to obtain the key of the current process definition in an event listener (for metering purposes). I came up with the solution below, which has a couple issues:

  • I don’t know what guarantee I have that Context.getCommandContext() will work in my listener (it does in my basic tests…)
  • I have to downcast the engine config to ProcessEngineConfigurationImpl

Is there a better way to achieve this? Obviously I cannot afford going back to the DB every time…

public class TimingListener implements FlowableEventListener {   
public void onEvent(FlowableActivityEvent event)
    String activityId = event.getActivityId();
    String processId = event.getProcessDefinitionId();
    ProcessEngineConfigurationImpl engineConfig = (ProcessEngineConfigurationImpl) 
    ProcessDefinitionCacheEntry entry = engineConfig.getDeploymentManager().
    processKey = entry.getProcessDefinition().getKey()

The listener is added as a typed listener for FlowableEngineEventType.ACTIVITY_STARTED, FlowableEngineEventType.ACTIVITY_COMPLETED and FlowableEngineEventType.ACTIVITY_CANCELLED event types.

You can use ProcessDefinitionUtil#getProcessDefinition(), which works against the cache and does a query if needed.

Also: the command context will always be there when the FlowableEventListener is invoked, as it’s part of the current command invocation.

Excellent, thanks!

I wasn’t aware of those Util classes - esp. CommandContextUtil, they will make our life much easier.