ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Springboot 整合Retry 实现重试机制,java架构师课程推荐

2021-12-28 14:02:40  阅读:183  来源: 互联网

标签:Exception Retry java Springboot int springframework 重试 import public


然后创建一个测试重试的TestRetryService.java:

/**

  • @Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

public interface TestRetryService {

int dignifiedTest(int code) throws Exception;

}

然后是TestRetryServiceImpl.java:

import com.mail.elegant.service.TestRetryService;

import org.springframework.retry.annotation.Backoff;

import org.springframework.retry.annotation.Recover;

import org.springframework.retry.annotation.Retryable;

import org.springframework.stereotype.Service;

import java.time.LocalTime;

/**

《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》

【docs.qq.com/doc/DSmxTbFJ1cmN1R2dB】 完整内容开源分享

@Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

@Service

public class TestRetryServiceImpl implements TestRetryService {

@Override

@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5))

public int dignifiedTest(int code) throws Exception{

System.out.println(“dignifiedTest被调用,时间:”+LocalTime.now());

if (code==0){

throw new Exception(“情况不对头!”);

}

System.out.println(“dignifiedTest被调用,情况对头了!”);

return 200;

}

@Recover

public int recover(Exception e){

System.out.println(“回调方法执行!!!!”);

//记日志到数据库 或者调用其余的方法

return 400;

}

}

到这里,已经整合完毕,最后剩下测试了,在测试前,我们先一起来看看代码里面的关键信息的意义:

可以看到代码里面,这个方法上面加上了注解 ,

@Retryable(value = Exception.class,maxAttempts = 3,backoff = @Backoff(delay = 2000,multiplier = 1.5,maxDelay=360000L))

@Retryable : 注解方式标记当前方法会使用重试机制

里面的 value: 重试的触发机制,当遇到Exception异常的时候,触发;

maxAttempts: 重试的次数(包括第一次调用,也就是说如果设置3次,调用一次后,如果一直失败触发重试,那么还当前方法还会调用2次);

delay:重试的延迟时间,也就是距离上一次重试方法调用的间隔,单位毫秒

multiplier: delay间隔时间的倍数,也就是说,第一次重试间隔如果是2000ms,那第二次重试的时候就是2000ms 乘以这个倍数1.5,就是3000ms;

maxDelay:重试次数之间的最大时间间隔,默认为0,即忽略,如果小于delay的设置,则默认为30000L;

再来看下面的这个小方法:

@Recover

public int recover(Exception e){

System.out.println(“回调方法执行!!!!”);

//记日志到数据库 或者调用其余的方法

return 400;

}

这个方法用到了**@Recover**,也就是用注解方式标记当期方法为回调方法,可以看到传参里面写的是 Exception e,这个是作为回调的接头暗号(重试次数用完了,还是失败,我们抛出这个Exception e通知触发这个回调方法)。

PS:该回调方法与重试方法写在同一个实现类里面。

然后在启动类加上开启重试注解:


@EnableRetry

好了,基本简单讲解完毕,接下来测试看看什么效果,

创建一个TestController.java ,写过简单的测试方法:

import com.mail.elegant.service.TestRetryService;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RestController;

/**

  • @Author : JCccc

  • @CreateTime : 2019/8/16

  • @Description :

**/

@RestController

public class TestController {

@Autowired

TestRetryService testRetryServiceImpl;

@GetMapping("/testRetry")

public String testRetry() throws Exception {

int code=0;

int result = testRetryServiceImpl.dignifiedTest(code);

return “result:”+result;

}

}

标签:Exception,Retry,java,Springboot,int,springframework,重试,import,public
来源: https://blog.csdn.net/m0_65322636/article/details/122191875

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有