原创

【编程系列】AOP 结合guava retryer实现接口自动重试

概述

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;
}

 

切面

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

@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);
}
}

 

正文到此结束
本文目录