Tuesday, November 3, 2015

How to use Fault sequence in WSO2 ESB

In WSO2 ESB, fault sequence is used for exception handling. In service logic if any exception comes and you want ESB to execute a particular scenario or just send a proper exception error to client, just put the logic in fault sequence and it will be executed. This is the same exception handling as we have in other programming language.

Test Case:

In our example, we are trying to call DSS service from the ESB. We have created a simple pass through proxy of DSS service. This DSS service gives you detail of employee based on the Employee ID.

To get an exception, I will stop the DSS server so that ESB will get an exception and we are assuming that on getting the exception, fault sequence will be executed. In fault sequence I have mentioned a normal exception message on the response and same response we will give back to the client.

Below is the EmployeeDataservice I have created in DSS.

Above service gives you detail of employee on passing Employee Id.

Below is the pass through proxy I have created in ESB with name of 'DSS_EmployeeDetailDataServiceProxy’.

From ESB URL, I have created a SOAP UI project which work the same as DSS does as it’s a pass through proxy.

Now I have stopped the DSS server and expecting to get an Exception in ESB for DSS and this scenario will trigger the fault sequence. In fault sequence I have put normal exception message in EmployeeDetailService response.

Exception in Logs:

Complete Exception Message:

You can see that fault block now have been executed and that is why we are getting expected error message what we have put in fault sequence.

ESB Synapse Code:

<proxy name="DSS_EmployeeDetailDataServiceProxy" transports="http" startOnLoad="true" trace="disable" statistics="enable">

<target inSequence="DSS_EmployeeDetailDataService_IN" outSequence="CommonSequence_OUT" faultSequence="DSS_EmployeeDetailDataService_Fault"/>

<publishWSDL key="DSS_EmployeeDetailDataService_wsdl"/>

<localEntry key="DSS_EmployeeDetailDataService_wsdl" src="file:repository/conf/employee/EmployeeDetailDataService.wsdl"/>

<sequence name="DSS_EmployeeDetailDataService_IN">
        <log level="full"/>
            <endpoint key="DSS_EmployeeDetailDataService_EPR"/>
  <sequence name="DSS_EmployeeDetailDataService_Fault">
        <log level="full"/>
                <shr:employees xmlns:shr="http://shriwithjava.blogspot.co.nz/">
                        <shr:id>Exception has been caught</shr:id>
                        <shr:name>Exception has been caught</shr:name>
                        <shr:address>Exception has been caught</shr:address>
        <property name="RESPONSE" value="true"/>
        <header name="To" action="remove"/>
<sequence name="CommonSequence_OUT">
        <log level="full"/>
<endpoint name="DSS_EmployeeDetailDataService_EPR">
        <address uri="http://localhost:9763/services/EmployeeDataService">