Monday, September 19, 2022

HTTP Retry - Groovy + Looping Process Call

This blog post will cover the HTTP Retry process using a Groovy script and Looping process call combination. Sometimes, we get a temporary time out error on the HTTP endpoint. To tackle this, we can use this mechanism to do a http retry for a specific interval / specific number of times. 


Overall iFlow Design:


Set Properties in Content modifier:

For the Groovy script to work, we need to declare the below properties.



Looping Process Call:




HTTP Retry Groovy Script:


If the HTTP Response code is greater than or equal to 400, the script will retry depending on the retry_interval and retry_limit properties.

-------------------------------------------------------------------------------------------------------------


import com.sap.gateway.ip.core.customdev.util.Message;

import java.util.HashMap;

def Message processData(Message message) {


        def map = message.getHeaders();

        def  httpStatusCode = map.get("CamelHttpResponseCode") as Integer;

        def _loopIndex=message.getProperty("CamelLoopIndex") as Integer;

        def _propRetryLimit = message.getProperty("propRetryLimit") as Integer;

        def _propRetryInterval=message.getProperty("propRetryInterval") as Integer;

        def _retryFlag=false;

        

        switch(httpStatusCode>=400)

        {

            case true:

            switch(_loopIndex+1<=_propRetryLimit) {

                case true:

                    _retryFlag=true;

                    sleep(_propRetryInterval);

                    break;

                case false:

                    _retryFlag=false;

                    //def ex = properties.get("CamelExceptionCaught");

                    throw new Exception("HTTP request failed after exausting configured retry limit with status code:"+map.get("CamelHttpResponseCode")+" response body:"+ message.getBody(java.lang.String));

                    break;

            }

            break;

            case false:

            _retryFlag=false;

            break;

        }

        

        message.setProperty("propIsRetry",_retryFlag)

       

       return message;

}


-------------------------------------------------------------------------------------------------------------

Note:

Disable the throw exception checkbox in HTTP Adapter, else the flow won't move to the next shape (Groovy script) and will get halted.






With Throw Exception Enabled, error message will be as below: (here I am purposefully making the http call fail)




Flow with Throw Exception Enabled: It won't go to the next shape avail - Groovy script




-------------------------------------------------------------------------------------------------------------

With Throw Exception Disabled, error message will be as below: (here I am purposefully making the http call fail)





CamelLoopIndex starts with zero.




------------------------------------------------AMOR FATI---------------------------------------------------



XSLT Mapping - Extract Integer value

 I had a requirement where in we wanted to extract the km value from a node value.


Input XML Example:

-------------------------------------------------------------------------------

<root>

<test>Nasik 180km Bombay</test>

</root>




-------------------------------------------------------------------------------


XSLT Mapping used:

-------------------------------------------------------------------------------

<xsl:stylesheet version="1.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:strip-space elements="*"/>


 <xsl:template match="/*">

     <xsl:value-of select=

      "translate(.,translate(., '0123456789', ''), '')"/>

 </xsl:template>

</xsl:stylesheet>


-------------------------------------------------------------------------------





Output:





-------------------------------------Love Your Fate ----------------------------------------------------------