h5网站建设机构,网站建设是什么,销售公司怎么做网站,网站备案 多久Java 代码重试实现方式 一.方法内直接自动重试二.静态代理方式1.启动类2.接口3.实现4.静态代理5.单元测试类 三.JDK 动态代理1.代理类2.单元测试 四.CGLIB 动态代理1.动态代理类2.单元测试 五.手动 AOP1.自定义注解2.重试注解切面3.测试类4.单元测试方法 六.Spring Retry1.测试… Java 代码重试实现方式 一.方法内直接自动重试二.静态代理方式1.启动类2.接口3.实现4.静态代理5.单元测试类 三.JDK 动态代理1.代理类2.单元测试 四.CGLIB 动态代理1.动态代理类2.单元测试 五.手动 AOP1.自定义注解2.重试注解切面3.测试类4.单元测试方法 六.Spring Retry1.测试类2.单元测试类3.单元测试方法 七.Guava Retry1.测试类2.单元测试 依赖包
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersiongroupIdorg.example/groupIdartifactIdlearn-demo/artifactIdversion1.0-SNAPSHOT/versionpropertiesmaven.compiler.source20/maven.compiler.sourcemaven.compiler.target20/maven.compiler.targetproject.build.sourceEncodingUTF-8/project.build.sourceEncodingspring.version3.1.2/spring.version/propertiesdependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdversion1.18.28/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion2.0.32/version/dependencydependencygroupIdorg.aspectj/groupIdartifactIdaspectjweaver/artifactIdversion1.9.19/version/dependencydependencygroupIdcglib/groupIdartifactIdcglib/artifactIdversion3.3.0/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.13.2/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion6.0.11/version/dependency/dependencies/project一.方法内直接自动重试 public static void main(String[] args) {autoRetry(3);}/*** 自动重试执行总次数包含于重试次数内* 优点:实现简单* 缺点:复用性差代码侵入* param retryTimes*/public static void autoRetry(int retryTimes){int times 0;do {try {int i 3/0;} catch (Exception e) {times;System.out.println(第 times 次失败);}} while (times retryTimes);}
二.静态代理方式
1.启动类
package org.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** author Administrator*/
SpringBootApplication
public class LearnApp {public static void main(String[] args) {SpringApplication.run(LearnApp.class,args);}
}2.接口
package org.example.service;/*** description* author zhuwd moon* version 1.0*/
public interface LearnService {/*** 自动重试*/void autoRetry();
}
3.实现
package org.example.service.impl;import org.example.service.LearnService;
import org.springframework.stereotype.Service;/*** author zhuwd moon* Description* create 2023-08-08 1:05*/
Service
public class LearnServiceImpl implements LearnService {Overridepublic void autoRetry() {throw new RuntimeException();}
}
4.静态代理
package org.example.service.impl;import org.example.service.LearnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;/*** author zhuwd moon* Description* create 2023-08-08 1:06*/
Service
public class LearnServiceProxyImpl implements LearnService {Autowiredprivate LearnServiceImpl learnService;private static final int RETRY_TIMES 3;Overridepublic void autoRetry() {int times 0;do {try {learnService.autoRetry();} catch (Exception e) {times;System.out.println(第 times 次失败);}} while (times RETRY_TIMES);}
}
5.单元测试类
import org.example.LearnApp;
import org.example.service.impl.LearnServiceProxyImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredLearnServiceProxyImpl learnServiceProxy;Testpublic void test(){learnServiceProxy.autoRetry();}} 三.JDK 动态代理
1.代理类
package org.example.proxy;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;/*** author zhuwd moon* Description 实现方式较优雅但是被代理的类必须实现某个接口才能操作* create 2023-08-08 21:14*/
public class RetryInvocationHandler implements InvocationHandler {private static final int RETRY_TIMES 3;/*** 目标对象*/private final Object target;/*** 有参构造* param target*/public RetryInvocationHandler(Object target) {this.target target;}Overridepublic Object invoke(Object proxy, Method method, Object[] args) throws Throwable {int times 0;do {try {return method.invoke(target,args);} catch (Exception e) {times;System.out.println(第 times 次失败);}} while (times RETRY_TIMES);return null;}
}
2.单元测试
import org.example.LearnApp;
import org.example.proxy.RetryInvocationHandler;
import org.example.service.LearnService;
import org.example.service.impl.LearnServiceImpl;
import org.example.service.impl.LearnServiceProxyImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Proxy;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredLearnServiceImpl learnService;Testpublic void testJdk(){InvocationHandler handler new RetryInvocationHandler(learnService);LearnService service (LearnService) Proxy.newProxyInstance(handler.getClass().getClassLoader(), learnService.getClass().getInterfaces(),handler);service.autoRetry();}
} 四.CGLIB 动态代理
注意依赖选择 cglib 包下的不要用 spring 下的1.动态代理类
package org.example.proxy;import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;import java.lang.reflect.Method;/*** author zhuwd moon* Description* create 2023-08-08 21:25*/
public class CGLibRetryProxyHandler implements MethodInterceptor {private static final int RETRY_TIMES 3;private Object target;Overridepublic Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {int times 0;do {try {return method.invoke(target,objects);} catch (Exception e) {times;System.out.println(第 times 次失败);}} while (times RETRY_TIMES);return null;}/*** 生产代理类* param target* return*/public Object getProxy(Object target){this.target target;Enhancer enhancer new Enhancer();enhancer.setSuperclass(this.target.getClass());enhancer.setCallback(this);enhancer.setClassLoader(this.target.getClass().getClassLoader());Object proxy enhancer.create();return proxy;}}
2.单元测试
import org.example.LearnApp;
import org.example.proxy.CGLibRetryProxyHandler;
import org.example.service.LearnService;
import org.example.service.impl.LearnServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredLearnServiceImpl learnService;Testpublic void testCglib(){CGLibRetryProxyHandler handler new CGLibRetryProxyHandler();LearnService service (LearnService) handler.getProxy(learnService);service.autoRetry();}
}
如果是JDK17及以上版本用IDEA调试需要增加VM配置--add-opens java.base/java.langALL-UNNAMED五.手动 AOP
利用自定义注解实现重试AOP逻辑1.自定义注解
package org.example.anno;import java.lang.annotation.*;/*** author zhuwd moon* Description* create 2023-08-08 22:50*/
Documented
Target(ElementType.METHOD)
Retention(RetentionPolicy.RUNTIME)
public interface RetryAnno {/*** 重试次数* return*/int retryTimes() default 3;/*** 重试间隔s* return*/int retryInterval() default 3;}
2.重试注解切面
package org.example.config;import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.example.anno.RetryAnno;
import org.springframework.stereotype.Component;import java.util.concurrent.TimeUnit;/*** author zhuwd moon* Description* create 2023-08-08 22:52*/
Aspect
Component
public class RetryAspect {Pointcut(annotation(org.example.anno.RetryAnno))private void retryCall(){};Around(retryCall())public Object retry(ProceedingJoinPoint joinPoint) throws Throwable{MethodSignature signature (MethodSignature) joinPoint.getSignature();RetryAnno retryAnno signature.getMethod().getAnnotation(RetryAnno.class);int retryTimes retryAnno.retryTimes();int retryInterval retryAnno.retryInterval();int times 0;do {try {return joinPoint.proceed();} catch (Exception e) {times;System.out.println(System.currentTimeMillis() 第 times 次失败);TimeUnit.SECONDS.sleep(retryInterval);}} while (times retryTimes);return null;}}
3.测试类
package org.example.config;import org.example.anno.RetryAnno;
import org.springframework.stereotype.Component;/*** author zhuwd moon* Description* create 2023-08-08 23:02*/
Component
public class LearnRetry {RetryAnno(retryTimes 3,retryInterval 3)public void retry(){throw new RuntimeException();}}
4.单元测试方法
import org.example.LearnApp;
import org.example.config.LearnRetry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredLearnRetry learnRetry;Testpublic void testAopRetry(){learnRetry.retry();}
} 六.Spring Retry
dependencygroupIdorg.springframework.retry/groupIdartifactIdspring-retry/artifactIdversion2.0.2/version
/dependency1.测试类
package org.example.config;import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.EnableRetry;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Component;/*** author zhuwd moon* Description* create 2023-08-08 23:10*/
Component
EnableRetry
public class SpringRetry {/*** maxAttempts 最大重试次数* backoff.delay 重试延迟* backoff.multiplier 延迟倍数即第一次间隔 2S 第二次将间隔 4秒*/Retryable(maxAttempts 3,backoff Backoff(delay 2000,multiplier 2))public void retry(){System.out.println(System.currentTimeMillis() 重试 ...);throw new RuntimeException();}/*** Recover 注解必须和 Retryable 在同一个类且返回值一致不能抛出额外异常** param e*/Recoverpublic void recover(RuntimeException e){System.out.println(已达最大重试次数);}}
2.单元测试类
import org.example.LearnApp;
import org.example.config.SpringRetry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredSpringRetry springRetry;Testpublic void testSpringRetry(){springRetry.retry();}
} 3.单元测试方法
import org.example.LearnApp;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.retry.backoff.FixedBackOffPolicy;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.HashMap;
import java.util.Map;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {public void retry(){System.out.println(System.currentTimeMillis() 重试 ...);throw new RuntimeException();}Testpublic void testSpringRetryMethod(){RetryTemplate template new RetryTemplate();/*** 重试策略*/MapClass? extends Throwable,Boolean map new HashMap();map.put(RuntimeException.class,true);SimpleRetryPolicy retryPolicy new SimpleRetryPolicy(3,map);/*** 重试回退策略*/FixedBackOffPolicy backOffPolicy new FixedBackOffPolicy();backOffPolicy.setBackOffPeriod(2000);template.setRetryPolicy(retryPolicy);template.setBackOffPolicy(backOffPolicy);Boolean execute template.execute(retryContext - {retry();return true;},retryContext - {System.out.println(已达最大重试次数);return false;});System.out.println(调用结果 execute);}
} 七.Guava Retry
dependencygroupIdcom.github.rholder/groupIdartifactIdguava-retrying/artifactIdversion2.0.0/version
/dependency1.测试类
package org.example.config;import org.springframework.stereotype.Component;/*** author zhuwd moon* Description 10S 后返回正确值* create 2023-08-08 23:49*/
Component
public class GuavaRetry {private static long init_time System.currentTimeMillis() 1000 * 10;public int retry(){if (System.currentTimeMillis() init_time){return 0;}return -1;}}
2.单元测试
import com.github.rholder.retry.*;
import org.example.LearnApp;
import org.example.config.GuavaRetry;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;/*** author zhuwd moon* Description* create 2023-08-08 1:11*/
RunWith(SpringJUnit4ClassRunner.class)
ContextConfiguration(classes LearnApp.class)
public class LearnTest {AutowiredGuavaRetry guavaRetry;Testpublic void testGuavaRetry(){RetryerInteger retryer RetryerBuilder.IntegernewBuilder()//根据异常重试.retryIfException()//根据结果重试.retryIfResult(result- !Objects.equals(result,0))//重试策略.withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS))//停止策略.withStopStrategy(StopStrategies.stopAfterAttempt(300))//监听重试进度.withRetryListener(new RetryListener() {Overridepublic V void onRetry(AttemptV attempt) {System.out.println(System.currentTimeMillis() 第 attempt.getAttemptNumber() 次失败);}}).build();try {int result retryer.call(()-guavaRetry.retry());System.out.println(调用结果 result);} catch (ExecutionException e) {throw new RuntimeException(e);} catch (RetryException e) {throw new RuntimeException(e);}}}