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

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'

012 spring retry重试原理的解析的更多相关文章

  1. 自己动手实践 spring retry 重试框架

    前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...

  2. Spring Retry 重试

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...

  3. spring boot 启动原理详细解析

    我们开发任何一个Spring Boot项目,都会用到如下的启动类 1 @SpringBootApplication 2 public class Application { 3 public stat ...

  4. spring retry 重试机制完整例子

    public static Boolean vpmsRetryCoupon(final String userId) { // 构建重试模板实例 RetryTemplate retryTemplate ...

  5. spring容器IOC原理解析

    原理简单介绍: Spring容器的原理,其实就是通过解析xml文件,或取到用户配置的bean,然后通过反射将这些bean挨个放到集合中,然后对外提供一个getBean()方法,以便我们获得这些bean ...

  6. Spring异常重试框架Spring Retry

    Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...

  7. Spring IOC设计原理解析:本文乃学习整理参考而来

    Spring IOC设计原理解析:本文乃学习整理参考而来 一. 什么是Ioc/DI? 二. Spring IOC体系结构 (1) BeanFactory (2) BeanDefinition 三. I ...

  8. Spring Boot启动原理解析

    Spring Boot启动原理解析http://www.cnblogs.com/moonandstar08/p/6550758.html 前言 前面几章我们见识了SpringBoot为我们做的自动配置 ...

  9. 这一次搞懂Spring自定义标签以及注解解析原理

    前言 在上一篇文章中分析了Spring是如何解析默认标签的,并封装为BeanDefinition注册到缓存中,这一篇就来看看对于像context这种自定义标签是如何解析的.同时我们常用的注解如:@Se ...

随机推荐

  1. kubernetes资源预留---转发

    下面内容还处于测试阶段,生产上是否能保证集群稳定暂时还不清楚.

  2. 使用Cloudera Manager添加Sentry服务

    使用Cloudera Manager添加Sentry服务 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.通过CM添加Sentry服务 1>.点击添加服务进入CM服务安装向 ...

  3. tensorflow tfrecoder read write

    # write in tfrecord import tensorflow as tf import os os.environ[' FLAGS = tf.app.flags.FLAGS tf.app ...

  4. sq分页

    create proc RowNumber @pageindex int,@pagesize int AS BEGIN select * from (select ROW_NUMBER() OVER( ...

  5. spring cloud turbine 监控不到其它机器上的hystrix.stream 的解决方法 指定监控ip

    turbine多台机器熔断聚合的时候  turbine控制台一直寻找的是localhost下的监控熔断数据. c.n.t.monitor.instance.InstanceMonitor   : Ur ...

  6. 基于Asp.net core + EF + Sqlite 5分钟快速上手一个小项目

    虽然该方法不会用在实际开发中,但该过程对于初学者还是非常友好的,真应了麻雀虽小,五脏俱全这句话了.好了不多废话了,直接开始!! 1.建立一个名为test的Asp.net core web应用程序 这一 ...

  7. Alpha冲刺(9/10)——2019.5.2

    所属课程 软件工程1916|W(福州大学) 作业要求 Alpha冲刺(9/10)--2019.5.2 团队名称 待就业六人组 1.团队信息 团队名称:待就业六人组 团队描述:同舟共济扬帆起,乘风破浪万 ...

  8. Convert.ToString(null) => null

    { string str0 = Convert.ToString(null); Console.WriteLine("0,{0}", str0); if (str0==" ...

  9. 09-Flutter移动电商实战-移动商城数据请求实战

    1.URL接口管理文件建立 第一步需要在建立一个URL的管理文件,因为课程的接口会一直进行变化,所以单独拿出来会非常方便变化接口.当然工作中的URL管理也是需要这样配置的,以为我们会不断的切换好几个服 ...

  10. [PWA] Storage information for PWA application

    Be careful with the storage use cases, free the storage when it is necessary.