添加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. sql的行转列(PIVOT)与列转行(UNPIVOT) webapi 跨域问题 Dapper 链式查询 扩展 T4 代码生成 Demo (抽奖程序)

    sql的行转列(PIVOT)与列转行(UNPIVOT)   在做数据统计的时候,行转列,列转行是经常碰到的问题.case when方式太麻烦了,而且可扩展性不强,可以使用 PIVOT,UNPIVOT比 ...

  2. MVC摘记

    2013年9月23日18:04:06 直接cshtml页面的js中使用     var customerId = @(ViewBag.Oppotunity as OppotunityDto==null ...

  3. HTML <video> 标签

    http://www.w3school.com.cn/tags/tag_video.asp <%@ Page Language="VB" AutoEventWireup=&q ...

  4. Mac OS X 下多个JDK版本的切换小技巧

    如果在系统中安装了多个Java JDK 版本,可能会根据项目或编译环境的不同来回切换,下面有两种非常方便的方法供大家选择. 我的机器上有 Java 7 和 Java 8,大家根据自己的 JDK 版本自 ...

  5. 深入理解Python中的yield和send

    send方法和next方法唯一的区别是在执行send方法会首先把上一次挂起的yield语句的返回值通过参数设定,从而实现与生成器方法的交互. 但是需要注意,在一个生成器对象没有执行next方法之前,由 ...

  6. .net自定义控件Control、WebControl、CompositeControl

    一.呈现方法 1.Control主要有以下4个方法用于呈现 //该方法为入口方法 public virtual void RenderControl (HtmlTextWriter writer) { ...

  7. Java 8 – Convert a Stream to LIST

    Java 8 – Convert a Stream to LIST package com.mkyong.java8; import java.util.List;import java.util.s ...

  8. C#两个DataTable拷贝问题:该行已经属于另一个表的解决方法

    一.DataTable.Rows.Add(DataRow.ItemArray); 二.DataTable.ImportRow(DataRow) 三.设置DataTable的tablename,然后.R ...

  9. SpringBoot自定义序列化的使用方式--WebMvcConfigurationSupport

    场景及需求: 项目接入了SpringBoot开发,现在需求是服务端接口返回的字段如果为空,那么自动转为空字符串. 例如: [     {         "id": 1,      ...

  10. php分享二十二:设计模式

    策略模式与观察者模式的区别: 策略是我现在方法有几个东西 我传入不同的参数就能拿到不同的东西,就像是spring中的populate,我传入不同的handler对象进来 返回的可以是javaBean或 ...