0

i'm sending a request to a web service through Http.outboundGateway, and i 'm expecting to have a response with on of these three cases

1- Response success [Ok]

2- Connection Failure [ need to Retry]

3- response return with error code, ex. 400 [save it]

i used advice() after poller to reattempt the Connection Failure, but The problem is that the Error Message exception was thrown in both cases ( Connection Failure , response error code ), so the retry was called for both cases

How could i differentiate between of them and only use the Retry advice for the Connection Failure

.handle(
    Http.outboundGateway(propertiesConfig.getURL())
    ......
    , endpoint -> endpoint                                  
    .poller(Pollers.fixedDelay(delayBetweenRequests)
    .errorChannel("errorChannel")
    .taskExecutor(executor)                                                    
    .receiveTimeout(timeoutDelay)
    )
    .advice(retryAdvice)
)

Retry advice creation bean

@Bean("retryAdvice")
public RequestHandlerRetryAdvice maspRetryAdvice() {
Request

HandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
    RetryTemplate retryTemplate = new RetryTemplate();
    FixedBackOffPolicy policy = new FixedBackOffPolicy();
    policy.setBackOffPeriod(interval);
    retryTemplate.setBackOffPolicy(policy);

    SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
    retryPolicy.setMaxAttempts(nRetry);
    retryTemplate.setRetryPolicy(retryPolicy);

    retryAdvice.setRetryTemplate(retryTemplate);
    ErrorMessageSendingRecoverer recover = new 
    ErrorMessageSendingRecoverer(aggregatorChannel());
    handlerRetryAdvice.setRecoveryCallback(recover);
    return retryAdvice;
} 
Ramy Ahmed
  • 11
  • 1
  • i could fix the issue, throw choosing which exception to execute the retry at the sampleRetryPolicy, you can define type of exception or even traverse in the error searching for it ` Map, Boolean> throwableBooleanMap = new HashMap<>(); throwableBooleanMap.put(java.net.ConnectException.class,true); SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(nRetry, throwableBooleanMap, true);` – Ramy Ahmed May 16 '18 at 07:51

1 Answers1

0

I was facing almost the exact same challenge. For Connection time out this link provides the exact solution: [Configure error handling and retry for Http.outboundGateway spring dsl ] [1]: Configure error handling and retry for Http.outboundGateway spring dsl

.... .handle(Http.outboundGateway( parser().parseExpression("headers[url]"))

                    .httpMethod(HttpMethod.POST)
                    .headerMapper(headerMapper())

                    .expectedResponseType(String.class)
                    .requestFactory(clientHttpRequestFactory())

                    // The inner writer method is doing nothing, just place holder for future usage, 
                    // errorHandler is necessary to capture :
                    // e.g. org.springframework.web.client.HttpClientErrorException: 403 Forbidden
                    .errorHandler(responseErrorFileWriter())
                    , (Consumer<GenericEndpointSpec>)e -> e.advice(retryAdvice()))