添加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基础点:

一天一个Java基础——泛型

Java基础——异常处理

spring-retry的简单使用的更多相关文章

  1. 自己动手实践 spring retry 重试框架

    前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...

  2. Spring retry实践

    在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...

  3. 【spring】spring retry介绍

    一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...

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

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

  5. Spring Retry 重试

    重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...

  6. Spring Security4.X 简单实例介绍

    简介 本例子采用的是SpringMVC.SpringSecurity和Spring整合的简单使用 使用gradle搭建的项目(gradle比maven更加便捷),可以自行了解 web.xml配置 &l ...

  7. Spring retry基本使用

    Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...

  8. 使用Spring缓存的简单Demo

    使用Spring缓存的简单Demo 1. 首先创建Maven工程,在Pom中配置 <dependency> <groupId>org.springframework</g ...

  9. Spring依赖注入 --- 简单使用说明

    Spring依赖注入 --- 简单使用说明 本文将对spring依赖注入的使用做简单的说明,enjoy your time! 1.使用Spring提供的依赖注入 对spring依赖注入的实现方法感兴趣 ...

  10. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

随机推荐

  1. Win7系统计算机中Msvcr100.dll丢失的解决办法

    1.使用安全卫士里的人工服务. 在搜索框里输入msvcr100.dll. 点击查找方案. 2.点击msvcr100.dll问题后面的立即修复. 只要等待片刻就好了.

  2. mvc 缓存页面 减轻服务器压力

    方法: using System; using System.Collections.Generic; using System.Linq; using System.Web; using Syste ...

  3. STM32的JTAG下载模式

    SWJ:串行线JTAG配置 (Serial wire JTAG configuration)  SWJ(串行线JTAG)支持JTAG或SWD访问Cortex的调试端口. 系统复位后的默认状态是启用SW ...

  4. git clone 问题 fatal: unable to access

    git clone 遇到问题 Cloning into 'warp-ctc'...fatal: unable to access 'https://github.com/SeanNaren/warp- ...

  5. User-Agent 信息汇总(拿去就能用)

    # encoding=utf- agents = [ "Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) ...

  6. 解决Myeclipse中导入自定义的配色方案后,JSP中的js代码块为白色背景的问题

    捣鼓了大半个上午,终于搞定.这样设置就可以了: 点击MyEclipse上方的菜单栏中的window菜单.选择Preferences菜单项.在弹出的窗口的左侧树形菜单依次选择:MyEclipse.Fil ...

  7. MySQL 两个死锁样例

    [引子] 从事MySQL-DBA这一行也有些年头了,想对新人说,在分析死锁问题时应该还要考虑到有一个叫请求队列的“概念”.之所以 在这里提这个不是因为新手不知道,而是有时候会自然而然的想不到. 不信的 ...

  8. Android 计算文件 MD5 遇到的问题

    版本下载,做 MD5 校验,使用的 MD5 算法出现了异常,当出现以 0 开头的 MD5的时候,会把 0 给忽略掉,造成 MD5 只有 31 位,造成校验失败. 转:http://blog.csdn. ...

  9. [svc]arpping链路层检测

    arping命令,测试链路层是否同 参考 yum install iputils -y arping - send ARP REQUEST to a neighbour host -b:用于发送以太网 ...

  10. python 给文件批量加‘“’ ”,"

    file = open('face.txt','r') filew = open('face1.txt','w') try: for line in file: print(line) if line ...