cancel
Showing results for 
Search instead for 
Did you mean: 

Catching exception in Service Task & Directing to User Task

sankarts
Champ in-the-making
Champ in-the-making
Using Java logic (not explicitly modeled in BPMN), e.g. catch a database persistence exception from a Service Task and direct it to a User Task (another sequence flow for the failure case). 

In this case, my plan is that the Service Task implements ActivityBehavior instead of Java Delegate.

Is this a viable and preferred approach? If not, please suggest alternatives. I am using Activiti 5.16.1.

Thanks in advance.
4 REPLIES 4

sankarts
Champ in-the-making
Champ in-the-making
It would be great, if someone well versed with Activiti can throw some light on my query. Thanks.

trademak
Star Contributor
Star Contributor
Hi,

Yes that's a viable approach. Another way would be to throw a BPMN error in that case and catch it with a boundary error event that connects to a user task. And it's also possible to use an exclusive gateway and check on a specific variable you set in the service task.

Best regards,

sankarts
Champ in-the-making
Champ in-the-making
Thanks for the quick response.

BTW, are you referring to the approach mentioned in the user guide for ActivityBehavior. If yes,

1) Is it possible without specifying exception and no-exception sequence flows in the workflow spec i.e. in .bpmn file? If yes, any pointers on this please?

Details extracted teh User Guide as below.
<xml>
serviceTask id="javaService"
  name="Java service invocation"
  activiti:class="org.activiti.ThrowsExceptionBehavior"
/serviceTask
   
sequenceFlow id="no-exception" sourceRef="javaService" targetRef="theEnd" /
sequenceFlow id="exception" sourceRef="javaService" targetRef="fixException" /

and the sample code snippet looks like
<java>
public class ThrowsExceptionBehavior implements ActivityBehavior {

  public void execute(ActivityExecution execution) throws Exception {
    String var = (String) execution.getVariable("var");

    PvmTransition transition = null;
    try {
      executeLogic(var);
      transition = execution.getActivity().findOutgoingTransition("no-exception");
    } catch (Exception e) {
      transition = execution.getActivity().findOutgoingTransition("exception");
    }
    execution.take(transition);
  }
 
}
</java>

jbarrez
Star Contributor
Star Contributor
If I'm following you, that would need to be configured in your own Behaviour (and use eg Field injection to configure those)