Unique id in process flow

I am coding an event listener class based on FlowableEventListener to get notified about started and completed activities. But if i have the following code to find a unique id for the current task, i can not find any unique id in my logs.

    public void onEvent(FlowableEvent event) {
        if (event instanceof FlowableEngineEvent engineEvent) {
            FlowableEventType eventType = engineEvent.getType();
            
            if (eventType != FlowableEngineEventType.ENTITY_INITIALIZED && 
                eventType != FlowableEngineEventType.ENTITY_CREATED &&
                    eventType != FlowableEngineEventType.ENTITY_DELETED &&
                    eventType != FlowableEngineEventType.VARIABLE_DELETED &&
            eventType != FlowableEngineEventType.HISTORIC_ACTIVITY_INSTANCE_ENDED &&
            eventType != FlowableEngineEventType.HISTORIC_ACTIVITY_INSTANCE_CREATED &&
            eventType != FlowableEngineEventType.SEQUENCEFLOW_TAKEN){
                
                System.out.println("Event name: " + eventType.name());
                System.out.println("Scope ID: " + engineEvent.getScopeId());
                System.out.println("Scope Definition ID: " + engineEvent.getScopeDefinitionId());
                System.out.println("Sub Scope ID: " + engineEvent.getSubScopeId());
                System.out.println("ProcessInstanceId: " + engineEvent.getProcessInstanceId());
                System.out.println("ProcessDefinitionId: " + engineEvent.getProcessDefinitionId());
                System.out.println("ExecutionId: " + engineEvent.getExecutionId());
...

All the possible ids are always the same for a complete process run.

flowable-rest-1  | Event name: ACTIVITY_STARTED                                                                                               
flowable-rest-1  | Scope ID: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                             
flowable-rest-1  | Scope Definition ID: ff247244-571a-11f0-bb84-661101dc1f65
flowable-rest-1  | Sub Scope ID: fff0c308-571a-11f0-bb84-661101dc1f65                                                                         
flowable-rest-1  | ProcessInstanceId: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                    
flowable-rest-1  | ProcessDefinitionId: ff247244-571a-11f0-bb84-661101dc1f65                                                                  

flowable-rest-1  | Event name: ACTIVITY_COMPLETED                                                                                             
flowable-rest-1  | Scope ID: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                             
flowable-rest-1  | Scope Definition ID: ff247244-571a-11f0-bb84-661101dc1f65
flowable-rest-1  | Sub Scope ID: fff0c308-571a-11f0-bb84-661101dc1f65                                                                         
flowable-rest-1  | ProcessInstanceId: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                    
flowable-rest-1  | ProcessDefinitionId: ff247244-571a-11f0-bb84-661101dc1f65                                                                  
flowable-rest-1  | Activity ID for started activity: output_fo_ed227980_b234_4fb5_98a3_ed44ae821e77

flowable-rest-1  | Event name: ACTIVITY_STARTED                                                                                               
flowable-rest-1  | Scope ID: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                             
flowable-rest-1  | Scope Definition ID: ff247244-571a-11f0-bb84-661101dc1f65
flowable-rest-1  | Sub Scope ID: fff0c308-571a-11f0-bb84-661101dc1f65                                                                         
flowable-rest-1  | ProcessInstanceId: ffee03d6-571a-11f0-bb84-661101dc1f65                                                                    
flowable-rest-1  | ProcessDefinitionId: ff247244-571a-11f0-bb84-661101dc1f65                                                                  

I expected that the first and the last ACTIVITY_STARTED event should have any class method with this unique id. I can not believe, that there is not such a field in this class. What am is missing?

Hey @MeAndMyTeam,

Which unique ID are you talking about? What do you need this ID for?

Cheers,
Filip

Hello @filiphr ,
i am talking about a unique id or key, sometimes called correlation id. An id/key, which i can use to identify f.e. a service task over an ACTIVITY_STARTED to an ACTIVITY_COMPLETED event over all runned processes.
In our case we are sending the ACTIVITY_STARTED and ACTIVITY_COMPLETED events to our own system to log the history. And to combine the events to a single event, we need this id/key.

I think that the best solution for what you need is to implement org.flowable.engine.impl.history.HistoryManager and use the CompositeHistoryManager by including the default and yours. There we have recordActivityStart(ActivityInstance) and recordActivityEnd(ActivityInstance)`

Cheers,
Filip

Ok, i see a method
getId() The unique identifier of this historic activity instance.
in this ActivityInstance. I think this is the id, i can use for?

But i see right now that me requirement is a little bit more complex. I also have a ServiceTask with a JavaDelegate class. In this i got a new call of this ServcieTask over the execute(DelegateExecution execution) method.
The id in this execute method must be the same, as in the ACTIVITY_STARTED and ACTIVITY_COMPLETED events. So that i can merge all this data.
I think the “The unique identifier of this historic activity instance” is not available in the execute(DelegateExecution execution) method? I can not find it there.

Is this scenario even all possible with flowable?

In theory you can locate the ActivityInstance for the particular execution through even more low level non public API (the HistoryManager is not really part of the public API). You can look at the ActivityInstanceEntityManager

Cheers,
Filip

But if i use the ActivityInstanceEntityManager class i only find methods, which gave me multiple results, like findActivityInstancesByExecutionAndActivityId. My expected result must be only one. In this method i must deliver the two parameter String executionId and String activityId
But if a have a process, where i run throw one ServiceTask several time like
START → TASK1 (first invocation) → TASK2 → TASK1 (second invocation) → TASK2 → TASK3 → END
i will always get the same executionId (it is always the same running process) and the activityId is also the same of TASK1. So with this parameters i can not find f.E. the second TASK1 invocation.
So i believe, this ActivityInstanceEntityManager is not my correct solution?

Did you see findUnfinishedActivityInstance?

Ok, thanks, i thought that this was the wrong method. But it is working fine with it. So i got my unique history id in the JavaDelegate class.
But how can i now find this history id in my onEvent Listener method in my FlowableEventListener class?
If i interpret it right, the suggested methods recordActivityStart and recordActivityEnd are only to record a new activity. But i need a possibility to find the history id of the current event, i am in?
How can i achieve this goal?

Your assumption is wrong. Have a look at our default implementation. The ActivityInstance you find through the method I mentioned has the id. For use the runtime and historic activity instance would have the exact same id. The history is just a copy of the runtime.

Which default implementation do you mean?

The DefaultHistoryManager from Flowable Open Source