# Access process key from FlowableEventListener

**URL:** <https://forum.flowable.org/t/access-process-key-from-flowableeventlistener/6463>\
**Category:** Flowable Engine\
**Created:** [July 30, 2020, 4:14pm UTC](https://forum.flowable.org/t/access-process-key-from-flowableeventlistener/6463 "2020-07-30T16:14:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![franck102](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@franck102](https://forum.flowable.org/u/franck102)\
**Post date:** [July 30, 2020, 4:14pm UTC](https://forum.flowable.org/t/access-process-key-from-flowableeventlistener/6463/1 "2020-07-30T16:14:42Z")

</div>

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) 
        Context.getCommandContext().getCurrentEngineConfiguration();
    ProcessDefinitionCacheEntry entry = engineConfig.getDeploymentManager().
        getProcessDefinitionCache().get(processId);
    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.

---

<div class="post-metadata">

**Author:** ![joram](https://yyz1.discourse-cdn.com/flex035/user_avatar/forum.flowable.org/joram/32/26_2.png) [@joram](https://forum.flowable.org/u/joram)\
**Post date:** [July 30, 2020, 5:17pm UTC](https://forum.flowable.org/t/access-process-key-from-flowableeventlistener/6463/2 "2020-07-30T17:17:30Z")

</div>

> [@franck102](#):
>
> Is there a better way to achieve this? Obviously I cannot afford going back to the DB every time…

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.

---

<div class="post-metadata">

**Author:** ![franck102](https://avatars.discourse-cdn.com/v4/letter/f/35a633/32.png) [@franck102](https://forum.flowable.org/u/franck102)\
**Post date:** [July 30, 2020, 6:03pm UTC](https://forum.flowable.org/t/access-process-key-from-flowableeventlistener/6463/3 "2020-07-30T18:03:13Z")

</div>

Excellent, thanks!

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

Franck
