溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Spring Retry實(shí)現(xiàn)原理的示例分析

發(fā)布時間:2021-08-06 11:22:48 來源:億速云 閱讀:139 作者:小新 欄目:編程語言

這篇文章主要為大家展示了“Spring Retry實(shí)現(xiàn)原理的示例分析”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Spring Retry實(shí)現(xiàn)原理的示例分析”這篇文章吧。

注解定義

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;

}

代理實(shí)現(xiàn)

以Cglib作為代理工具,先來寫個Callback實(shí)現(xiàn),這也是重試的實(shí)現(xiàn)的核心邏輯

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{

  //記錄重試次數(shù)
  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("已達(dá)最大重試次數(shù):" + maxAttemps + ",不再重試!");
        }else{
          System.out.println("調(diào)用" + method.getName() + "方法異常,開始第" + times +"次重試。。。");
          //注意這里不是invokeSuper方法,invokeSuper會退出當(dāng)前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();
  }
}

 測試

通過一個用戶相關(guān)的業(yè)務(wù)方法來測試上面的代碼

接口定義:

package facade;

/**
 * Created by Jack.wu on 2016/9/26.
 */
public interface UserFacade {

  void add() throws Exception;

  void query() throws Exception;
}

接口實(shí)現(xiàn):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方法不添加重試注解,程序異常結(jié)束,query方法添加重試注解,設(shè)置重試3次,運(yùn)行效果如下

Spring Retry實(shí)現(xiàn)原理的示例分析

以上是“Spring Retry實(shí)現(xiàn)原理的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI