当前位置:网站首页>@Retryable @Backoff @Recover 重试注解的使用
@Retryable @Backoff @Recover 重试注解的使用
2022-07-20 08:10:00 【WuSong1999】
@Retryable 注解各参数功能如下
使用场景:
处理RetryableException.class异常,如果抛出了RetryableException.class异常(value = RetryableException.class),则开始重试机制.
重试最大次数为5次(maxAttempts = 5),第一次是100毫秒。
后面的每一次重试都是前面的1.5倍(Backoff(delay = 100L, multiplier = 1.5))
@Backoff 注解各参数功能如下
注意点:
在使用@Retryable和@Recover注解时,有几点需要注意:
首先,要添加maven依赖
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
并添加@EnableRetry注解使@Retryable和@Recover注解生效。
另外@Retryable和@Recover修饰的方法要在同一个类中,且被@Retryable 标记的方法不能有返回值,这样Recover方法才会生效。
最后,由于@Retryable注解是通过切面实现的,因此我们要避免@Retryable 注解的方法的调用方和被调用方处于同一个类中,因为这样会使@Retryable 注解失效
样例:
@ApiOperation("测试重试")
@PostMapping("/testRetry")
public XXFResponseResult<Void> testRetry(){
recordService.testRetry();
return new XXFResponseResult<>();
}
void testRetry();
@Retryable(value = {RetryException.class},//指定发生的异常进行重试
maxAttempts=3, //重试次数,默认即为3
backoff = @Backoff(value = 2000))//每次重试延迟毫秒数
@Override
public void testRetry() {
log.info("retry start");
throw new RetryException("retry fail");
}
@Recover
public void recover (RetryException e) {
log.info("recovery,{}",e.getMessage());
}
边栏推荐
- JMeter Association
- File upload and download
- ASP. Net core usage record 3
- [El upload realizes a function of modifying avatar]
- Iptables firewall experiment
- R 语言计算定积分
- The best method of converting string to date in R language
- tp5.1 打开网址 输出的是网页代码输出 没有转变过 没有渲染(模板 return $this-&gt;fetch() return view();)
- R 语言 rmd输出有中文 ,怎么去除中文
- Google Play 商品详情,考试内容
猜你喜欢
随机推荐
CTFShow-MISC入门篇
Location unavailable - file or directory is corrupt and unreadable
JUC包下的常见类
Iptables firewall experiment
Node JS processes pictures asynchronously and JSON text asynchronously
Google Play 商品详情,考试内容
ASP. Net core using Autofac
Two ways of QT multithreading implementation thread implementation
Google play product details, exam content
Comparative evaluation of reverse analysis tool IDA and open source tools ghidra and cutter
Entity annotation - batch generate 10000 test data
【el-upload实现一个修改头像的功能】
sql递归随记
R 语言 rmd输出有中文 ,怎么去除中文
R语言 | jiebaR 词频统计
Emplacement non disponible - fichier ou répertoire corrompu et illisible
[recursion] 7-segment code (backtracking + end condition to judge the constraint of adjacent nodes through joint set lookup)
【Verilog数字系统设计(夏宇闻)----Verilog的基础知识1】
逆向分析工具IDA与开源工具Ghidra、Cutter对比测评
Rmarkdown与Word文档的交互