[英]Spring retry exception handling execution behavior
我正在嘗試以最佳方式將spring-retry
@Retryable
注釋包裝在外部服務調用周圍。 這是我的代碼:
@Retryable(exclude = HttpClientErrorException.BadRequest.class, value = RestClientException.class)
private ResponseEntity<Item> retrieveItemById(String id)
{
HttpHeaders headers = new HttpHeaders();
try {
return restTemplate.exchange(httpConnectionProperties.getBaseUrl() + "/items",
HttpMethod.GET, new HttpEntity<>(item, headers), Item.class, id);
}
catch (RestClientException e) {
log.error("Exception occurred while retrieving an item" , e);
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
關於發生RestClientException
時會發生什么,我有幾個問題:
retry
開始之前執行catch 塊還是在catch 塊執行之前開始重試? 我需要恢復塊嗎?由於您正在捕獲並“處理”異常,因此重試被禁用; 僅當方法引發異常時,重試才會起作用。
要更改結果(而不是在重試用盡時向調用者拋出異常,您需要一個@Recover
方法。
不可重試的異常會直接出現; 您可以為不同的異常類型設置多個@Recover
方法,或者使用一個通用的方法,您可以自己檢查異常類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.