Search code examples
spring-integrationspring-retry

RequestHandlerRetryAdvice with HttpRequestExecutingMessageHandler not working


I have the below configuration file.

@Configuration
@PropertySource({ "application.properties" })
@EnableIntegration
@IntegrationComponentScan
@EnableRetry
public class IntegrationBeanConfiguration {

    @Bean
    public SimpleRetryPolicy retryPolicy(){
        SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
        retryPolicy.setMaxAttempts(5);
        return retryPolicy;
    }

    @Bean
    public FixedBackOffPolicy fixedBackOffPolicy(){
        FixedBackOffPolicy p = new FixedBackOffPolicy();
        p.setBackOffPeriod(1000);
        return p;
    }

    @Bean
    public RequestHandlerRetryAdvice retryAdvice(SimpleRetryPolicy retryPolicy, FixedBackOffPolicy fixedBackOffPolicy){
        RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
        RetryTemplate retryTemplate = new RetryTemplate();
        retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
        retryAdvice.setRetryTemplate(retryTemplate);
        return retryAdvice;
    }

    @Bean
    @ServiceActivator(inputChannel="rtpRequestPostOperationRequestChannel")
    public MessageHandler httResponseMessageHandler(MessageChannel rtpRequestPostOperationResponseChannel, HeaderMapper<HttpHeaders> headerMapper, RequestHandlerRetryAdvice retryAdvice) {
        List<Advice> list = new ArrayList<>();
        list.add(retryAdvice);
        HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://myhost:8080/rtp/request");
        handler.setHttpMethod(HttpMethod.POST);
        handler.setHeaderMapper(headerMapper);
        handler.setOutputChannel(rtpRequestPostOperationResponseChannel);
        handler.setExpectedResponseType(RtpResponse.class);
        handler.setAdviceChain(list);
        return handler;
    }

}

In my understanding, the retry would trigger if I make a request to a non-existing URL like

https://myhost:8080/rtp/request123

But the retry does not occur. Please advise if my understanding is incorrect or there is something wrong in the configuration.

Thanks


Solution

  • With help from Gary's comment. The following configuration works

    @Configuration
    @PropertySource({ "application.properties" })
    @EnableIntegration
    @IntegrationComponentScan
    @EnableRetry
    public class IntegrationBeanConfiguration {
    
        @Bean
        public SimpleRetryPolicy retryPolicy(){
            SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
            retryPolicy.setMaxAttempts(5);
            return retryPolicy;
        }
    
        @Bean
        public FixedBackOffPolicy fixedBackOffPolicy(){
            FixedBackOffPolicy p = new FixedBackOffPolicy();
            p.setBackOffPeriod(1000);
            return p;
        }
    
        @Bean
        public RequestHandlerRetryAdvice retryAdvice(SimpleRetryPolicy retryPolicy, FixedBackOffPolicy fixedBackOffPolicy){
            RequestHandlerRetryAdvice retryAdvice = new RequestHandlerRetryAdvice();
            RetryTemplate retryTemplate = new RetryTemplate();
            retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
            retryTemplate.setRetryPolicy(retryPolicy);
            retryAdvice.setRetryTemplate(retryTemplate);
            return retryAdvice;
        }
    
        @Bean
        @ServiceActivator(inputChannel="rtpRequestPostOperationRequestChannel")
        public MessageHandler httResponseMessageHandler(MessageChannel rtpRequestPostOperationResponseChannel, HeaderMapper<HttpHeaders> headerMapper, RequestHandlerRetryAdvice retryAdvice) {
            List<Advice> list = new ArrayList<>();
            list.add(retryAdvice);
            HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://myhost:8080/rtp/request");
            handler.setHttpMethod(HttpMethod.POST);
            handler.setHeaderMapper(headerMapper);
            handler.setOutputChannel(rtpRequestPostOperationResponseChannel);
            handler.setExpectedResponseType(RtpResponse.class);
            handler.setAdviceChain(list);
            return handler;
        }
    
    }