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
-------------------------------------------------------------------------------------------------------------
------------------------------------------------AMOR FATI---------------------------------------------------
No comments:
Post a Comment