cancel
Showing results for 
Search instead for 
Did you mean: 

Distinguish deleted and finished processes in end listener

msvelev
Champ in-the-making
Champ in-the-making
Hello community,

I'm currently trying to implement a logic that understands if a process has been deleted from the runtime api or it has finished on its own.
I'll be using this in an ExecutionListener which is triggered when a process ends.
My current solution is to check:
"End".equals(context.getCurrentActivityName());

And it works just fine, but I want this to be a generic method that can be applied to all activiti processes and not only those whose end event is named "End".

I've noticed that when the process was deleted I get null from
context.getCurrentActivityName()

If that's always the case when a process is deleted then it would be a perfect solution to just check if it is null.
Is this the expected behavior and if not can you propose another solution?

Thanks & best regards,
Mincho
3 REPLIES 3

mandas
Champ in-the-making
Champ in-the-making
Hi,

I believe that the state is not yet flushed to the db at that point so you can check if the completed/cancelled execution has both its parent id and super execution id set to null. Unfortunately, this would require an extra query (by pk though) to get the super execution id as it's not mapped to DelegateExecution.

An alternative approach would be to use the event handlers (http://www.activiti.org/userguide/#eventDispatcher) and
the ACTIVITY_COMPLETED event type but still you 'll have to write filtering code inside the listener. Another alternative would be to implement a BpmnParseHandler to handle Process elements.

Hope that helps

Dimitris

msvelev
Champ in-the-making
Champ in-the-making
Hi Dimitris,

Thanks for the response!

I don't mind another query and the first option seems the best.
I'm not sure how can I get the completed/cancelled execution ID having access only to the DelegateExecution.

Is this the way to do it:
<java>
private boolean isAborted(DelegateExecution context) {
        return (context.getParentId() == null && getParentExecutionId(context) == null);
    }

    private String getParentExecutionId(DelegateExecution context) {
        String currentActivitiId = context.getCurrentActivityId();

        return context.getEngineServices().getRuntimeService().createExecutionQuery()
                .activityId(currentActivitiId).singleResult().getParentId();
    }
</java>

Thanks & best regards,
Mincho

EDIT:
<java>context.getEngineServices().getRuntimeService().createExecutionQuery()
                .activityId(currentActivitiId).singleResult()</java>
This seems to return null.
Could you tell how should it be done?

mandas
Champ in-the-making
Champ in-the-making
Well, it's been almost two weeks and I don't understand why I 've mentioned about the discrimination between top-level processes and subprocesses. It has nothing to do with your question, sorry for the inconvenience 😕

You can go through event handlers and ActivitiEventType.PROCESS_COMPLETED and ActivitiEventType.PROCESS_CANCELLED.

Regards,
Dimitris