温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Spring Retry实现原理的示例分析

发布时间:2021-08-06 11:22:48 来源:亿速云 阅读:169 作者:小新 栏目:编程语言

这篇文章主要为大家展示了“Spring Retry实现原理的示例分析”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“Spring Retry实现原理的示例分析”这篇文章吧。

注解定义

package retry.annotation; import java.lang.annotation.*; /**  * Created by Jack.wu on 2016/9/30.  */ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface Retryable {   int maxAttemps() default 0; }

代理实现

以Cglib作为代理工具,先来写个Callback实现,这也是重试的实现的核心逻辑

package retry.interceptor; import net.sf.cglib.proxy.MethodInterceptor; import net.sf.cglib.proxy.MethodProxy; import retry.annotation.Retryable; import java.lang.reflect.Method; /**  * Created by Jack.wu on 2016/9/30.  */ public class AnnotationAwareRetryOperationsInterceptor implements MethodInterceptor{   //记录重试次数   private int times = 0;   @Override   public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {     //获取拦截的方法中的Retryable注解     Retryable retryable = method.getAnnotation(Retryable.class);     if(retryable == null){       return proxy.invokeSuper(obj,args);     }else{ //有Retryable注解,加入异常重试逻辑       int maxAttemps = retryable.maxAttemps();       try {         return proxy.invokeSuper(obj,args);       } catch (Throwable e) {         if(times++ == maxAttemps){           System.out.println("已达最大重试次数:" + maxAttemps + ",不再重试!");         }else{           System.out.println("调用" + method.getName() + "方法异常,开始第" + times +"次重试。。。");           //注意这里不是invokeSuper方法,invokeSuper会退出当前interceptor的处理           proxy.invoke(obj,args);         }       }     }     return null;   } }

然后是写个代理类,使用AnnotationAwareRetryOperationsInterceptor作为拦截器

package retry.core; import net.sf.cglib.proxy.Enhancer; import retry.interceptor.AnnotationAwareRetryOperationsInterceptor; /**  * Created by Jack.wu on 2016/9/30.  */ public class SpringRetryProxy {   public Object newProxyInstance(Object target){     Enhancer enhancer = new Enhancer();     enhancer.setSuperclass(target.getClass());     enhancer.setCallback(new AnnotationAwareRetryOperationsInterceptor());     return enhancer.create();   } }

 测试

通过一个用户相关的业务方法来测试上面的代码

接口定义:

package facade; /**  * Created by Jack.wu on 2016/9/26.  */ public interface UserFacade {   void add() throws Exception;   void query() throws Exception; }

接口实现:package facade.impl;

import facade.UserFacade; import retry.annotation.Retryable; /**  * Created by Jack.wu on 2016/9/26.  */ public class UserFacadeImpl implements UserFacade {   @Override   public void add() throws Exception {     System.out.println("添加用户。。。");     throw new RuntimeException();   }   @Override   @Retryable(maxAttemps = 3)   public void query() {     System.out.println("查询用户。。。");     throw new RuntimeException();   } }

测试:

public class Main {   public static void main(String[] args) throws Exception{     UserFacadeImpl user = new UserFacadeImpl();     //SpringRetry代理测试     SpringRetryProxy springRetryProxy = new SpringRetryProxy();     UserFacade u = (UserFacade)springRetryProxy.newProxyInstance(user);     //u.add();//失败不重试     u.query();//失败重试   } }

add方法不添加重试注解,程序异常结束,query方法添加重试注解,设置重试3次,运行效果如下

Spring Retry实现原理的示例分析

以上是“Spring Retry实现原理的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI