Saturday, May 1, 2021

WSO2 EI Backend Retry Handling

 Requirement:

Retry backend call 3 times with 5 seconds delay when backend not available.

Implementation:

Use below in-sequence and fault-sequence for your mediation.

1. sample-mediation-in-sequence

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="sample-mediation-in-sequence" onError="sample-mediation-fault-sequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <log>
        <property value="Invoking in sequence" name="sample-mediation-in-sequence"/>
    </log>
    <enrich>
        <source clone="true" type="body"/>
        <target property="payload" type="property"/>
    </enrich>
    <log>
        <property value="Calling MAAIF backend" name="sample-mediation-in-sequence"/>
    </log>
    <call>
        <endpoint>
            <http method="post" uri-template="http://sample.backend.url"/>
        </endpoint>
    </call>
    <log>
        <property expression="fn:concat('[MAAIF] ' , $ctx:uri.var.path,  ' status ', get-property('axis2', 'HTTP_SC'))" name="sample-mediation-in-sequence"/>
    </log>
    <respond/>
</sequence>

Note: Change this as per your backend.

2. sample-mediation-fault-sequence

<?xml version="1.0" encoding="UTF-8"?>
<sequence name="sample-mediation-fault-sequence" trace="disable" xmlns="http://ws.apache.org/ns/synapse">
    <filter xmlns:ns="http://org.apache.synapse/xsd" xmlns:ns3="http://org.apache.synapse/xsd" xpath="get-property('retry_count')">
        <then>
            <log>
                <property name="[Error] sample-mediation-fault-sequence" value="Error invorking backend API"/>
                <property expression="get-property('CODE')" name="error code"/>
                <property expression="get-property('ERROR_MESSAGE')" name="error message"/>
            </log>
            <property expression="number(get-property('retry_count'))+1" name="retry_count" scope="default"/>
            <filter xpath="get-property('retry_count') > 3">
                <then>
                    <payloadFactory media-type="xml">
                        <format>
                            <error xmlns="">
                                <details>$1</details>
                                <code>$2</code>
                                <message>$3</message>
                            </error>
                        </format>
                        <args>
                            <arg value="Error invorking backend API"/>
                            <arg evaluator="xml" expression="get-property('CODE')"/>
                            <arg evaluator="xml" expression="get-property('ERROR_MESSAGE')"/>
                        </args>
                    </payloadFactory>
                    <property name="HTTP_SC" scope="axis2" type="STRING" value="500"/>
                    <respond/>
                </then>
                <else>
                    <log>
                        <property name="[Error] sample-mediation-fault-sequence" expression="fn:concat('retry count ' , $ctx:retry_count)"/>
                    </log>
                    <script language="js"><![CDATA[java.lang.Thread.sleep(5000);]]></script>
                    <log>
                        <property name="[Error] sample-mediation-fault-sequence" value="Retrying backend call"/>
                    </log>
                    <clone continueParent="true" sequential="false">
                        <target sequence="sample-mediation-in-sequence"/>
                    </clone>
                </else>
            </filter>
        </then>
        <else>
            <property name="retry_count" scope="default" type="STRING" value="1"/>
            <clone continueParent="true" sequential="false">
                <target sequence="sample-mediation-fault-sequence"/>
            </clone>
        </else>
    </filter>
</sequence>

No comments:

Post a Comment