Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包。

快速集成的代码样例:

@Configuration
@EnableRetry
public class Application { @Bean
public Service service() {
return new Service();
} } @Service
class Service {
@Retryable(RemoteAccessException.class)
public service() {
// ... do something
}
}

下面是基于Spring Boot项目的集成步骤:

POM:

        <!-- Spring Retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
</dependency>

Service:

package com.jsoft.springboottest.springboottest1;

import java.time.LocalTime;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service; @Service
public class RemoteService { private final static Logger logger = LoggerFactory.getLogger(RemoteService.class); @Retryable(value = { RemoteAccessException.class }, maxAttempts = 3, backoff = @Backoff(delay = 5000l, multiplier = 1))
public void call() throws Exception {
logger.info(LocalTime.now()+" do something...");
throw new RemoteAccessException("RPC调用异常");
} @Recover
public void recover(RemoteAccessException e) {
logger.info(e.getMessage());
}
}

@Retryable注解

被注解的方法发生异常时会重试

  • value:指定发生的异常进行重试
  • include:和value一样,默认空,当exclude也为空时,所有异常都重试
  • exclude:指定异常不重试,默认空,当include也为空时,所有异常都重试
  • maxAttemps:重试次数,默认3
  • backoff:重试补偿机制,默认没有

@Backoff注解

  • delay:指定延迟后重试
  • multiplier:指定延迟的倍数,比如delay=5000l,multiplier=2时,第一次重试为5秒后,第二次为10秒,第三次为20秒

@Recover

当重试到达指定次数时,被注解的方法将被回调,可以在该方法中进行日志处理。需要注意的是发生的异常和入参类型一致时才会回调。

Controller:

package com.jsoft.springboottest.springboottest1.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.jsoft.springboottest.springboottest1.RemoteService; @RestController
public class TestController { @Autowired
private RemoteService remoteService; @RequestMapping("/show")
public String show(){
try {
remoteService.call();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
}
return "Hello World";
}
}

App:

package com.jsoft.springboottest.springboottest1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.retry.annotation.EnableRetry; /**
* Hello world!
*
*/
@SpringBootApplication
@EnableRetry
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}

效果:

说明:

1、使用了@Retryable的方法不能在本类被调用,不然重试机制不会生效。也就是要标记为@Service,然后在其它类使用@Autowired注入或者@Bean去实例才能生效。

2、要触发@Recover方法,那么在@Retryable方法上不能有返回值,只能是void才能生效。

3、使用了@Retryable的方法里面不能使用try...catch包裹,要在发放上抛出异常,不然不会触发。

4、在重试期间这个方法是同步的,如果使用类似Spring Cloud这种框架的熔断机制时,可以结合重试机制来重试后返回结果。

5、Spring Retry不只能注入方式去实现,还可以通过API的方式实现,类似熔断处理的机制就基于API方式实现会比较宽松。

示例代码:https://github.com/easonjim/5_java_example/tree/master/springboottest/springboottest3

参考:

http://blog.csdn.net/u014513883/article/details/52371198(以上内容部分转自此篇文章)

https://github.com/spring-projects/spring-retry(官网)

http://www.jianshu.com/p/314059943f1c

http://www.broadview.com.cn/article/233(熔断重试)

Spring异常重试框架Spring Retry的更多相关文章

  1. 异常重试框架Spring Retry实践

    前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...

  2. Spring错误异常重试框架guava-retrying

    官网:https://github.com/rholder/guava-retrying Maven:https://mvnrepository.com/artifact/com.github.rho ...

  3. C#异常重试通用类Retry

    //Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...

  4. spring batch批处理框架学习

    内如主要来自以下链接: http://www.importnew.com/26177.html http://www.infoq.com/cn/articles/analysis-of-large-d ...

  5. 12.Spring——Web MVC框架

    1.Spring Web MVC 框架 2.Spring MVC Hello World 例子 1.Spring Web MVC 框架 Spring web         MVC 框架提供了模型-视 ...

  6. Spring官方文档翻译——15.1 介绍Spring Web MVC框架

    Part V. The Web 文档的这一部分介绍了Spring框架对展现层的支持(尤其是基于web的展现层) Spring拥有自己的web框架--Spring Web MVC.在前两章中会有介绍. ...

  7. Spring Batch 批处理框架

    <Spring Batch 批处理框架>基本信息作者: 刘相 出版社:电子工业出版社ISBN:9787121252419上架时间:2015-1-24出版日期:2015 年2月开本:16开页 ...

  8. 图书简介:Spring Batch批处理框架

    大数据时代批处理利器,国内首度原创解析Spring Batch框架. 内容简介: <Spring Batch 批处理框架>全面.系统地介绍了批处理框架Spring Batch,通过详尽的实 ...

  9. Spring Batch 批处理框架介绍

    前言 在大型的企业应用中,或多或少都会存在大量的任务需要处理,如邮件批量通知所有将要过期的会员,日终更新订单信息等.而在批量处理任务的过程中,又需要注意很多细节,如任务异常.性能瓶颈等等.那么,使用一 ...

随机推荐

  1. idea打jar包

    昨天碰到个问题:使用idea打成jar包,但是在测试环境一直报错.参考: http://blog.csdn.net/aotian16/article/details/52198378 之后发现原来的j ...

  2. Java接口与多态

    接口 可以理解为一种特殊的类,里面全部是由全局常量(static final)和公共的抽象方法所组成 接口的定义格式 接口的数据成员,只允许被public, static, final修饰. 接口的方 ...

  3. IP及DNS设置(Netsh)

    #根据连接状态查找使用中网卡gwmi win32_networkadapter -filter "NetConnectionStatus = 2"#根据是否配置网关查找使用中网卡$ ...

  4. CVE-2013-3897漏洞成因与利用分析

    CVE-2013-3897漏洞成因与利用分析 1. 简介 此漏洞是UAF(Use After Free)类漏洞,即引用了已经释放的内存.攻击者可以利用此类漏洞实现远程代码执行.UAF漏洞的根源源于对对 ...

  5. Ubuntu双系统安装过程中遇到的问题

    1.在第六步选择时区的时候,会报错,这时候可以选择断开网络(取消右上角的网络选项)! 2.用easybcd添加启动项以后,无法进入,F12进入BIOS,选择Ubuntu进入,因为以前的BIOS BOO ...

  6. linux系统开机突然黑屏,只有光标

    以前系统都是好好的,今天开机黑屏了,只有一个光标可以看见其他东西都看不见了. 经过检查发现是磁盘满了,将不用的文件删除之后可以正常开机了.特此记录一下!!! 有人问黑屏了怎么删文件? 我采用的方法是W ...

  7. WiFi安全测试工具WiFiPhisher

    官方下载地址:https://github.com/sophron/wifiphisher打不开的要翻GFW好事做到底wifiphisher-master.zip=================== ...

  8. C#图解教程读书笔记(第2章 C#编程概述)

    这章主要是一个对于C#程序的概括解释 和C/C++不同,不是用include声明引用的头文件,而是通过using的方式,声明引用的命名空间. 命名和C/C++类似,并且也是区分大小写的,这件事情在VB ...

  9. 理解Underscore中的节流函数

    上一篇中讲解了Underscore中的去抖函数(_.debounced),这一篇就来介绍节流函数(_.throttled). 经过上一篇文章,我相信很多人都已经了解了去抖和节流的概念.去抖,在一段连续 ...

  10. mysql中与 in 相反的语句 find_in_set('数据',字段名)

    在 mysql 中,我们经常用 in 来查询众多数据中是否有数据表字段中的值: 如果我们在数据表的字段中添加了很多值,然后查询某个值是否是这个字段中众多值的一个时可以用 find_in_set('数据 ...