cancel
Showing results for 
Search instead for 
Did you mean: 

How to trace the path executed to reach an task

gualberto
Champ in-the-making
Champ in-the-making
Hi all

I need to execute some code on an TaskListener that depends on the path used by the process to reach the task.
In other words, i need to know the entire trace the current token has done until the moment.

If it's not possible, could i add an listerner in a sequence flow ?

any kind of help is welcome

best regards
13 REPLIES 13

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

You could use HistoryService to get path to the current node for the process instance.
Another possibility is to set process variable in process instance execution and base decision in the task listener on the value in variable.

Regards
Martin

Hi Martin

Thanks for the help.
I could not found any query available from the History Service that returns the path information.

Could you post some example code ?

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi.

example:

historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list();

Regards
Martin

Hi Martin

I'm really appreciating your help, but after reading the HistoricActivityInstance Javadocs I have not seen how it will help on this case, because it don't exposes any 'getExecutionPath' method (or something like this).

Could you please elaborate it ?

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

Working example could be found here:
https://github.com/Activiti/Activiti/blob/0a353c17ed011a6285be297f0a292a7957bca416/modules/activiti-...

e.g. method:
testHistoricTaskInstanceQuery.

query:

historyService.createHistoricTaskInstanceQuery().processInstanceId(processInstanceId).list();

Returns list of task from the history for the given processInstance.
In the case of parallel paths you have to take process definition into consideration to get execution path.

Regards
Martin

gualberto
Champ in-the-making
Champ in-the-making
Hi Martin

I checked the HistoricTaskInstance example but i have not found anything to help with the transitions, then i tried my luck looking up the source code on Git and found something that worked great to me, see:

<java>
import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.ExecutionListener;
import org.activiti.engine.impl.pvm.delegate.ExecutionListenerExecution;


public class MyRuntimeListener implements ExecutionListener {

@Override
public void notify(DelegateExecution de) throws Exception {
  System.out.println(((ExecutionListenerExecution)de).getEventSource().getId());
}

}
</java>

This way i add an ExecutionListener on my transitions and now i can take decisions based on transition's ID.

I have seen on docs that we must avoid classes from "impl" package. So how can i get this ID without the "forbidden" ExecutionListenerExecution ?

Thank you very much for the help.

martin_grofcik
Confirmed Champ
Confirmed Champ
Could you use:

org.activiti.engine.delegate.DelegateExecution#getCurrentActivityId
?
Regards
Maartin

Hi Martin

Unfortunately, DelegateExecution#getCurrentActivityId does not return the sequence flow id, but the source of it. So, if we have multiple outgoing flows it's impossible know exactly which one was taken.

The solution i found, after read the sources, was the following:
<java>
public class MyRuntimeListener implements ExecutionListener {

@Override
public void notify(DelegateExecution de) throws Exception {
  System.out.println(((ExecutionListenerExecution)de).getEventSource().getId());
}
}
</java>

Of course, i am using "downcasting", what it's wrong, but is there any other way ?

Thanks.

martin_grofcik
Confirmed Champ
Confirmed Champ
Hi,

You are right with the flow recognition. I think listeners are the easiest way.
Regards
Martin