spring retry 使用
1. 场景
<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.dalong.spring.io</groupId><artifactId>springretrydemo</artifactId><version>0.0.1-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.4.2.RELEASE</version></parent><dependencies><dependency><groupId>org.springframework.retry</groupId><artifactId>spring-retry</artifactId><version>1.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$ --></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.geronimo.bundles</groupId><artifactId>aspectjweaver</artifactId><version>1.6.8_2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin><plugin><artifactId>maven-compiler-plugin</artifactId><version>3.1</version><!--$NO-MVN-MAN-VER$ --><configuration><source>1.8</source><target>1.8</target></configuration></plugin></plugins></build></project>
@ServicepublicclassFirstService{@Retryable(value=RemoteAccessException.class,maxAttempts=3,backoff=@Backoff(delay =5000l,multiplier =1))publicString service(String value){// ... do somethingSystem.out.println("do something...");if(null==value||"".equals(value)){thrownewRemoteAccessException("RPC调用异常");}else{return"dalong demo info";}}@RecoverpublicString recover(RemoteAccessException e){// ... panicSystem.out.println(e.getMessage());System.out.println("rong method");return e.getMessage();}}
@ServicepublicclassMyService2{@AutowiredpublicUserOperator userOperator;publicUserInfo getUserinfo()throwsTimeoutException{RetryTemplate template =newRetryTemplate();TimeoutRetryPolicy policy =newTimeoutRetryPolicy();policy.setTimeout(1000L);template.setRetryPolicy(policy);UserInfo result = template.execute(newRetryCallback<UserInfo,TimeoutException>(){@OverridepublicUserInfo doWithRetry(RetryContext context)throwsTimeoutException{// TODO Auto-generated method stubUserInfo info =null;info = userOperator.getUserinfo();return info;}},newRecoveryCallback<UserInfo>(){@OverridepublicUserInfo recover(RetryContext context)throwsException{// TODO Auto-generated method stubUserInfo inf =newUserInfo();inf.setDate(newDate());inf.setAge(333);inf.setInfo("default");return inf;}});return result;}}
@ServicepublicclassUserOperator{publicUserInfo getUserinfo()throwsTimeoutException{UserInfo info =newUserInfo();info.setDate(newDate());info.setAge(333);info.setInfo("dddddd");try{Thread.sleep(2000);}catch(InterruptedException e){// TODO Auto-generated catch blocke.printStackTrace();thrownewTimeoutException("timeout");}return info;}}
@AutowiredpublicMyService2 myService2;@RequestMapping(value ="/user", method =RequestMethod.GET)publicObject getuser()throwsTimeoutException{UserInfo info =null;info = myService2.getUserinfo();return info;// return "this is demo";}
- org.springframework.retry.policy.SimpleRetryPolicy
该策略定义了对指定的异常进行若干次重试。默认情况下,对Exception异常及其子类重试3次。如果创建SimpleRetryPolicy并指定重试异常map,可以选择性重试或不进行重试。下面的代码定义了对TimeOutException进行重试 - org.springframework.retry.policy.NeverRetryPolicy
执行一次待执行操作,若出现异常后不进行重试。 - org.springframework.retry.policy.AlwaysRetryPolicy
异常后一直重试直到成功。 - org.springframework.retry.policy.TimeoutRetryPolicy
在执行execute方法时从open操作开始到调用TimeoutRetryPolicy的canRetry方法这之间所经过的时间。这段时间未超过TimeoutRetryPolicy定义的超时时间,那么执行操作,否则抛出异常。 org.springframework.retry.policy.ExceptionClassifierRetryPolicy
根据产生的异常选择重试策略。
org.springframework.retry.policy.CompositeRetryPolicy
用户指定一组策略,随后根据optimistic选项来确认如何重试。
spring retry 使用的更多相关文章
- Spring retry基本使用
Spring retry基本使用 背景介绍 在实际工作过程中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络 波动出现超时而采取重试手段 ...
- 自己动手实践 spring retry 重试框架
前序 马上过年了,预祝大家,新年快乐,少写bug 什么是spring retry? spring retry是从spring batch独立出来的一个能功能,主要实现了重试和熔断. 什么时候用? 远程 ...
- Spring Retry
最近组内准备将项目中原有的重试功能抽取出来重构为一个重试平台,由于对重试的功能要求比较高,采用了不少中间件和框架(jimdb,jproxy, Elastic-Job ,JMQ,Hbase, Disru ...
- Spring retry实践
在开发中,重试是一个经常使用的手段.比如MQ发送消息失败,会采取重试手段,比如工程中使用RPC请求外部服务,可能因为网络波动出现超时而采取重试手段......可以看见重试操作是非常常见的一种处理问题, ...
- Spring异常重试框架Spring Retry
Spring Retry支持集成到Spring或者Spring Boot项目中,而它支持AOP的切面注入写法,所以在引入时必须引入aspectjweaver.jar包. 快速集成的代码样例: @Con ...
- 【spring】spring retry介绍
一.为什么需要重试? 我们知道只要是网络请求都有失败的情况,这个时候增加retry机制是必要的.而spring全家桶中就有这么一套机制. 二.spring retry spring系列的spring ...
- 异常重试框架Spring Retry实践
前期准备在Maven项目中添加Spring Retry和切面的依赖 POM: <!-- Spring Retry --> <dependency> <groupId> ...
- Spring框架中一个有用的小组件:Spring Retry
1.概述 Spring Retry 是Spring框架中的一个组件, 它提供了自动重新调用失败操作的能力.这在错误可能是暂时发生的(如瞬时网络故障)的情况下很有帮助. 在本文中,我们将看到使用Spri ...
- Spring Retry 在SpringBoot 中的应用
Spring Boot中使用Spring-Retry重试框架 Spring Retry提供了自动重新调用失败的操作的功能.这在错误可能是暂时的(例如瞬时网络故障)的情况下很有用. 从2.2.0版本开始 ...
- Spring Retry 重试
重试的使用场景比较多,比如调用远程服务时,由于网络或者服务端响应慢导致调用超时,此时可以多重试几次.用定时任务也可以实现重试的效果,但比较麻烦,用Spring Retry的话一个注解搞定所有.话不多说 ...
随机推荐
- Java中文件的随机读写
[例 10-12]模仿系统日志,将数据写入到文件尾部. //********** ep10_12.java ********** import java.io.*; class ep10_12{ pu ...
- ASP.NET 缓存技术分析
缓存功能是大型网站设计一个很重要的部分.由数据库驱动的Web应用程序,如果需要改善其性能,最好的方法是使用缓存功能.可能的情况下尽量使用缓存,从内存中返回数据的速度始终比去数据库查的速度快,因而可以大 ...
- 为什么匿名内部类参数必须为final类型
1) 从程序设计语言的理论上:局部内部类(即:定义在方法中的内部类),由于本身就是在方法内部(可出现在形式参数定义处或者方法体处),因而访问方法中的局部变量(形式参数或局部变量)是天经地义的.是很自 ...
- u-boot学习笔记(一):基础概念
1.U-Boot,全称 Universal Boot Loader,是遵循GPL条款的开放源码项目.U-Boot的作用是系统引导.U-Boot从FADSROM.8xxROM.PPCBOOT逐步发展演化 ...
- Python 学习记录----利用Python绘制奥运五环
import turtle #导入turtle模块 turtle.color("blue") #定义颜色 turtle.penup() #penup和pendown()设置画笔抬起 ...
- css实现隐藏显示
<head> <meta http-equiv="content-type" content="text/html;charset=utf-8" ...
- amazon oa1 - search in 2D array II [Leetcode] 240
https://leetcode.com/problems/search-a-2d-matrix-ii/ 巧解题,矩阵本身等于了一个binary search tree,从中值开始走 时间复杂度 O( ...
- Python——Day3知识点——文件操作
一.打开文件 文件句柄 = open('文件路径', '模式') 打开文件时,需要指定文件路径和以何等方式打开文件,打开后,即可获取该文件句柄,日后通过此文件句柄对该文件操作. 打开文件的模式有: r ...
- redis 数据结构一 之t_string
简介 REDIS有非常丰富的数据结构 以及建立在这数据结构上的操作,在源文件中主要集中在 T_hash.c /T_list.c /T_string.c/T_zset.c 可以说读懂了这4个源文件 大 ...
- A - 迷宫问题
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Pra ...