1

I want to use Spring retry functionality in case of 'SocketTimeoutException' from rest template.

but spring Rest template throwing like bellow: org.springframework.web.client.ResourceAccessException: I/O error: Read timed out; nested exception is java.net.SocketTimeoutException: Read timed out

I have added SocketTimeoutException in Retry Template Map. Spring retry works only if I add SocketTimeoutException in Retry Template Map or Do I need to add ResourceAccessException also.

Veerendra
  • 31
  • 7

1 Answers1

1

You need to use a custom SimpleRetryPolicy that has the traverseCauses option set. Then, instead of just looking at the top level exception, it will examine the cause hierarchy to look for a match.

/**
 * Create a {@link SimpleRetryPolicy} with the specified number of retry
 * attempts. If traverseCauses is true, the exception causes will be traversed until
 * a match is found.
 *
 * @param maxAttempts the maximum number of attempts
 * @param retryableExceptions the map of exceptions that are retryable based on the
 * map value (true/false).
 * @param traverseCauses is this clause traversable
 */
public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions,
        boolean traverseCauses) {
    this(maxAttempts, retryableExceptions, traverseCauses, false);
}
Gary Russell
  • 166,535
  • 14
  • 146
  • 179