詳解Spring Retry實現(xiàn)原理
在前面這篇博客中介紹了Spring Retry的使用,本文通過一個簡單的例子演示Spring Retry的實現(xiàn)原理,例子中定義的注解只包含重試次數(shù)屬性,實際上Spring Retry中注解可設(shè)置屬性要多的多,單純?yōu)榱酥v解原理,所以弄簡單點,關(guān)于Spring Retry可查閱相關(guā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;
}
代理實現(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();
}
}
測試
通過一個用戶相關(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;
}
接口實現(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次,運行效果如下

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項目使用Disruptor做內(nèi)部消息隊列的實現(xiàn)
本文主要介紹了springboot項目使用Disruptor做內(nèi)部消息隊列的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-07-07
Spring Cloud下OAUTH2注銷的實現(xiàn)示例
本篇文章主要介紹了Spring Cloud下OAUTH2注銷的實現(xiàn)示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-03-03
IDEA JeeSite框架httpSession.invalidate()無效問題解決方案
這篇文章主要介紹了IDEA JeeSite框架httpSession.invalidate()無效問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-09-09
通過代理類實現(xiàn)java連接數(shù)據(jù)庫(使用dao層操作數(shù)據(jù))實例分享
java通過代理類實現(xiàn)數(shù)據(jù)庫DAO操作代碼分享,大家參考使用吧2013-12-12

