超时和重试

超时和重试并且 我们通常还会设置重试的间隔 比如说我们要重试 3 次的话 第 1 次请求失败后 等待 1 秒再进行重试 第 2 次请求失败后 等待 2 秒再进行重试 第 3 次请求失败后 等待 3 秒再进行重试

大家好,欢迎来到IT知识分享网。

@EnableRetry @SpringBootApplication public class TestApplication { public static void main(String[] args) { SpringApplication.run(TestApplication.class, args); } }
@Slf4j @Service public class RetryService { @Retryable(value= IllegalArgumentException.class, maxAttempts = 4) public void test(int age) { log.info("Age={}", age); if (age <= 0) { throw new IllegalArgumentException("Illegal Data"); } } @Recover public void recover(IllegalArgumentException e) { log.info("Message={}", e.getMessage()); } }

运行结果

2023-08-03 15:39:39 [http-nio-8080-exec-7] INFO cn.hwd.service.RetryService - Age=-13 2023-08-03 15:39:40 [http-nio-8080-exec-7] INFO cn.hwd.service.RetryService - Age=-13 2023-08-03 15:39:41 [http-nio-8080-exec-7] INFO cn.hwd.service.RetryService - Age=-13 2023-08-03 15:39:42 [http-nio-8080-exec-7] INFO cn.hwd.service.RetryService - Age=-13 2023-08-03 15:39:42 [http-nio-8080-exec-7] INFO cn.hwd.service.RetryService - Message=Illegal Data 2023-08-03 15:39:46 [http-nio-8080-exec-9] INFO cn.hwd.service.RetryService - Age=0 2023-08-03 15:39:47 [http-nio-8080-exec-9] INFO cn.hwd.service.RetryService - Age=0 2023-08-03 15:39:48 [http-nio-8080-exec-9] INFO cn.hwd.service.RetryService - Age=0 2023-08-03 15:39:49 [http-nio-8080-exec-9] INFO cn.hwd.service.RetryService - Age=0 2023-08-03 15:39:49 [http-nio-8080-exec-9] INFO cn.hwd.service.RetryService - Message=Illegal Data 2023-08-03 15:39:52 [http-nio-8080-exec-8] INFO cn.hwd.service.RetryService - Age=7 2023-08-03 15:39:58 [http-nio-8080-exec-10] INFO cn.hwd.service.RetryService - Age=9 

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/134100.html

(0)
上一篇 2025-07-14 22:26
下一篇 2025-07-14 22:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信