如何利用Spring AOP实现异常重试

微信公众号:deepstack   欢迎一起交流

背景:在业务中,出现方法执行失败需要重试的场景很多,如网络抖动导致的连接失败或者超市等。

优雅实现

1、减少代码侵入

2、方便可用

3、配置灵活

步骤

1、创建一个annotation。源码如下。
 
 1 /**
 2 * <用来异常重试>
 3 * 注意:needThrowExceptions & catchExceptions 是相关联的, 有顺序依赖。
 4 * 当这两个数组的长度都为0时, 直接执行重试逻辑。
 5 *
 6 * @author lihaitao on 2019/1/2
 7 * @version 1.0
 8 * @see ExceptionRetryAspect
 9 */
10 @Documented
11 @Target(ElementType.METHOD)
12 @Retention(RetentionPolicy.RUNTIME)
13 public @interface ExceptionRetry {
14     /**
15      * 设置失败之后重试次数,默认为1次。
16      * 少于1次,则默认为1次
17      * 推荐最好不要超过5次, 上限为10次
18      * 当没有重试次数时, 会将异常重新抛出用来定位问题。
19      *
20      * @return
21      */
22     int times() default 1;
23  
24     /**
25      * 重试等待时间,时间单位为毫秒。默认是 0.5 * 1000ms, 小于等于0则不生效
26      * 推荐不要超过 3 * 1000ms
27      * 上限为 10 * 1000ms
28      *
29      * @return
30      */
31     long waitTime() default 500; 32 33 /** 34 * 需要抛出的异常, 这些异常发生时, 将直接报错, 不再重试。 35 * 传入一些异常的class对象 36 * 如UserException.class 37 * 当数组长度为0时, 那么都不会抛出, 会继续重试 38 * 39 * @return 异常数组 40 */ 41 Class[] needThrowExceptions() default {}; 42 43 /** 44 * 需要捕获的异常, 如果需要捕获则捕获重试。否则抛出异常 45 * 执行顺序 needThrowExceptions --> catchExceptions 两者并不兼容 46 * 当 needThrowExceptions 判断需要抛出异常时, 抛出异常, 否则进入此方法, 异常不在此数组内则抛出异常 47 * 当数组长度为0时, 不会执行捕获异常的逻辑。 48 * 49 * @return 异常数组 50 */ 51 Class[] catchExceptions() default {}; 52 } 53 
2、有了注解之后,我们还需要对这个注解的方法进行处理。所以我们还要写一个切面。
/**
* <异常重试切面>
*
* @author lihaitao on 2019/1/2
*/
@Aspect
@Component
public class ExceptionRetryAspect {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExceptionRetryAspect.class);
 
    @Pointcut("@annotation(com.jason.annotation.ExceptionRetry)")
    public void retryPointCut() {
    }
 
    @Around("retryPointCut()") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature(); Method method = methodSignature.getMethod(); ExceptionRetry retry = method.getAnnotation(ExceptionRetry.class); String name = method.getName(); Object[] args = joinPoint.getArgs(); String uuid = UUID.randomUUID().toString(); LOGGER.info("执行重试切面{}, 方法名称{}, 方法参数{}", uuid, name, JsonUtil.toJson(args)); int times = retry.times(); long waitTime = retry.waitTime(); Class[] needThrowExceptions = retry.needThrowExceptions(); Class[] catchExceptions = retry.catchExceptions(); // check param if (times <= 0) { times = 1; } for (; times >= 0; times--) { try { return joinPoint.proceed(); } catch (Exception e) { // 如果需要抛出的异常不是空的, 看看是否需要抛出 if (needThrowExceptions.length > 0) { for (Class exception : needThrowExceptions) { if (exception == e.getClass()) { LOGGER.warn("执行重试切面{}失败, 异常在需要抛出的范围{}, 业务抛出的异常类型{}", uuid, needThrowExceptions, e.getClass().getName()); throw e; } } } // 如果需要抛出异常,而且需要捕获的异常为空那就需要再抛出 if (catchExceptions.length > 0) { boolean needCatch = false; for (Class catchException : catchExceptions) { if (e.getClass() == catchException) { needCatch = true; break; } } if (!needCatch) { LOGGER.warn("执行重试切面{}失败, 异常不在需要捕获的范围内, 需要捕获的异常{}, 业务抛出的异常类型{}", uuid, catchExceptions, e.getClass().getName()); throw e; } } // 如果接下来没有重试机会的话,直接报错 if (times <= 0) { LOGGER.warn("执行重试切面{}失败", uuid); throw e; } // 休眠 等待下次执行 if (waitTime > 0) { Thread.sleep(waitTime); } LOGGER.warn("执行重试切面{}, 还有{}次重试机会, 异常类型{}, 异常信息{}, 栈信息{}", uuid, times, e.getClass().getName(), e.getMessage(), e.getStackTrace()); } } return false; }
3、写完了切面,我们再继续处理测试逻辑,看看写的好使不好使,此处的代码是模拟redis链接异常。我们先在redis conn 正常的情况下触发此测试方法,在执行过程中,是否能重试?拭目以待
 1 /**
 2 * <TestController>
 3 * <详细介绍>
 4 *
 5 * @author lihaitao on 2019/1/2
 6 */
 7 @RestController
 8 @RequestMapping("/test")
 9 public class TestController {
10  
11     @Autowired
12     private IRedisService iRedisService;
13  
14     @GetMapping("/exception-retry-aop") 15 @ExceptionRetry(needThrowExceptions = {NullPointerException.class}, times = 5, 16 catchExceptions = {QueryTimeoutException.class, RedisConnectionFailureException.class}, waitTime = 2 * 1000) 17 public void test() { 18 for (int i = 1; i < 100; i++) { 19 iRedisService.setValue("userName", "jason"); 20 try { 21 Thread.sleep(4000L); 22 } catch (InterruptedException e) { 23  e.printStackTrace(); 24  } 25  } 26  } 27 }
4、测试结果截图
下面是在连接正常的情况下,直接kill掉redis进程,让方法进行重试,可以看到方法重试了5次,最终因为redis没有启动起来还是执行失败了。
 
下面放一张redis在尝试次数未耗尽时,如果重新连接上的话,在下次重试的时候就会重新执行方法
 
 
总结
    异常处理机制的步骤:catch Exception(捕获什么异常,忽略什么异常) ——》 do Something(怎么做,异步?同步?重试还是只是记录留待之后再执行?需要等待否?监控记录?)。
    其他Java Exception Retry实现还有:Guava Retryer、Spring Retry 。实现原理大同小异。 
 
 
转载请说明出处~

猜你喜欢

转载自www.cnblogs.com/leemumu/p/10259559.html