0

I have integrated a rest template within my spring boot application. The rest template successfully establishes connection and read timeouts. I have also integrated a retry template to retry on these connection and read timeouts. The code for my retry template is below. I am having a problem with my code and cannot figure it out. Any insight would be very helpful.I am having an error with the "callback" it stating it could not be autowired and no beans were found. For 'FAILED' I am getting a cannot resolve symbol.

 @Bean
public RetryTemplate retryTemplate(RetryCallback<RetryTemplate, Exception> callback) {
    RetryTemplate template = new RetryTemplate();
    ExceptionClassifierRetryPolicy retryPolicy = new ExceptionClassifierRetryPolicy();
    Map<Class<? extends Throwable>, Boolean> retryableExceptions = new HashMap<Class<? extends Throwable>, Boolean>();
    retryableExceptions.put(SocketTimeoutException.class, Boolean.TRUE);
    retryableExceptions.put(ConnectionPoolTimeoutException.class, Boolean.TRUE);
    retryableExceptions.put(ResourceAccessException.class, Boolean.TRUE);

    NeverRetryPolicy doNotRetry = new NeverRetryPolicy();
    SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(maxAttempts, retryableExceptions);
    simpleRetryPolicy.setMaxAttempts(maxAttempts);


    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(backOffPeriod);

    template.setRetryPolicy(simpleRetryPolicy);
    template.setBackOffPolicy(backOffPolicy);

    try {
        return template.execute(callback);
    } catch (Exception e) {
        logger.error("Retry processing failed " + e.getMessage());
        return RetryTemplate.FAILED;
    }
}
Dave Michaels
  • 847
  • 1
  • 19
  • 51

1 Answers1

0

A @Bean factory method is simply a bean definition; you can't put runtime code like this

try {
    return template.execute(callback);
} catch (Exception e) {
    logger.error("Retry processing failed " + e.getMessage());
    return RetryTemplate.FAILED;
}

there. You must return template;.

Since you are passing in a RetryCallback into the factory method, you must have defined a @Bean of that type elsewhere.

However, the callback is used at runtime, not at bean definition time.

You then use the template.execute() elsewhere in your application.

Exactly what is RetryTemplate.FAILED - there is no such constant.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • I see. My mindset is to execute the retry template in the event an exception in any of those classes mentioned above. I had read this syntax is to be used for executing this template or to return a message the retry template did not execute since an exception was not caught. Is there a better more efficient way to execute this retryTemplate? – Dave Michaels Oct 15 '19 at 20:11
  • You have to always execute within the `template.execute()` and the retry policy determines whether the exception is retryable - the template has to catch the exception to make the determination; hence all calls have to go through it. – Gary Russell Oct 15 '19 at 20:31