接口重发设计

本文介绍了如何在Java中设计一个可重发的异常处理类`CustomException`,配合自定义注解`CustomAnnotation`,并在业务代码中使用AspectJ进行异常捕获和重试控制。
摘要由CSDN通过智能技术生成

1.设计需要重发的异常处理类

package com.notebook.exception;

public class CustomException extends Exception{

    //设计异常类
    private int errorCode;
    private String msg;

    public CustomException() {
    }

    public CustomException(String message) {
        super(message);
    }


    public CustomException(CustomException customException) {
        super(customException.getMessage());
        this.errorCode = customException.getErrorCode();
    }
    public CustomException(String message, int errorCode) {
        super(message);
        this.errorCode = errorCode;
    }


    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    @Override
    public String toString() {
        return "CustomException{" +
                "message='" + getMessage() + '\'' +
                ", errorCode=" + errorCode +
                '}';
    }


}

2.自定义注解

package com.notebook.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CustomAnnotation {
    int maxcount() default 15 * 1000; // 可以理解为最大重试总时间(毫秒)

    int maxRetries() default 15; // 明确表示最大重试次数
}




package com.notebook.annotationtest;

import com.notebook.annotation.CustomAnnotation;
import com.notebook.exception.CustomException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class YourAspect {

    @Around("@annotation(com.notebook.annotation.CustomAnnotation)")
    public Object aroundCustomAnnotation(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        CustomAnnotation customAnnotation = signature.getMethod().getAnnotation(CustomAnnotation.class);

        int maxRetries = customAnnotation.maxRetries();//最大重试次数
        int maxcount = customAnnotation.maxcount();//重试间隔时间 毫秒
        // 获取调用方法的参数
        boolean falg = true;
        int recount = 0;
        while (falg && recount<maxRetries){
            try {
                falg=false;
                joinPoint.proceed();
            } catch (Exception e) {
                System.out.println(1);
                if (e instanceof CustomException) {
                    CustomException customException = (CustomException) e;
                    if (500 == customException.getErrorCode() || 400 == customException.getErrorCode() || 300 == customException.getErrorCode()) {
                        recount++;
                        falg = true;
                        Thread.sleep(maxcount);
                    }
                }

            }
        }



        return null;
    }
}

3.业务代码

package com.notebook.service.impl;

import com.notebook.annotation.CustomAnnotation;
import com.notebook.exception.CustomException;
import com.notebook.service.TestAnno;
import org.springframework.stereotype.Service;

@Service
public class TestAnnoServiceImpl implements TestAnno {
    int count=0;

    @CustomAnnotation
    @Override
    public String test(String demo) throws Exception {
        try {
            count++;
            System.out.println("计数"+count);
            System.out.println("异常执行次数!参数:"+demo);
        String resultB = bMethod("");
        String resultC = null;

            try {
                resultC = send();
            } catch (CustomException e) {
                throw new CustomException("自定义异常",500);
            }
        } catch (Exception e) {
            if (e instanceof CustomException) {
                CustomException e1 = (CustomException) e;
                throw new CustomException(e1);
            }else {
                throw new Exception();

            }
        }
        String resultD = dMethod();
        return "";
    }


    public String bMethod(String aa) {
        return "result of bMethod";
    }

    public String send() throws CustomException {
        try {
            String aa=null;
            aa.toString();
        } catch (Exception e) {
            throw new CustomException("自定义异常",500);
        }
        return "AA";
    }

    public String dMethod() {
        return "result of dMethod";
    }
}

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值