cancel
Showing results for 
Search instead for 
Did you mean: 

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

neville_sequeir
Confirmed Champ
Confirmed Champ
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
   }
}

10 REPLIES 10

neville_sequeir
Confirmed Champ
Confirmed Champ
I happened to stumble on http://www.jorambarrez.be/blog/2012/10/25/call-a-service-in-a-service-tas/
I am using Activiti version 5.13, which should include the fix mentioned at that link.
Based on what is mentioned at the said link, I would expect my code to work. Or could it be that my code taskService.createTaskQuery() is somehow still being executed against a newly created transaction different from the one that is already running?

I guess its time to add some more break points 😞

neville_sequeir
Confirmed Champ
Confirmed Champ
Verified that no extra/unnecessary transactions are being created.
Kind of stuck on this.
Could someone help please?

frederikherema1
Star Contributor
Star Contributor
Why do you need the Task object? you have access to the DelegateTask, which holds all info related to the current task…

neville_sequeir
Confirmed Champ
Confirmed Champ
Yeah, I know about DelegateTask.
However we are migrating from jBPM3, and long story short, being able to get a reference to the Activiti Task object from within the task listener will allow us to continue having certain functionality that we currently have.

What exactly is the reason the task service returns <code>null</code> in my scenario? Is this is a bug? Should I report in Activiti JIRA?

trademak
Star Contributor
Star Contributor
Hi,

What Frederik is saying is that the DelegateTask object is the Task object, so why can't you use the DelegateTask object?
It's true that you can't query for the task yet, because it was not flushed to the database yet. But the DelegateTask object is the object that will be persisted to the database and it already contains the id, so what would you need more?

Best regards,

Got it. Thanks for the clarification.
The queries executed using services like TaskService go directly to the database and the Task object, whose creation caused my listener to be called, has not been flushed to the db yet. I am kind of too used to the persistence context cache from JPA.

Performance being a crucial aspect for us, I was thinking if I get a reference to the Task object, I can avoid having to create another of our own class to copy the id and other properties from DelegateTask object.

Would I be hoping against hope if I asked if there was way to cause the flush to happen before my task listener is called by Activiti ?
As an alternative, will it be reasonable for me to hold a reference to the DelegateTask instead ?

jbarrez
Star Contributor
Star Contributor
You can flush, but you will need to cast to get hold of the DbSQLSession from the ProcessEngineConfigurationImpl. However, I wold strongly advise against it as it might have unforseen consequences.

I don't follow the reasoning why you want to keep a reference and why it has impact on performance. What's the use case you are trying to fulfil?

neville_sequeir
Confirmed Champ
Confirmed Champ
Hi jbarrez,

Please see  http://forums.activiti.org/content/spring-jpatransactionmanager-not-saving-activiti-entities-db
I have attached a small project to that thread on Tijs's request.

krolikzaia
Champ on-the-rise
Champ on-the-rise
How to catch event of record in the table, please tell me.
Is it possible when creating a task,?
I want to send a message to the user that a task has been assigned to him?
activiti 6

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();

sendMessageToUser(delegateTask.getId());
}
}
receive client:
activitiTaskService.createTaskQuery().taskId(new_taskId).singleResult() it null

?