ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自定义注解

2021-11-20 20:04:16  阅读:148  来源: 互联网

标签:自定义 joinPoint backoff new date 注解 bizRetry operateCouponsRequest


1 重试注解

(1)定义重试注解

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RetryAnnotation {

    int retryTimes() default 0 ;

    /**
     * 重试策略
     * @return
     */
    MidBackoff backoff() default @MidBackoff();
}

(2)重试切面

@RefreshScope
@Component
@Aspect
@Slf4j
public abstract class RetryAspect {

    @Resource
    private RetryTemplate retryTemplate;

    @Value("${system.config.cluster:normal}")
    private String env;

    @Value("${myj.env.groupId}")
    private String groupId;

    @Value("${myj.env.cluster}")
    private String envConfig;

    @Pointcut("@annotation(com.sr.bizmid.tradeCore.common.annotation.RetryAnnotation)")
    public void pointCut() {
    }

    @Around("pointCut() && @annotation(retryAnnotation)")
    public Object process(ProceedingJoinPoint joinPoint, RetryAnnotation retryAnnotation) throws Throwable {
        AtomicReference<Object> proceed = new AtomicReference<>();
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        Class targetClass = signature.getDeclaringType();
        final Object[] args = joinPoint.getArgs();
        try {
            retryTemplate.execute(context -> {
                try {
                      proceed.set(joinPoint.proceed());
                } catch (Exception e) {
                    log.error("RetryAspect retryError className:{}  method:{} args:{}",targetClass.getName(),name, JSON.toJSONString(args),e);
                    throw e;
                }
                return null;
            });

        } catch (Exception e) {
            log.error("RetryAspect retryFinishError className:{}  method:{} args:{}",targetClass.getName(),name,JSON.toJSONString(args),e);
            //加入重试表重试
            saveRetryRecord(joinPoint, retryAnnotation);
            throw new TRetailSystemException(TradeBaseErrorCode.TRADE_CORE_RETRY_ASPECT_ERROR,e);
        }
        return proceed.get();
    }

    private void saveRetryRecord(ProceedingJoinPoint joinPoint, RetryAnnotation retryAnnotation){
        try {
            Date date = new Date();
            BizRetry bizRetry = new BizRetry();
            bizRetry.setCreateTime(date);
            bizRetry.setUpdateTime(date);
            bizRetry.setOperableTime(date);
            bizRetry.setStatus((byte) 0);
            bizRetry.setIsDeleted(isAbandon() ? (byte) 1 : (byte) 0);//加上开关
            bizRetry.setRetriedCount(0);
            bizRetry.setMethodName(joinPoint.getSignature().getName());
            bizRetry.setClassName(joinPoint.getSignature().getDeclaringType().getName());

            MidBackoff backoff = retryAnnotation.backoff();
            BizRetry.MidBackoff midBackoff = new BizRetry.MidBackoff();
            midBackoff.setCount(backoff.count());
            midBackoff.setDelay(backoff.delay());
            midBackoff.setMultiplier(backoff.multiplier());

            bizRetry.setBackoff(midBackoff);

            int delay = backoff.delay();
            if (backoff.multiplier() > 0d) {
                delay = (int) ((double) delay * backoff.multiplier());
            }
            bizRetry.setOperableTime(add(date, Calendar.SECOND, delay));

            Object[] args = joinPoint.getArgs();
            List<String> argsList = new ArrayList<>(args.length);
            List<String> types = new ArrayList<>();
            Class[] parameterTypes = ((MethodSignature)joinPoint.getSignature()).getMethod().getParameterTypes();

            for (Class clazz : parameterTypes) {
                types.add(clazz.getName());
            }

            for (Object arg : args) {
                argsList.add(JSON.toJSONString(arg));
            }

            // 参数列表类型
            bizRetry.setArgsTypes(JSON.toJSONString(types));
            // 参数
            bizRetry.setArgs(JSON.toJSONString(argsList));

            // 保存重试记录
            saveRetryRecord(bizRetry);
        }catch (Throwable t){
            LoggerUtils.fmtError(log,t,"saveRetryRecord新增重试记录异常");
        }
    }

    /**
     * 返回true拦截,false不拦截
     * @return
     */
    private boolean isAbandon() {
        boolean result;
        try {
            String appId = TsrSpringContextAware.getApplicationContext().getEnvironment().getProperty("tsf_group_id");
            // 匹配过滤的环境
            result = groupId.equals(appId) && envConfig.equals(env);
        } catch (Exception e) {
            result = false;
        }
        return result;
    }

    private Date add(Date date, int calendarField, int amount) {
        if (date == null) {
            throw new IllegalArgumentException("The date must not be null");
        }
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(calendarField, amount);
        return c.getTime();
    }

    public abstract void saveRetryRecord(BizRetry bizRetry);

}



(3)RPC 调用使用重试注解

    @RetryAnnotation(backoff = @MidBackoff(count = 5,multiplier = 1,delay = 30))
    public void writeOFFCoupon(String orderNo, String uid, Long rootSaasId, Long saasId, String userName, List<String> codes){
        OperateCouponsRequest operateCouponsRequest = new OperateCouponsRequest();
        operateCouponsRequest.setRequestId(uid + System.currentTimeMillis());
        operateCouponsRequest.setBizId(rootSaasId);
        operateCouponsRequest.setSubSaasId(saasId);
        operateCouponsRequest.setOperator(userName);
        operateCouponsRequest.setFunc(OperateCouponFuncEnum.WRITE_OFF.getFunc());
        operateCouponsRequest.setCodes(codes);
        operateCouponsRequest.setUserId(uid);
        operateCouponsRequest.setUseSceneId(UseChannelEnum.ONLINE.getChannel().toString());
        operateCouponsRequest.setUseTransactionId(orderNo);
        HttpResult<Boolean> httpResult;
        try {
            log.info("调用核销优惠券请求入参:{}", JSON.toJSONString(operateCouponsRequest));
            httpResult = couponClient.operateCoupons(operateCouponsRequest);
            log.info("调用核销优惠券请求response:{}", JSON.toJSONString(httpResult));
        }catch (Exception e) {
            throw new TRetailRpcResultException(TradeRpcErrorCode.TRADE_CALL_COUPON_ERROR, e);
        }
        if (!httpResult.isSuccess() || httpResult.getData() == null) {
            throw new TRetailRpcResultException(TradeRpcErrorCode.TRADE_CALL_COUPON_ERROR);
        }
    }

标签:自定义,joinPoint,backoff,new,date,注解,bizRetry,operateCouponsRequest
来源: https://blog.csdn.net/wydyd110/article/details/121444474

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有