I've got two issues that arise after calling processInstance.end() that I'm not sure how to handle.
1) Available tasks are still returned by taskMgmtSession.findPooledTaskInstances(poolName) for the ended process .
2) If the open tasks are then subsequently cancelled, the process graph continues execution, despite the fact that the process has been ended.
When a process is cancelled (prematurely ended), I need the ability to cancel out any open tasks without furthuring the process instance (graph execution should stop).
This is the code I'm using to end the process instance:
jbpmSession.beginTransaction();
//Record a standardized variable that we can use to determine that this process has
// been 'cancelled' and not just ended.
pi.getContextInstance().createVariable("cancelled", request.getParameter("reason") == null ? "" : request.getParameter("reason"));
pi.getContextInstance().createVariable("cancelled_by", request.getParameter("actor") != null ? request.getParameter("actor") : user);
//End the process instance and any active tokens
pi.end();
//Unfortunately, we may still have open tasks, even though their parent tokens
// have been ended. So we'll simply get all tasks from all tokens and cancel
// them if they are still active.
String hql = "from "+Token.class.getName()+" as t where t.processInstance = :instance" ;
Query hqlQuery = jbpmSession.getSession().createQuery( hql );
hqlQuery.setEntity("instance", pi);
List tokensForInstance = hqlQuery.list();
if(tokensForInstance != null && tokensForInstance.size() > 0) {
for (Iterator itr = tokensForInstance.iterator(); itr.hasNext();) {
Token t = (Token)itr.next();
List tasks = taskMgmtSession.findTaskInstancesByToken(t.getId());
Iterator taskItr = tasks.iterator();
while(taskItr.hasNext()) {
TaskInstance ti = (TaskInstance)taskItr.next();
if(!ti.hasEnded()) ti.cancel();
}
}
}
graphSession.saveProcessInstance(pi);
jbpmSession.commitTransaction();
I looked for a jira on this--there is one with a title about a task instance fix, but it has no description.
Is there another strategy I should be using?
Thanks,
Britt