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

}

}

 

 




 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值