spring-rsetry注解实现循环重试功能
温馨提示:
本文最后更新于 2022年11月16日,已超过 527 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
循环重试用到的场景
一、@Retryable是什么?
二、使用步骤
1.POM依赖
org.springframework.retry
spring-retry
2.启用@Retryable
@EnableRetry
@SpringBootApplication
public class HelloApplication {
public static void main(String[] args) {
SpringApplication.run(HelloApplication.class, args);
}
}
3.在方法上添加@Retryable
@Service
public class TestRetryServiceImpl implements TestRetryService {
@Override
@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))
public int test(int code) throws Exception{
System.out.println("test被调用,时间:"+LocalTime.now());
if (code==0){
throw new Exception("情况不对头!");
}
System.out.println("test被调用,情况对头了!");
return 200;
}
}
简单解释一下注解中几个参数的含义:
当重试耗尽时还是失败,会出现什么情况呢?
4.@Recover
@Recover
public int recover(Exception e, int code){
System.out.println("回调方法执行!!!!");
//记日志到数据库 或者调用其余的方法
return 400;
}
5.注意事项
正文到此结束
- 本文标签: Spring Java 后端
- 本文链接: http://www.92cxy.cn/article/60
- 版权声明: 本文由王小东原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权
热门推荐
相关文章
该篇文章的评论功能已被站长关闭