這篇文章主要為大家展示了“Spring Retry實(shí)現(xiàn)原理的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“Spring Retry實(shí)現(xiàn)原理的示例分析”這篇文章吧。
我們注重客戶提出的每個(gè)要求,我們充分考慮每一個(gè)細(xì)節(jié),我們積極的做好成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)服務(wù),我們努力開(kāi)拓更好的視野,通過(guò)不懈的努力,創(chuàng)新互聯(lián)贏得了業(yè)內(nèi)的良好聲譽(yù),這一切,也不斷的激勵(lì)著我們更好的服務(wù)客戶。 主要業(yè)務(wù):網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)站設(shè)計(jì),重慶小程序開(kāi)發(fā),網(wǎng)站開(kāi)發(fā),技術(shù)開(kāi)發(fā)實(shí)力,DIV+CSS,PHP及ASP,ASP.Net,SQL數(shù)據(jù)庫(kù)的技術(shù)開(kāi)發(fā)工程師。
注解定義
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作為代理工具,先來(lái)寫個(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() + "方法異常,開(kāi)始第" + times +"次重試。。。");
//注意這里不是invokeSuper方法,invokeSuper會(huì)退出當(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();
}
}測(cè)試
通過(guò)一個(gè)用戶相關(guān)的業(yè)務(wù)方法來(lái)測(cè)試上面的代碼
接口定義:
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();
}
}測(cè)試:
public class Main {
public static void main(String[] args) throws Exception{
UserFacadeImpl user = new UserFacadeImpl();
//SpringRetry代理測(cè)試
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)原理的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁(yè)標(biāo)題:SpringRetry實(shí)現(xiàn)原理的示例分析
本文路徑:http://www.chinadenli.net/article30/ihooso.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、、用戶體驗(yàn)、網(wǎng)站設(shè)計(jì)公司、標(biāo)簽優(yōu)化、全網(wǎng)營(yíng)銷推廣
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)