AnsweredAssumed Answered

Any way to get reference to created task from task create listener?

Question asked by neville.sequeira on Aug 28, 2013
Latest reply on Sep 6, 2013 by neville.sequeira
Maybe its a silly question and the obvious answer might be that the task being queried for has not yet been committed to the database.
The question is, why do I get back
null
for the Task retrieved from the task query in the code below? I have hooked this as a listener for task create event on the concerned user task in the process definition.

Assuming my guessed answer is right, is this not a problem? I need to get hold of the Task that is created. That is the whole point of my listener. I know when the transaction, is committed, the Task is in the database and running the same task query after that does retrieve the task. However, I need to refer to the task before the transaction is committed. Contrasted to this, if Activiti persistence was based on JPA (Hibernate) instead of iBatis, the Task being queried could be found in the persistence context easily using entityManager.find().

Is there some way, from inside the listener, to get hold of the Task whose creation lead to the listener to be called? Long story short, we need this for our phased migration from jBPM 3 to Activiti.


import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.task.Task;


public class MyTaskCreateListener implements TaskListener {
   @Override
   public void notify(DelegateTask delegateTask) {
      TaskService activitiTaskService = delegateTask.getExecution().getEngineServices().getTaskService();
      Task theActivitiTask = activitiTaskService.createTaskQuery().taskId(delegateTask.getId()).singleResult();
      
      // Do something with theActivitiTask
   }
}

Outcomes