It looks like TASK_CREATED does not do what I want either, at least not completely (refer to original post)…
Here is what my code does:
I store candidate groups along with the Task in the same transaction and I listen for the TASK_CREATED event.
<code>
@Transactional
public Task saveTask(Task newTask) {
taskService.saveTasK(newTask);
taskService.addCandidateGroup(newTask.getId(), "someGroup");
return newTask;
}
//Spring @Transaction commit;
</code>
TASK_CREATED listener looks like this:
<code>
public void onEvent(ActivitiEvent event) {
ActivitiEntityEvent entityEvent = (ActivitiEntityEvent) event;
Task taskEntity = (Task) entityEvent.getEntity();
List<IdentityLink> identityLinkList = taskService
.getIdentityLinksForTask(taskId);
// Get IdentityLink's that are type CANDIDATE and
// Broadcast using WebSockets to the browsers to tell each "candidate's" Queue there is a new task available for them to claim
}
</code>
However, the identityLinkList is empty inside the ActivitiEventListener, so I do not have the information I need to broadcast to the candidate browsers at this time.
It appears that the ActivitiEntityEvent TASK_CREATED is received before the transaction commit and if I look for candidates in that event listener, I see none. Activiti must be using READ_COMMITTED on the call to TaskService.getIdentityLinksForTask(taskId), hence, it does not see that I wanted a candidate group for it. Yes, still, I would've hoped a query inside the same transaction would read back the data. This is using the H2 DataSource, BTW. Would behavior be different on PostgreSQL?
It would be great if the TASK_CREATED was not raised until after Txn Commit. Then, ActivitiEventListeners that receive the event can ask questions about it and get the answers consistent with the Txn & DB regardless of Driver & Database.
If there is another way to accomplish my goals that I am not seeing, please let me know.