cancel
Showing results for 
Search instead for 
Did you mean: 

How to get process instance variables on event ACTIVITY_START

cdeneux
Champ in-the-making
Champ in-the-making
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
4 REPLIES 4

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi Christophe,

make the first service task asynchronous.

Regards
Martin

cdeneux
Champ in-the-making
Champ in-the-making
Hi Martin,

Thanks for your reply. Adding <code>activiti:async="true"</code> on the service task works fine but I think it is not usable in my particular use case.
I'm embedding Activiti engine into a component to be deployed on Petals ESB infrastructure to provide a process/service convergence to our users. Our users will develop their process definition or service orchestrations and deploy them into our Activiti component. So, it is not conceivable for us to force our users to set <code>activiti:async="true"</code> in few cases to work around a technical issue. In a conceptual point of view, all services invoked by a process instance should be processed with the same manner.
So, except if another solution exists, I will investigate a contribution to include the process instance variables to the event.

Regards,
Christophe

jbarrez
Star Contributor
Star Contributor
One way do solve this is to have a BpmnParseHandler that automatically sets async to true for your service tasks.

cdeneux
Champ in-the-making
Champ in-the-making
Thanks Joram, that works fine.

I use your example CustomUserTaskBpmnParseHandler available into the Activiti user documentation as a post BpmnParseHandler.

Regards,
Christophe