在前面這篇博客中介紹了Spring Retry的使用,本文通過一個簡單的例子演示Spring Retry的實現原理,例子中定義的註解只包含重試次數屬性,實際上Spring Retry中註解可設置屬性要多的多,單純爲了講解原理,所以弄簡單點,關於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;
}
代理實現
以Cglib作爲代理工具,先來寫個Callback實現,這也是重試的實現的核心邏輯
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{
//記錄重試次數
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("已達最大重試次數:" + maxAttemps + ",不再重試!");
}else{
System.out.println("調用" + 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();
}
}
測試
通過一個用戶相關的業務方法來測試上面的代碼
接口定義:
package facade;
/**
* Created by Jack.wu on 2016/9/26.
*/
public interface UserFacade {
void add() throws Exception;
void query() throws Exception;
}
接口實現:
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次,運行效果如下