cancel
Showing results for 
Search instead for 
Did you mean: 

TASK_CREATED event fired after its userTask execution

cnirparis
Champ in-the-making
Champ in-the-making
Hello.

I've defined in my
activiti.cfg.xml
a listener class which implements
ActivitiEventListener
. This class tests the event type TASK_CREATED for doing some work before the task. But I have two problems :

1) I have to check the task name which has fired the event. I use this code :

Task t =  ts.createTaskQuery().processInstanceId(event.getProcessInstanceId()).orderByExecutionId().desc().singleResult();
if (t != null)
{
taskId = t.getId();
taskName = t.getName();
}

but sometimes, specially with the first task of the process, I get 'null' values, so I cannot know which task fired the event.

2) the event TASK_CREATED is fired AFTER the same name task is executed , as it appears in the logging.
How can I ensure that the event is fired before the task begins to work ?

Thank you.
2 REPLIES 2

cnirparis
Champ in-the-making
Champ in-the-making
Problem solved. For getting the task name wich is created, you must access the in-memory objects, and not the Database ones, which aren't still commited. So you have to code that way :
<code>
public class ActionListener implements ActivitiEventListener
{


@Override
public void onEvent(ActivitiEvent event)
{

  if (event.getType().equals(ActivitiEventType.TASK_CREATED))
  {

    ActivitiEntityEvent ve = (ActivitiEntityEvent) event;
    TaskEntity t = (TaskEntity) ve.getEntity();
    String taskName = t.getName();
    String taskId = t.getId();
    String piid = t.getProcessInstanceId();
    String processsName = t.getProcessDefinitionId();

    System.out.println("TASK_CREATED - : Process " + processName +  " - Pid #" + piid
      + " : TaskId : # " + taskId
      + " - TaskName : " + taskName
      );
  }
}
</code>
This works only with UserTasks, not serviceTasks. By the way the TaskEntity class is not documented…

jbarrez
Star Contributor
Star Contributor
That's current. Committing happens at the end of the transaction.

TaskEntity is an internal class. In v6 we've interfaced all entities and we'll document those.