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