spring-retry的简单使用
添加Maven依赖:
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.0.RELEASE</version>
</dependency>
代码结构:

源码:
package Exception; /**
* Created by zhengbin06 on 2017/2/14.
*/
public class Clazz {
private int i = 0;
public Student getStudent() throws SException {
if(++i != 3) {
System.out.println(i);
throw new SException();
}
return new Student();
}
}
Clazz.java
package Exception; import org.springframework.retry.RetryCallback;
import org.springframework.retry.RetryContext;
import org.springframework.retry.policy.SimpleRetryPolicy;
import org.springframework.retry.support.RetryTemplate;
import java.util.HashMap;
import java.util.Map; public class RetryUtil { public static <T> T requestThridtWithRetry(final SRequest<T> sRequest) throws Exception {
// 重试模板
RetryTemplate template = new RetryTemplate(); // 何种异常将触发重试
Map<Class<? extends Throwable>, Boolean> map = new HashMap<Class<? extends Throwable>, Boolean>();
map.put(SException.class, true); // 重试次数
int retryTimes = 3; // 定义重试规则
SimpleRetryPolicy policy = new SimpleRetryPolicy(retryTimes, map);
template.setRetryPolicy(policy); // 执行重试
return template.execute(new RetryCallback<T, Exception>() {
public T doWithRetry(RetryContext context) throws SException {
return sRequest.request();
}
}); }
}
RetryUtil.java
package Exception; /**
* Created by zhengbin06 on 2017/2/14.
*/
public class SException extends Exception {
public SException() {
super("getStudenException");
}
}
SException.java
package Exception; /**
* Created by zhengbin06 on 2017/2/14.
*/
public interface SRequest<T> {
public T request() throws SException;
}
SRequest.java
package Exception; /**
* Created by zhengbin06 on 2017/2/14.
*/
public class Student {
@Override
public String toString() {
return "I'am student";
}
}
Student.java
package Exception; /**
* Created by zhengbin06 on 2017/2/14.
*/
public class TestRetry {
public static void main(String[] args) throws Exception {
final Clazz finalS = new Clazz();
Student student = null;
try {
student = RetryUtil.requestThridtWithRetry(new SRequest<Student>() {
public Student request() throws SException {
return finalS.getStudent();
}
});
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(student.toString());
}
}
TestRetry.java
引出一些Java基础点:
spring-retry的简单使用的更多相关文章
- 自己动手实践 spring retry 重试框架
前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...
- Spring retry实践
在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...
- 【spring】spring retry介绍
一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...
- 异常重试框架Spring Retry实践
前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...
- Spring Retry 重试
重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...
- Spring Security4.X 简单实例介绍
简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...
- Spring retry基本使用
Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...
- 使用Spring缓存的简单Demo
使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...
- Spring依赖注入 --- 简单使用说明
Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...
- spring+springMVC+mybatis简单整合
spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...
随机推荐
- PHP 将html页面导出至Word
<?php header("Content-Type: application/msword"); header("Content-Disposition: att ...
- MySQL加载配置文件的顺序
MySQL5.6启动时,按照下表,从上往下的顺序加载配置文件: File Name Purpose /etc/my.cnf Global options /etc/mysql/my.cnf Globa ...
- cors的实现原理
如何辨认一个请求的源domain? 如何发送和处理cors请求? 优势 和 弱点 cookie 和 伪装 1. http://www.staticapps.org/articles/cross-dom ...
- mindoc 在线文档接口系统的 docker 制作过程
说明: mindoc 是一款在线接口文档编辑系统,百度一下就知道了.github地址:https://github.com/lifei6671/mindoc 本机:ubuntu16.04 + dock ...
- WPF对象级资源的定义与查找
文章概述: 本演示介绍了怎样定义WPF对象级的资源,并通过XAML代码和C#訪问和使用对象级资源. 相关下载(代码.屏幕录像):http://pan.baidu.com/s/1hqvJNY8 在线播放 ...
- private static final long serialVersionUID = 1L;
作者:郭无心链接:https://www.zhihu.com/question/24852886/answer/117314768来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...
- ISO GPS定位,坐标转换以及如何显示
这个写的公共类叫做:GPScombineClass类主要展示GPS位置的定位,GPS坐标的获取,然后从手机坐标转换成火星坐标,继而在需要的情况下,由火星转百度 ,百度转火星的详细算法: 在GPSc ...
- BLIST,BindingSource
//gridControl1.DataSource = BList; //BindingSource bs = new BindingSource(); //bs.DataSource = BList ...
- 关于casperjs的wait方法的执行顺序
var casper = require('casper').create({ viewportSize:{ width:1920, height:1080 } }); var url1 = 'htt ...
- nodejs中aes-128-cbc加密和解密
和java程序进行交互的时候,java那边使用AES 128位填充模式:AES/CBC/PKCS5Padding加密方法,在nodejs中采用对应的aes-128-cbc加密方法就能对应上,因为有使用 ...