[英]Spring retry policy for connection and read timeouts
我有一個 spring 啟動應用程序,我在其中實現了 rest 模板並設置了連接和讀取超時。 我正在嘗試在執行這些超時時為應用程序重試實施重試策略。 發生超時時,連接和讀取超時將引發 ResourceAccessException。 進一步了解每個超時的詳細信息,連接超時顯示 ConnectionTimeoutException,讀取超時顯示 SocketTimeoutException。 我已經實現了一個 RetryTemplate,如下所示,以執行一定次數,並且需要幫助執行它並重試這些超時異常。 任何建議將不勝感激。 謝謝!!
@Bean
public RetryTemplate retryTemplate() {
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
retryPolicy.setMaxAttempts(maxAttempts);
FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
backOffPolicy.setBackOffPeriod(backOffPeriod);
RetryTemplate template = new RetryTemplate();
template.setRetryPolicy(retryPolicy);
template.setBackOffPolicy(backOffPolicy);
return template;
}
public class RetryService {
@Autowired
private RetryTemplate retryTemplate;
public class RetryService {
@Autowired
private RetryTemplate retryTemplate;
public void withTemplate() {
retryTemplate.execute(retryContext -> {
});
}
}
您可以使用其中一種高級重試策略(閱讀他們的 javadocs)。
或者,只需使用其更豐富的構造函數之一配置SimpleRetryPolicy
,例如this 。
/**
* 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. The default value indicates whether to retry or not for exceptions (or super
* classes) are not found in the map.
* @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
* @param defaultValue the default action.
*/
public SimpleRetryPolicy(int maxAttempts, Map<Class<? extends Throwable>, Boolean> retryableExceptions,
boolean traverseCauses, boolean defaultValue) {
使用您想要重試的異常配置 map。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.