1

我想在休息模板中出现“SocketTimeoutException”的情况下使用 Spring 重试功能。

但是spring Rest模板抛出如下所示:org.springframework.web.client.ResourceAccessException:I/O错误:读取超时;嵌套异常是 java.net.SocketTimeoutException: Read timed out

我在重试模板映射中添加了 SocketTimeoutException。只有当我在重试模板映射中添加 SocketTimeoutException 或者我是否还需要添加 ResourceAccessException 时,Spring 重试才有效。

4

1 回答 1

1

您需要使用SimpleRetryPolicy具有traverseCauses选项集的自定义。然后,它不会只查看顶级异常,而是会检查原因层次结构以查找匹配项。

/**
 * 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);
}
于 2019-05-07T13:09:31.683 回答