Spring异常重试框架Spring Retry
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的更多相关文章
- 异常重试框架Spring Retry实践
前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...
- Spring错误异常重试框架guava-retrying
官网:https://github.com/rholder/guava-retrying Maven:https://mvnrepository.com/artifact/com.github.rho ...
- C#异常重试通用类Retry
//Retry机制 public static class Retry { /// <summary> /// 重试零个参数无返回值的方法 /// </summary> /// ...
- spring batch批处理框架学习
内如主要来自以下链接: http://www.importnew.com/26177.html http://www.infoq.com/cn/articles/analysis-of-large-d ...
- 12.Spring——Web MVC框架
1.Spring Web MVC 框架 2.Spring MVC Hello World 例子 1.Spring Web MVC 框架 Spring web MVC 框架提供了模型-视 ...
- Spring官方文档翻译——15.1 介绍Spring Web MVC框架
Part V. The Web 文档的这一部分介绍了Spring框架对展现层的支持(尤其是基于web的展现层) Spring拥有自己的web框架--Spring Web MVC.在前两章中会有介绍. ...
- Spring Batch 批处理框架
<Spring Batch 批处理框架>基本信息作者: 刘相 出版社:电子工业出版社ISBN:9787121252419上架时间:2015-1-24出版日期:2015 年2月开本:16开页 ...
- 图书简介:Spring Batch批处理框架
大数据时代批处理利器,国内首度原创解析Spring Batch框架. 内容简介: <Spring Batch 批处理框架>全面.系统地介绍了批处理框架Spring Batch,通过详尽的实 ...
- Spring Batch 批处理框架介绍
前言 在大型的企业应用中,或多或少都会存在大量的任务需要处理,如邮件批量通知所有将要过期的会员,日终更新订单信息等.而在批量处理任务的过程中,又需要注意很多细节,如任务异常.性能瓶颈等等.那么,使用一 ...
随机推荐
- geoserver 知识小计
http://localhost:8888/geoserver/wms?service=WMS&request=GetCapabilities 这个地址用于获取发布的WMS服务的属性,用于获取 ...
- Ubuntu14.04下如何安装Python爬虫框架Scrapy
按照官方文档的说明,安装scrapy 需要以下程序或者库: (1).Python 2.7 (2).lxml. Most linux distributions ships PRepackaged ve ...
- Java初学者 编译能通过,但显示有错误,并且不会自动弹出方法的解决方法。
因为使用了 @Data注解,关于注解的作用尚未深入理解,此处先做一个记录. 解决方法是,添加lombok插件
- java img图片转pdf 工具类
package com.elitel.hljhr.comm.web.main.controller; import java.io.File; import java.io.FileNotFoundE ...
- a标签在实际工作中的应用
学习的时候,我们对a标签的认知: 1.href属性添加一个地址,可进行页面的跳转 2.用锚点,制作页面内跳转和跨页面跳转(之前有写过一篇关于锚点的随笔:http://www.cnblogs.com/q ...
- Spring学习总结之---装配Bean
Spring配置的可选方案 前言:Spring容器负责创建应用程序中的bean并通过DI来协调这些对象之间的关系,作为开发人员,你需要告诉Spring容器要创建那些Bean,以哪种方式创建,并且如何将 ...
- SAP S/4HANA里如何创建Customer主数据以及执行后续处理
来自Jerry的同事Zhang Sean. 1, Launch tcode: BP and select the Organization 2, Maintain the information fo ...
- FRP-Functional Reactive Programming-函数响应式编程
响应式编程是一种面向数据流和变化传播的编程范式: 响应式编程和函数式编程的融合: 响应式编程为内核:函数式编程为工具: 流的概念先天适合函数式编程. Some quotes from the arti ...
- 表中与当前库的collation不一致的字段
--对于与当前数据库排序规则不一致的字段. select o.name, c.name ,collation_namefrom sys.columns c,sys.objects o where c. ...
- HDU 1281 棋盘游戏 【二分图最大匹配】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1281 题意概括: 有N*M大的棋盘,要在里面放尽量多的“车”,求最多能放的车的个数,和为了放最多的车有多 ...