添加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. iphone 开发中使用zbar时遇到的几个典型问题解决方法。

    iphone 开发中使用zbar时遇到的几个典型问题解决方法.   在近期的一个ios项目中使用到了一个二维码扫描库(Qrcode)--ZBar, 期间遇到2个问题.   1. zbar下载后使用其l ...

  2. git 使用经验与技巧总结 (不断更新中)

    git 使用技巧总结   说明: 本地repo已经有啦,对应的远程repo 为tiger, 本地分支为master.    问题一: 当拉取代码的remote repo仓库为tiger, 而现在要将修 ...

  3. Oracle中文乱码解决办法总结

    AMERICAN_AMERICA.WE8ISO8859P1,这个字符编码是西欧字符编码,对应于.Net的就是iso-8859-1字符编码,所以只需要改到系统的默认字符编码就行了. 解决方法: (1) ...

  4. Web用户控件开发--星型评分控件

    本文中分享一个实现简单,使用方便的星型评分控件. 一:贴几张测试图片先: 二.星型评分控件的实现: RatingBar.ascx: <%@ Control Language="C#&q ...

  5. Redis使用问题及知识点记录 - 待整理

    介绍 官网:https://redis.io/commands/expire spring data redis 整合redis使用方法 spring 整合api :http://docs.sprin ...

  6. [转]在windows上实现多个java jdk的共存解决办法

    问题背景 公司项目中应用到的jdk环境为1.6,最近在家学习IntelliJ IDEA中sdk多环境配置时,想安装Jdk1.8,作为学习基础.那么问题来了,公司项目扩展不支持jdk1.8,为了既能满足 ...

  7. Spring boot validation校验

    使用 Hibernate validator 的步骤:1. 在 Pojo 类的字段上, 加上 Hibernate validator 注解2. 在Controller 函数的形参前加上 @Valid ...

  8. mysql 大数据提取

    今天要重五百多万的一个数据库表 提取 大约五十万条数据,刚开始的解决思路是: 先把数据查询出来,然后再导出来,然后再设计一个数据库表格,把这些数据导入,最后导出数据和导入数据花费了很多时间,最后向同事 ...

  9. django官方文档--对静态文件的管理

    一.入门级理解: 在django中对静态文件的管理和模板(template)的思路是一样的.在模板的管理中django是把app用到 到的模板都保存到app目录下的templates子目录中. 静态文 ...

  10. vue-cli项目配置图解

    配置vue项目机构步骤1.搭建环境:node.npm2.安装vue脚手架工具:npm install -g vue-cli3.初始化项目:vue init webpack vuedemo(“vuede ...