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"/>
</proxy>

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

<sequence name="DSS_EmployeeDetailDataService_IN">
        <log level="full"/>
        <send>
            <endpoint key="DSS_EmployeeDetailDataService_EPR"/>
        </send>
    </sequence>
                          
  <sequence name="DSS_EmployeeDetailDataService_Fault">
        <log level="full"/>
        <payloadFactory>
            <format>
                <shr:employees xmlns:shr="http://shriwithjava.blogspot.co.nz/">
                    <shr:employee>
                        <shr:id>Exception has been caught</shr:id>
                        <shr:name>Exception has been caught</shr:name>
                        <shr:address>Exception has been caught</shr:address>
                    </shr:employee>
                </shr:employees>
            </format>
        </payloadFactory>
        <property name="RESPONSE" value="true"/>
        <header name="To" action="remove"/>
        <send/>
        <drop/>
    </sequence>
<sequence name="CommonSequence_OUT">
        <log level="full"/>
        <send/>
</sequence>
<endpoint name="DSS_EmployeeDetailDataService_EPR">
        <address uri="http://localhost:9763/services/EmployeeDataService">
            <timeout>
                <duration>10000</duration>
                <responseAction>fault</responseAction>
            </timeout>
            <suspendOnFailure>
                <errorCodes>101500,101501,101506,101507,101508</errorCodes>
                <initialDuration>3000</initialDuration>
                <progressionFactor>1.0</progressionFactor>
            </suspendOnFailure>
            <markForSuspension>
                <errorCodes>101504,101505</errorCodes>
                <retriesBeforeSuspension>3</retriesBeforeSuspension>
                <retryDelay>1</retryDelay>
            </markForSuspension>
        </address>
</endpoint>