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. 三大 ...
随机推荐
- Oracle 12C -- truncate的级联操作
在之前的版本中,存在外键约束时,无法直接truncate父表.在12C中,对truncate操作添加了级联操作特性. 前提是创建外键约束时,使用了"on delete casacde&quo ...
- MySQL和ORACLE、SQL Server、PostgreSQL相比
- High-speed Charting Control--MFC绘制图表(折线图、饼图、柱形图)控件
原文地址:https://www.codeproject.com/articles/14075/high-speed-charting-control 本文翻译在CodeProject上的介绍(主要还 ...
- Loader Lock引起的一个Bug
在Windows中,让程序模块化实现的一种方式,就是让事实上现为动态链接库. 然后在主程序启动的时候隐式或者显示的去载入动态链接库.可是假设不恰当的编写动态链接库的DllMain函数,将会引起意想不到 ...
- 一个酷绚的linux 桌面程序 GLX-DOCK (cario-dock)
记录一个酷绚的linux 桌面程序 GLX-DOCK (cario-dock),支持多种风格的桌面主题. http://glx-dock.org/ 优势: 多个workspaces 方便自由切换 ...
- Java – How to join Arrays
Java – How to join Arrays In this article, we will show you a few ways to join a Java Array. Apache ...
- JavaScript监听手机物理返回键的两种解决方法
JavaScript没有监听物理返回键的API,所以只能使用 popstate 事件监听. 有两个解决办法: 1.返回到指定的页面 pushHistory(); window.addEventList ...
- 让MySQL在美国标准下运行
[美国标准下运行的MySQL会有哪方面的调整] 我不得不说,这里有点标题党了:事实上我想说的就是--ansi模式下启动mysqld进行,但是这个ansi我没有找到更好的译文,就给译成了“美国标准”了. ...
- Nginx(六):Nginx HTTP负载均衡和反向代理的配置与优化
一.什么是负载均衡和反向代理 随着网站访问量的快速增长,单台服务器已经无法承担大量用户的并发访问,必须釆用多台服务器协同工作,以提高计算机系统的处理能力和计算强度,满足当前业务量的需求.而如何在完成同 ...
- postgres json
https://www.postgresql.org/docs/9.6/static/functions-json.html Search Documentation: Home → Documen ...