有点复杂,在后续的章节,将会对其中涉及到的知识点,再分章节进行说明。

1.程序结构

  

2.@Retryable

package com.jun.web.annotation.theory;

import java.lang.annotation.*;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Retryable {
int maxAttemps() default 0;
}

3.RetryService

package com.jun.web.annotation.theory;

public interface RetryServcie {
void testRetry();
}

4.RetryServiceImpl

package com.jun.web.annotation.theory;

public class RetryServcieImpl implements RetryServcie {
private int count=5;
@Override
@Retryable(maxAttemps = 5)
public void testRetry() {
System.out.println("这是第"+count+"执行方法");
throw new RuntimeException();
}
}

5.拦截器

MethodInterceptor接口被用来拦截指定的方法,对方法进行增强
具体的是哪个方法,将会通过Enhancer说明
package com.jun.web.annotation.theory;

import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy; import java.lang.reflect.Method; //MethodInterceptor接口被用来拦截指定的方法,对方法进行增强
public class AnnotationRetryInterceptor implements MethodInterceptor {
private int times=0;
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
//obj是代理后的子类 ,method是调用方法 ,args是方法入参 , proxy是MethodProxy代理对象
//获取拦截方法中的注解
Retryable retryable = method.getAnnotation(Retryable.class);
if(retryable==null){
return methodProxy.invokeSuper(o, objects);
}else{
int maxAttemps = retryable.maxAttemps();
try {
return methodProxy.invokeSuper(o,objects);
}catch (Throwable t){
if (times++ == maxAttemps){
System.out.println("已经达到最大值:"+times);
}else {
System.out.println("调用"+method.getName()+"方法异常,开始第"+times+"次重试");
methodProxy.invoke(o,objects);
}
}
}
return null;
}
}

6.代理

package com.jun.web.annotation.theory;

import org.springframework.cglib.proxy.Enhancer;

public class RetryProxy {
public Object newProxyInstance(Object target){
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(target.getClass());
enhancer.setCallback(new AnnotationRetryInterceptor());
return enhancer.create();
}
}

7.测试

package com.jun.web.annotation.theory;

public class RetryHandler {
public static void main(String[] args) {
RetryServcieImpl retryServcieImpl = new RetryServcieImpl();
RetryProxy retryProxy = new RetryProxy();
//
RetryServcie retryServcie = (RetryServcie) retryProxy.newProxyInstance(retryServcieImpl);
retryServcie.testRetry();
}
}

8.效果

Connected to the target VM, address: '127.0.0.1:59161', transport: 'socket'
这是第5执行方法
调用testRetry方法异常,开始第1次重试
这是第5执行方法
调用testRetry方法异常,开始第2次重试
这是第5执行方法
调用testRetry方法异常,开始第3次重试
这是第5执行方法
调用testRetry方法异常,开始第4次重试
这是第5执行方法
调用testRetry方法异常,开始第5次重试
这是第5执行方法
已经达到最大值:6
Disconnected from the target VM, address: '127.0.0.1:59161', transport: 'socket'

最新文章

  1. python调取C/C++的dll生成方法
  2. python之路-Day5
  3. 第57讲:Scala中Dependency Injection实战详解
  4. 如果我可以重新学习iOS开发(转)
  5. 如何选择linux 版本
  6. 通过javascript完成分页查询功能
  7. kettle_为子server创建carte服务
  8. 基于 Apache Mahout 构建社会化推荐引擎
  9. chrome 开发人员工具
  10. 基于Ubuntu的LNMP环境搭建
  11. ●CodeForce 293E Close Vertices
  12. Java-GenricServlet
  13. java_oop_接口
  14. 你不可不知的Java引用类型之——WeakReference源码详解
  15. mac下用命令行解压文件
  16. 用stringr包处理字符串
  17. Java使用HTTP编程模拟多参数多文件表单信息的请求与处理
  18. Java面向对象六大原则
  19. php 百家姓
  20. ASP.NET Visual Studio2010 发布Web网站问题详解

热门文章

  1. 如何在Win7电脑上增加新磁盘分区?
  2. Docker02-重要概念
  3. linux中container_of
  4. Deployment
  5. 卓越Code第一次作业
  6. beta版本——第二次冲刺
  7. selenium+python自动化99-清空输入框clear()失效问题解决
  8. 最小圆覆盖(洛谷 P1742 增量法)
  9. Spring源码窥探之:声明式事务
  10. SPI总线协议理解