Red Hat Training
A Red Hat training course is available for JBoss Enterprise SOA Platform
7.26. Exception Handling Examples
Time-out:
If you are using the EsbActionHandler
action and the node is awaiting a callback, you can limit the waiting period. To do so, add a timer to the node. (That is how Service1
is configured in the process definition snippet below.) The timer can be set for a certain period, in this case, ten seconds:
<node name="Service1"> <action class= "org.jboss.soa.esb.services.jbpm.actionhandlers.EsbActionHandler"> <esbCategoryName>MockCategory</esbCategoryName> <esbServiceName>MockService</esbServiceName> </action> <timer name='timeout' duedate='10 seconds' transition='time-out-transition'/> <transition name="ok" to="Service2"></transition> <transition name="time-out-transition" to="ExceptionHandling"/> </node>
Service1
has two outgoing transitions. The first of these is ok
whilst the second one is time-out-transition
.
Normally the call-back will signal the default transition, which is
ok
, since it is defined as the first. However, if the processing of the service takes more then ten seconds, the timer will run instead. The timer's transition attribute is set to time-out-transition
, meaning that this transition will be taken on timing-out.
The processing ends up in the
ExceptionHandling
node. From here, you can perform compensatory work.
Exception Transition:
You can define an exceptionTransition
to handle any exceptions that occurs in the midst of the service being processed. Doing so sets the faultTo
endpoint reference on the message, meaning that the Enterprise Service Bus will make a call-back to this node. This signals the exceptionTransition
.
Service2
has two outgoing transitions: the ok
transition will be taken when things are happening normally, whilst the exception
transition will be taken when the service has, as its name indicates, thrown an exception during processing:
<node name="Service2"> <action class= "org.jboss.soa.esb.services.jbpm.actionhandlers.EsbActionHandler"> <esbCategoryName>MockCategory</esbCategoryName> <esbServiceName>MockService</esbServiceName> <exceptionTransition>exception</exceptionTransition> </action> <transition name="ok" to="Service3"></transition> <transition name="exception" to="ExceptionHandling"/> </node>
In the preceding definition of
Service2
, the action's exceptionTransition is set to exception
. In this scenario, the process itself also ends up in the ExceptionHandling
node.
Exception Decision:
Observe the configuration of Service3
and the exceptionDecision
node that follows it. Service3
processes to a normal conclusion and the transition out of its node occurs as one would expect.
However, at some point during the service execution, an
errorCode
was set, and the exceptionDecision
node checks if a variable of the same name has been called here:
<node name="Service3"> <action class= "org.jboss.soa.esb.services.jbpm.actionhandlers.EsbActionHandler"> <esbCategoryName>MockCategory</esbCategoryName> <esbServiceName>MockService</esbServiceName> <esbToBpmVars> <mapping esb="SomeExceptionCode" bpm="errorCode"/> </esbToBpmVars> </action> <transition name="ok" to="exceptionDecision"></transition> </node> <decision name="exceptionDecision"> <transition name="ok" to="end"></transition> <transition name="exceptionCondition" to="ExceptionHandling"> <condition>#{ errorCode!=void }</condition> </transition> </decision>
The
esbToBpmVars
mapping element extracts the errorCode
called SomeExceptionCode
from the message's body and sets in the JBPM context. (This is assuming that the SomeExceptionCode
is set.)
In the next node, named
exceptionDecision
, the ok
transition is taken if processing is normal, but if a variable called errorCode
is found in the JBPM context, the exceptionCondition
transition is taken instead.
To configure the system in this way, you need to use the JBPM's decision node feature. It allows you to nest multiple transitions within a condition:
<condition>#{ errorCode!=void }</condition>
Note
To learn more about conditional transitions, refer to the JBPM Reference Guide.