詳解Spring Retry實(shí)現(xiàn)原理
在前面這篇博客中介紹了Spring Retry的使用,本文通過一個(gè)簡單的例子演示Spring Retry的實(shí)現(xiàn)原理,例子中定義的注解只包含重試次數(shù)屬性,實(shí)際上Spring Retry中注解可設(shè)置屬性要多的多,單純?yōu)榱酥v解原理,所以弄簡單點(diǎn),關(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; }
代理實(shí)現(xiàn)
以Cglib作為代理工具,先來寫個(gè)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; } }
然后是寫個(gè)代理類,使用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(); } }
測試
通過一個(gè)用戶相關(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)行效果如下
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
springboot項(xiàng)目使用Disruptor做內(nèi)部消息隊(duì)列的實(shí)現(xiàn)
本文主要介紹了springboot項(xiàng)目使用Disruptor做內(nèi)部消息隊(duì)列的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例
本篇文章主要介紹了Spring Cloud下OAUTH2注銷的實(shí)現(xiàn)示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03IDEA JeeSite框架httpSession.invalidate()無效問題解決方案
這篇文章主要介紹了IDEA JeeSite框架httpSession.invalidate()無效問題解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09通過代理類實(shí)現(xiàn)java連接數(shù)據(jù)庫(使用dao層操作數(shù)據(jù))實(shí)例分享
java通過代理類實(shí)現(xiàn)數(shù)據(jù)庫DAO操作代碼分享,大家參考使用吧2013-12-12Spring Data Jpa多表查詢返回自定義實(shí)體方式
這篇文章主要介紹了Spring Data Jpa多表查詢返回自定義實(shí)體方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02