Veerendra
Veerendra

Reputation: 41

How to implement Spring Retry for SocketTimeoutException from Rest Template

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.

Upvotes: 2

Views: 1815

Answers (1)

Gary Russell
Gary Russell

Reputation: 174769

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);
}

Upvotes: 1

Related Questions