欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring Retry实现原理

程序员文章站 2023-11-04 20:38:22
在中介绍了spring retry的使用,本文通过一个简单的例子演示spring retry的实现原理,例子中定义的注解只包含重试次数属性,实际上spring retry中...

在中介绍了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次,运行效果如下

详解Spring Retry实现原理

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