cancel
Showing results for 
Search instead for 
Did you mean: 

processInstance.isEnded never gives correct status

gs76pl
Champ in-the-making
Champ in-the-making
hi,

this seems like a basic question but it looks like any call to processInstance.isEnded never returns TRUE even when the actual process has been completed. I suspect that in in order to get actual process status one should execute a process query like below

ProcessInstanceQuery pq = runtimeService.createProcessInstanceQuery().processInstanceId(pid);
ProcessInstance pi = pq.singleResult();

but even with this query when the process has been completed pi variable is null. Bearing that all in mind what is the purpose of having isEnded method when one never gets correct results while using it?
9 REPLIES 9

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
when the processinstance is active you ALWAYS get the CORRECT result 🙂 But for the real use I have no idea…

gs76pl
Champ in-the-making
Champ in-the-making
perhaps you're getting correct results for a process status while calling other processInstance methods e.g. isActive etc. but as i said when one uses isEnded the result is always wrong (completed process doesn't change its processInstance status isEnded to true). The code/flow i've been testing for this is as below:


<definitions id="definitions"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn"
targetNamespace="Examples">

<process id="helloProcess">

  <startEvent id="start" />
  <sequenceFlow id="flow1" sourceRef="start" targetRef="collect1" />

  <userTask id="collect1"> 
  </userTask>

  <sequenceFlow id="flow2" sourceRef="collect1" targetRef="end" />

  <endEvent id="end" />

</process>

</definitions>

and the java code:


ProcessInstance pi = runtimeService.startProcessInstanceByKey("helloProcess");

String taskId = taskService.createTaskQuery().singleResult().getId()
taskService.complete(taskId);

pi.isEnded()

for the code as above pi.isEnded() call returns FALSE even though the whole process has been completed as there was only one userTask

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
I never mentioned isActive() and I was only making a joke… sorry that that was not clear…

What you mis here is that the pi you use is a 'disconnected' representation of the instance state. The only updates take place in the database. So you have to refresh the pi and then pi will be null since it is not active anymore. It will be in the history tables though where you can see the state.

This is one of the less obvious things of activiti, but once you know this, it works.

gs76pl
Champ in-the-making
Champ in-the-making
thanks for the clarification.
i've suspected something like that is going on (the same way drools flow works) but unless i miss something more isEnded method doesn't make sense in my opinion.
As you said once the process has been completed its history is available through history service ie.

HistoricProcessInstance hpi = historyService.createHistoricProcessInstanceQuery().processInstanceId(pid).finished().singleResult()
above works fine with my code and i can see that my process has been ended.

The problem i have with the ProcessInstance is that if it's disconnected and one need to refresh it (i assume below code is the only way to refresh a process instance)

ProcessInstanceQuery pi = runtimeService.createProcessInstanceQuery().processInstanceId(pid).singleResult();
once the process has been ended runtimeService will never find it and as a result pi will always be null. As a consequence isEnded method doesn't make sense because:
1. if the process is active than above query will return NOT NULL (no need to call isEnded)
2. if the process has ended than above query will return NULL (no need and more importantly no way to call isEnded)

ronald_van_kuij
Champ on-the-rise
Champ on-the-rise
Correct, that is why I said
But for the real use I have no idea…

jbarrez
Star Contributor
Star Contributor
One reason I use it for is for unit tests with processes that have all automatic steps … but it's obviously not a 'real' use case

louis44
Champ in-the-making
Champ in-the-making
I'd like to follow up on gs76pl's question as it doesn't make sense to me either. Why does isEnded() exist on ProcessInstance if its behavior is so counter intuitive.

jbarrez:if your process has all automatic steps, how do you use isEnded()? When all of the steps have executed, does isEnded() return true or false?

louis44
Champ in-the-making
Champ in-the-making
Sorry, I just searched more and found this: http://forums.activiti.org/content/processinstanceisended-returns-false. In this thread, jbarrez indicates that if the process has all automatic steps, isEnded() will return true.

jbarrez
Star Contributor
Star Contributor
Correct. Always remember Activiti API's return a 'snapshot' of the data, not necessarily the 'current' state.