详解Spring Retry实现原理

所属分类: 软件编程 / java 阅读数: 34
收藏 0 赞 0 分享

前面这篇博客中介绍了Spring Retry的使用,本文通过一个简单的例子演示Spring Retry的实现原理,例子中定义的注解只包含重试次数属性,实际上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次,运行效果如下

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

java实现背单词程序

这篇文章主要为大家详细介绍了java实现背单词程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java实现单词查询小程序

这篇文章主要为大家详细介绍了java实现单词查询小程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java程序开发环境配置图文教程

这篇文章主要为大家详细介绍了Java程序开发环境配置图文教程,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解ssh框架原理及流程

在本文中小编给大家整理的是关于ssh框架原理及流程的相关知识点内容,有此需要的朋友们可以学习下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

详解springmvc常用5种注解

在本篇里我们给大家总结了关于springmvc常用5种注解相关知识点以及实例代码,需要的朋友们参考下。
收藏 0 赞 0 分享

Java实现弹窗效果的基本操作(2)

这篇文章主要为大家详细介绍了Java实现弹窗效果的基本操作第二篇,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Spring Boot假死诊断实战记录

这篇文章主要给大家介绍了关于Spring Boot假死诊断的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享

Java计时新姿势StopWatch详解

这篇文章主要介绍了Java计时新姿势StopWatch,最近公司来了个大佬,从他那里学到不少东西,其中一个就是计时的新姿势「StopWatch」,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现点击按钮弹出新窗体功能

这篇文章主要为大家详细介绍了java实现点击按钮弹出新窗体功能,旧窗体不进行操作,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多