使用Spring重试时是否需要@Recovery方法?
我的问题是关于春天的重试。 假设有一个简单的示例代码,其中我有一个服务层和控制器类 这是testService接口使用Spring重试时是否需要@Recovery方法?,spring,spring-boot,spring-retry,resiliency,Spring,Spring Boot,Spring Retry,Resiliency,我的问题是关于春天的重试。 假设有一个简单的示例代码,其中我有一个服务层和控制器类 这是testService接口 public interface testService{ @Retryable(value = { KnownExceptiomn.class }, backoff = @Backoff(delay = 1000), maxAttempts = 2) Address getAddress(String emailAddress); } 这是服务的实现 public
public interface testService{
@Retryable(value = { KnownExceptiomn.class }, backoff = @Backoff(delay = 1000), maxAttempts = 2)
Address getAddress(String emailAddress);
}
这是服务的实现
public class testServiceImpl{
public Address getAddress(String emailAddress){
//addressRepository is a crud repository
return addressRepository.getAddressFromEmail(emailAddress);
}
}
控制器是
@GetMapping("path/{emailId}")
public ResponseEntity<?> getAddress(@PathVariable("emailId") final String email){
final Address address;
try{
address= testService.getAddress(String emailAddress);
if(address != null) return new ResponseEntity<Address>(address,HttpStatus.OK);
return new ResponseEntity<String>("Invalid Email", HttpStatus.BAD_REQUEST);
}catch(KnownException e){
return errorMessage("Error");
}
}
@GetMapping(“path/{emailId}”)
public ResponseEntity getAddress(@PathVariable(“emailId”)最终字符串电子邮件){
最后地址;
试一试{
address=testService.getAddress(字符串emailAddress);
if(address!=null)返回新的ResponseEntity(address,HttpStatus.OK);
返回新的响应属性(“无效电子邮件”,HttpStatus.BAD_请求);
}渔获物(e){
返回错误消息(“错误”);
}
}
如图所示,@Retryble位于服务接口中。但是,我还没有实现@Recover方法。我的想法是,因为我没有任何辅助数据库,如果数据库在下面,就没有恢复选项,我没有添加@recovery方法。相反,在控制器中捕获并处理异常
我的问题是:
@恢复
是可选的;如果没有异常,在重试次数用尽后,最后一个异常将抛出给调用方,调用方必须处理该异常
没有@Recovery
并在调用者中处理异常是完全正常的,不管你喜欢什么方式