Spring retry基本使用
Spring retry基本使用
背景介绍
在实际工作过程中,重试是一个经常使用的手段。比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络
波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题,系统设计的手段
而在之前我们项目中处理重拾操作依赖MQ自身的重试机制,但是这种机制不是很灵活,如果某些功能没有使用MQ的话,那么就不是那么方便了,而本文介绍的
Spring-Retry却能够以一种很优雅的方式解决这种问题,当然目前版本的Spring-retry还不是完美的,还是有待改进的.不过已经很不错了.
基本使用
例子1
@Configuration
@EnableRetry
public class Application { @Bean
public Service service() {
return new Service();
} } @Service
class Service {
@Retryable(RemoteAccessException.class)
public void service() {
// ... do something
}
@Recover
public void recover(RemoteAccessException e) {
// ... panic
}
}
例子2
@org.springframework.stereotype.Service
public class Service1 { @Retryable(value = {RemoteAccessException.class, RuntimeException.class},
maxAttempts = 2,
backoff = @Backoff(value = 2000))
public void service() {
System.out.println("do some things");
// this exception will just trigger recover1, do not trigger recover3
throw new RemoteAccessException("remote access exception");
// this exception will just trigger recover2
// throw new RuntimeException("runtime exception"); // System.out.println("do another things");
} // 如果使用注解的话,这个recover貌似只能写在本类中,我测试了如果将recover方法写在
// recoverService中,好像找不到 @Recover
public void recover1(RemoteAccessException e) {
System.out.println(e.getMessage());
System.out.println("do recover operation1");
} @Recover
public void recover2(RuntimeException e) {
System.out.println(e.getMessage());
System.out.println("do recover operation2");
} @Recover
public void recover3(RemoteAccessException e) {
System.out.println(e.getMessage());
System.out.println("do recover operation3");
} }
例子3
@Service
public class Service2 { public void test(){
final RetryTemplate retryTemplate = new RetryTemplate();
final SimpleRetryPolicy policy = new SimpleRetryPolicy(3, Collections.<Class<? extends Throwable>, Boolean>
singletonMap(Exception.class, true));
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
fixedBackOffPolicy.setBackOffPeriod(100);
retryTemplate.setRetryPolicy(policy);
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
final RetryCallback<Object, Exception> retryCallback = new RetryCallback<Object, Exception>() {
public Object doWithRetry(RetryContext context) throws Exception {
System.out.println("do some thing");
//设置context一些属性,给RecoveryCallback传递一些属性
context.setAttribute("key1", "value1");
System.out.println(context.getRetryCount());
throw new Exception("exception");
// return null;
}
}; // 如果RetryCallback执行出现指定异常, 并且超过最大重试次数依旧出现指定异常的话,就执行RecoveryCallback动作
final RecoveryCallback<Object> recoveryCallback = new RecoveryCallback<Object>() {
public Object recover(RetryContext context) throws Exception {
System.out.println("do recory operation");
System.out.println(context.getAttribute("key1"));
return null;
}
}; try {
final Object execute = retryTemplate.execute(retryCallback, recoveryCallback);
} catch (Exception e) {
e.printStackTrace();
}
}
}
参考资料
Spring retry基本使用的更多相关文章
- 自己动手实践 spring retry 重试框架
前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...
- Spring Retry
最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...
- Spring retry实践
在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...
- Spring异常重试框架Spring Retry
Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...
- 【spring】spring retry介绍
一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...
- 异常重试框架Spring Retry实践
前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...
- Spring框架中一个有用的小组件:Spring Retry
1.概述 Spring Retry 是Spring框架中的一个组件, 它提供了自动重新调用失败操作的能力.这在错误可能是暂时发生的(如瞬时网络故障)的情况下很有帮助. 在本文中,我们将看到使用Spri ...
- Spring Retry 在SpringBoot 中的应用
Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...
- Spring Retry 重试
重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...
随机推荐
- Android Shape总结
Shape的基础标签共有6个:corners, gradient, solid, stroke, padding, size Shape可以有四种形状:rectangle(矩形,默认形状),oval( ...
- Tomcat启动找不到JRE_HOME的解决方法
在配置测试环境时,将生产服务器的Tomcat目录打包过来后解压后,启动Tomcat后,发现如下问题: [tomcat@gsp bin]$ ./shutdown.sh Using CATALINA_BA ...
- PowerShell中的基础数据类型
PowerShell是一个面向对象的语言,在申明变量的时候不强制要求申明数据类型,使用$开头来申明变量即可. 基本数据类型 PowerShell本身是基于.Net开发出来的,所以在.Net中的基本数据 ...
- 新书出版《.NET框架设计—模式、配置、工具》感恩回馈社区!
很高兴我的第一本书由图灵出版社出版.本书总结了我这些年来对框架学习.研究的总结,里面纯干货,无半句废话. 书的详情请看互动网的销售页面:http://product.china-pub.com/377 ...
- 基于Ajax+div的“左边菜单、右边内容”页面效果实现
效果演示: ①默认页面(index.jsp): ②:点击左侧 用户管理 标签下的 用户列表 选项后,右边默认页面内容更新为用户列表页(userList.jsp)的内容 : ③:同理,点击 产品管理.订 ...
- sql server: sql script
select ProductGUID,ProductName,ProjectGUID from dbo.Product /* F637A079-E22B-4E50-87E9-000147B1B1F4 ...
- Asp.Net MVC+BootStrap+EF6.0实现简单的用户角色权限管理2
首先我们来写个类进行获取当前线程内唯一的DbContext using System; using System.Collections.Generic; using System.Data.Enti ...
- 解决开启SQL Server sql Always on Group 事务日志增大的问题
配置了Alwayson之后,因为没有只能使用完全恢复模式,不能使用简单或大容量日志模式,所以日志不断增长,不能使用改变恢复模式的方式清空日志 手动操作收缩或截断日志也无效 读了一些文章后发现,有人使用 ...
- nginx 日志相关配置总结
设置位于nginx.conf: log_format main '$server_name $remote_addr - $remote_user [$time_local] &q ...
- [WPF系列]-Adorner
简介 通常我们想对现有的控件,做些修饰时我们就会想到一个装饰模式.WPF中也提供了这样的实现思路:通过将Adorner添加到AdornerLayer中来实现装饰现有控件的效果.如图示: 本来T ...