詳解Spring Retry實現(xiàn)原理
在前面這篇博客中介紹了Spring Retry的使用,本文通過一個簡單的例子演示Spring Retry的實現(xiàn)原理,例子中定義的注解只包含重試次數(shù)屬性,實際上Spring Retry中注解可設置屬性要多的多,單純?yōu)榱酥v解原理,所以弄簡單點,關于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;
}
代理實現(xiàn)
以Cglib作為代理工具,先來寫個Callback實現(xiàn),這也是重試的實現(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("已達最大重試次數(shù):" + maxAttemps + ",不再重試!");
}else{
System.out.println("調(diào)用" + 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();
}
}
測試
通過一個用戶相關的業(yè)務方法來測試上面的代碼
接口定義:
package facade;
/**
* Created by Jack.wu on 2016/9/26.
*/
public interface UserFacade {
void add() throws Exception;
void query() throws Exception;
}
接口實現(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方法不添加重試注解,程序異常結束,query方法添加重試注解,設置重試3次,運行效果如下

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
配置java環(huán)境變量(linux mac windows7)
本文給大家詳細總結介紹了Linux、MAC以及Windows下配置java環(huán)境變量的方法,非常的細致全面,有需要的小伙伴可以參考下2015-11-11
詳解SpringCloud mysql實現(xiàn)配置中心
這篇文章主要介紹了詳解SpringCloud mysql實現(xiàn)配置中心,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-09-09
Java 中的Printstream介紹_動力節(jié)點Java學院整理
PrintStream 是打印輸出流,它繼承于FilterOutputStream。接下來通過本文給大家介紹Java 中的Printstream,需要的朋友參考下吧2017-05-05
java使用BeanUtils.copyProperties踩坑經(jīng)歷
最近在做個項目,踩了個坑特此記錄一下,本文主要介紹了使用BeanUtils.copyProperties踩坑經(jīng)歷,需要的朋友們下面隨著小編來一起學習學習吧2021-05-05
Spring @Cacheable redis異常不影響正常業(yè)務方案
這篇文章主要介紹了Spring @Cacheable redis異常不影響正常業(yè)務方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02

