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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-28-2013 06:12 PM
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 }}
- Labels:
-
Archive

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2013 10:44 AM
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 😞

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-03-2013 02:22 PM
Kind of stuck on this.
Could someone help please?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2013 05:44 AM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2013 01:00 PM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2013 01:46 PM
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,

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-06-2013 02:37 PM
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 ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2013 03:42 AM
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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-13-2013 11:28 AM
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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2018 11:47 AM
I want to send a message to the user that a task has been assigned to him?
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?
