上一篇文章中介绍了Retryer 强大的工具库,以及创建方式。重试工具库一: Guava-Retrying。
本篇文章我们通过面向切面结合 guava 的这个强大的工具类,来实现只需要添加一行注解即可的自动重试机制。
注解:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Retryer {
// 指定时间间隔
long waitMsec() default 0;
// 重试的异常
Class[] retryThrowable() default {};
//在一定超时后停止
long maxDelayMsec() default 0;
// 最大重试次数
int maxAttempt() default 0;
}
AOP切面:
@Aspect
@Component
@Slf4j
public class RetryerAspect {
@Around(value = "@annotation(Retryer)")
public Object monitorAround(ProceedingJoinPoint pjp) throws Throwable {
Method method;
if (pjp.getSignature() instanceof MethodSignature) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
method = signature.getMethod();
} else {
log.error("Monitor Annotation not at a method {}", pjp);
return null;
}
Retryer retryerAnnotation = method.getDeclaredAnnotation(Retryer.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 {
log.info("<RETRYER>" + retrylog);
return pjp.proceed();
} catch (Throwable throwable) {
if (throwable instanceof Exception) {
throw (Exception) throwable;
} else {
throw new Exception(throwable);
}
}
});
}
}
使用例子:
@Retryer(retryThrowable = Exception.class, maxAttempt = 3,waitMsec = 2000)
public Object getTest(String hello) {
Map<String,String> result = null;
try {
System.out.println(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
result.put("name",hello);
} catch (Exception e) {
throw e;
}
return "";
}