Advice on setting a process variable in a Delegate when an error occurs

900 views
Skip to first unread message

Gareth

unread,
Sep 8, 2015, 3:41:47 AM9/8/15
to camunda BPM users
Hi All,

I may be going about this the wrong way, I have a delegate which can execute arbitrary snippets of code.  This means it can inherently contain errors, and I want the process to end if any errors occur.  I can achieve this by throwing an exception from the delegate ( See below ). However any variables I set in the delegate do not take effect if I thrown an exception.  Are they rolled back by the transaction handling ?  I want to set a variable to say that the process ended with an error.  I know this is inferred by the process recording an incident, but I want to store this as a process variable.  Is there a way to achieve this ?

public void execute( DelegateExecution execution ) throws Exception {

try {
   
// Do Stuff
} catch( EvalError e ) {
   
// Log Errors
    execution
.setVariable( "processFinalState", "Runtime Error" );
   
throw new ServiceRuntimeException( "Invocation failure", e );
}
}


Is it possible with an executionListener, if so what CamundaEvent do I use ? Or do I need to implement boundary events ? Or something else entirely ?


Thanks in advance,


Gareth

thorben....@camunda.com

unread,
Sep 8, 2015, 3:53:45 AM9/8/15
to camunda BPM users
Hi Gareth,

An uncaught Java exception triggers transaction rollback, so any variable update won't be visible.

Perhaps the concept of BPMN error events is what you want [1]. That way you can properly end a process instance in case of such an exception. Note that there is no need to catch a BPMN error with a boundary event. The specification defines that execution simply ends at that point of execution in case the error is not caught (this however does not end all executions in a process instance).

Cheers,
Thorben

[1] http://docs.camunda.org/7.3/api-references/bpmn20/#events-error-events

Gareth

unread,
Sep 8, 2015, 4:55:44 AM9/8/15
to camunda BPM users
Thanks for your reply, I had skipped the error events section as the manual states "So, this is different than Java exceptions...."

A couple of follow on question i couldn't spot in the manual immediately:
1) Would I have to manually create an Incident to record the error details ? ( As I can only pass an error code to the error event so would loose the error details )
2) How do I end the process from within the exception implementation ( and preserve the variables / not cause a rollback ) ?

Thanks in advance,

Gareth

thorben....@camunda.com

unread,
Sep 8, 2015, 6:01:12 AM9/8/15
to camunda BPM users
Hi Gareth,

I think we should clarify first what you want to achieve:

a) a technical exception undhandled in the BPMN diagram; rollback of the current transaction; creation of an incident
b) an exception handled in the BPMN diagram; no rollback of the current transaction; no incident (since incidents are only created for unhandled technical exceptions)

For b), BPMN errors are the way to go. Then you can simply set a variable you need. If you want to go with a), there is no out of the box way to set a variable since the current transaction is rolled back as you have noticed. In that case, transaction listeners may be a solution to set the variable in a second transaction after the first has been rolled back. However, depending on your process model, the execution may not exist in this second transaction since its creation may have been rolled back as well. See [1] for a transaction listener example.

Cheers,
Thorben

[1] https://groups.google.com/d/msg/camunda-bpm-users/--fRxgG6JI8/Bb7BFAE46MgJ

Gareth

unread,
Sep 8, 2015, 6:11:16 AM9/8/15
to camunda BPM users
Hi Thorben,

It's a technical exception, that is not expected by the user or the BPMN diagram.  In this state I wanted to record, in a process variable, that an error had occurred.

( Ill challenge the requirement again, as the state can be inferred by the existence of an incident ) - Thanks for your help.

Kind Regards

Gareth


Reply all
Reply to author
Forward
0 new messages