当前位置: 首页 > news >正文

基于 Guava Retry 在Spring封装一个重试功能

pom依赖

<dependency><groupId>com.github.rholder</groupId><artifactId>guava-retrying</artifactId><version>2.0.0</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId>
</dependency>

注解类

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Retryable {/*** 需要重试的异常类型* this.isAssignableFrom(方法抛出的异常) 也就是说,方法抛出的异常必须是retryOn的子类或者子接口*/Class<? extends Throwable> retryOn() default Throwable.class;// 重试次数int maxAttempts() default 3;// 重试间隔long delayMillis() default 1000;
}

import com.github.rholder.retry.RetryException;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;@Component
@Aspect
@Order(1)
public class RetryableAspect {@Around("@annotation(retryable)")public Object retry(ProceedingJoinPoint joinPoint, Retryable retryable) throws Throwable {Retryer<Object> retryer = RetryerBuilder.<Object>newBuilder().retryIfExceptionOfType(retryable.retryOn()).withStopStrategy(StopStrategies.stopAfterAttempt(retryable.maxAttempts())).withWaitStrategy(WaitStrategies.fixedWait(retryable.delayMillis(), TimeUnit.MILLISECONDS)).build();try {return retryer.call(() -> {try {return joinPoint.proceed();} catch (Exception exception) {throw exception;} catch (Throwable e) {throw new WrapRetryThrowable(e);}});} catch (RetryException e) {throw e.getLastFailedAttempt().getExceptionCause();}}public static class WrapRetryThrowable extends Exception {public WrapRetryThrowable(Throwable cause) {super(cause);}}}

测试类

在这里插入代码片@RestController
public class RetryController {/*** 顶级异常类测试* @return* @throws Throwable*/@Retryable(retryOn = Exception.class,maxAttempts = 3,delayMillis = 1000)@GetMapping("/throwable")public String performTask() throws Throwable {System.out.println("performTask" + System.currentTimeMillis());// 在这里实现可能抛出异常的业务逻辑throw new Throwable("error");}/*** 异常类测试* @return* @throws CustomException*/@Retryable(retryOn = CustomException.class,maxAttempts = 2,delayMillis = 1000)@GetMapping("/customException")public void customException() {System.out.println("customException" + System.currentTimeMillis());// 在这里实现可能抛出异常的业务逻辑}// 抛出的异常跟枚举异常不一致,不会重试@Retryable(retryOn = CustomException.class,maxAttempts = 3,delayMillis = 1000)@GetMapping("/customException2")public String customException2() throws Exception {System.out.println("customException2" + System.currentTimeMillis());// 在这里实现可能抛出异常的业务逻辑throw new Exception("这是一段自定义异常的抛出");}}
http://www.lryc.cn/news/116558.html

相关文章:

  • 适用HarmonyOS 3.1版本及以上的应用及服务开发工具 DevEco Studio 3.1.1 Release 安装
  • [信号与系统系列] 正弦振幅调制之差拍信号
  • vb+SQL航空公司管理系统设计与实现
  • python爬取网页视频
  • 数据挖掘具体步骤
  • react class与hooks区别
  • Python爬虫思维:异常处理与日志记录
  • (十六)大数据实战——安装使用mysql版的hive服务
  • 【信号生成器】从 Excel 数据文件创建 Simulink 信号生成器块研究(Simulink)
  • 【UE4 RTS】01-Camera SetUp
  • Mirror网络库 | 说明
  • 分布式异步任务处理组件(九)
  • [excel]vlookup函数对相同的ip进行关联
  • 两个状态的马尔可夫链
  • SpringBoot 依赖管理
  • 重试框架入门:Spring-RetryGuava-Retry
  • [QCM6125][Android13] 修复PRODUCT_COPY_FILES无法拷贝so
  • 微服务Eureka注册中心
  • Java:企业级java后端开发,需要掌握哪些内容
  • 使用Go语言生成Excel任务表依赖图(Markdown文件mermaid图)
  • C语言和C++的区别在哪?如何自学C++?
  • 功能强大的开源数据中台系统 DataCap 1.13.0 发布
  • JTS Self-intersection异常TopologyException: side location conflict解决办法
  • Maven: No compiler is provided in this environment.
  • .NET-10. 其他-VSTO+VBA
  • 相机传感器格式与镜头光圈参数
  • Android 设置头像(拍照获取、相册获取、裁剪照片)
  • android开发之Android 自定义滑动解锁View
  • CAD绘制法兰、添加光源、材质并渲染
  • ChatGPT访问流量下降的原因分析