AnsweredAssumed Answered

How to get process instance variables on event ACTIVITY_START

Question asked by cdeneux on May 20, 2015
Latest reply on Jun 8, 2015 by cdeneux
Hi all,

I use an event listener on event ACTIVITY_START to detect when a service task starts. In my event listener implementation, I query Activiti engine to get variables of the associated process instance. This works fine only if, in my process definition, a user task exists between the start event and the service task. Otherwise (the service task is just after the start event) my query returns null:

    @Override
    public void onEvent(final ActivitiEvent event) {
        final ActivitiActivityEvent eventImpl = (ActivitiActivityEvent) event;
        if ("serviceTask".equals(eventImpl.getActivityType())) {

            final ProcessInstanceQuery processInstanceQuery = event.getEngineServices().getRuntimeService()
                    .createProcessInstanceQuery().processInstanceId(event.getProcessInstanceId())
                    .includeProcessVariables();
            final ProcessInstance processInstance = processInstanceQuery.singleResult();

            if (processInstance == null) {
                this.log.warning("Unable to retrieve the process instance for which a service task starts.");
            } else {
                final Map<String, Object> processVariables = processInstance.getProcessVariables();

                …
            }
        }
   }


I think that the query returns null because the process instance creation is flushed into database when the Activiti engine ends its processing. When the event ACTIVITY_START for my service task is fired, nothing has been flushed in database, except if a user task was previously executed.

Is there another solution to get my process instance variables from the event ACTIVITY_START ?

In my mind, process instance variables could be included into the event fired. So this solve the problem and improve performances removing the querying.

What do you think about this ?

Regards,
Christophe DENEUX

Outcomes