AOP 结合 guava retryer 实现接口自动重试

2018-03-01 23:54:09   最后更新: 2018-03-01 23:54:09   访问数量:398




上一篇日志中,我们介绍了 Retryer 类,及可以用来方便的创建 Retryer 的强大工具类 RetryerBuilder

guava 重试工具库 -- Retryer

Retryer 创建工具 -- RetryerBuilder

 

本篇日志中,我们通过面向切面结合 guava 的这个强大的工具类,来实现只需要添加一行注解即可的自动重试机制

 

我们的注解旨在通过简单的参数配置常用的重试策略:

@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface TechlogRetryer { long waitMsec() default 0; Class[] retryThrowable() default {}; long maxDelayMsec() default 0; int maxAttempt() default 0; }

 

 

我们以上面的注解为切点构造一个切面,实现对方法的自动处理

@Aspect @Service public class TechlogRetryerAspect { private static final Logger LOGGER = LoggerFactory.getLogger(TechlogRetryerAspect.class); @Around(value = "@annotation(TechlogRetryer)") public Object monitorAround(ProceedingJoinPoint pjp) throws Throwable { Method method; if (pjp.getSignature() instanceof MethodSignature) { MethodSignature signature = (MethodSignature) pjp.getSignature(); method = signature.getMethod(); } else { LOGGER.error("Monitor Annotation not at a method {}", pjp); return null; } TechlogRetryer retryerAnnotation = method.getDeclaredAnnotation(TechlogRetryer.class); if (retryerAnnotation.maxDelayMsec() <= 0 && retryerAnnotation.maxAttempt() <= 1) { return pjp.proceed(); } RetryerBuilder retryer = RetryerBuilder.newBuilder(); if (retryerAnnotation.waitMsec() > 0) { retryer.withWaitStrategy(fixedWait(retryerAnnotation.waitMsec(), TimeUnit.MILLISECONDS)); } if (retryerAnnotation.retryThrowable().length > 0) { for (Class retryThrowable : retryerAnnotation.retryThrowable()) { if (retryThrowable != null && Throwable.class.isAssignableFrom(retryThrowable)) { retryer.retryIfExceptionOfType(retryThrowable); } } } if (retryerAnnotation.maxDelayMsec() > 0) { retryer.withStopStrategy(StopStrategies.stopAfterDelay(retryerAnnotation.maxDelayMsec(), TimeUnit.MILLISECONDS)); } if (retryerAnnotation.maxAttempt() > 0) { retryer.withStopStrategy(StopStrategies.stopAfterAttempt(retryerAnnotation.maxAttempt())); } String retrylog = pjp.getTarget().getClass().getCanonicalName() + "." + method.getName(); return retryer.build().call(() -> { try { LOGGER.info("<TECHLOG_RETRYER>" + retrylog); return pjp.proceed(); } catch (Throwable throwable) { if (throwable instanceof Exception) { throw (Exception) throwable; } else { throw new Exception(throwable); } } }); } }

 

 

对于我们希望进行重试的方法,我们只需要加上注解并配置合适的参数即可

@TechlogRetryer(retryThrowable = NeedCatchException.class, maxAttempt = 2) public Map<Long, SpuQuery> getPoiSpuListByIds(Long poiId, List<Long> poiSpuIds) { try { SpuListResponse skuListResponse = productService.getSpuByIds(poiId, poiSpuIds); if(skuListResponse == null || skuListResponse.getData() == null || skuListResponse.getData().isEmpty()) { return null; } Map<Long, SpuQuery> result = new HashMap<>(); for (SpuQuery spuQuery : skuListResponse.getData()) { result.put(spuQuery.getSpuId(), spuQuery); } return result; } catch (TException e) { throw new NeedCatchException(e, null); } }

 

 






技术帖      技术分享      spring      aop      注解      annotation      google      guava      retryer      重试     


1#111213133: (回复)2018-03-16 18:25:43

1

京ICP备15018585号