spring-retry简单demo(附完整代码)
重试
最近项目要用到重试。开始想自己写,后来想用RetryTemplate,最后想到既然项目用了springboot,还是直接集成spring-retry把。
代码
Application
package com.danni;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.retry.annotation.EnableRetry;
@SpringBootApplication
@EnableRetry
public class Application {
public static void main(String[] args) throws Exception {
@SuppressWarnings("resource")
ApplicationContext annotationContext = new AnnotationConfigApplicationContext("com.danni");
RemoteService remoteService = annotationContext.getBean("remoteService", RemoteService.class);
remoteService.call();
}
}
RemoteService
package com.danni;
import org.springframework.remoting.RemoteAccessException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
@Service
public class RemoteService {
@Retryable(value= {RemoteAccessException.class},maxAttempts = 3,backoff = @Backoff(delay = 5000l,multiplier = 1))
public void call() throws Exception {
System.out.println("do something...");
throw new RemoteAccessException("RPC调用异常");
}
@Recover
public void recover(RemoteAccessException e) {
System.out.println(e.getMessage());
}
}
pom.xml
<project xmlns="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>spring-retry-demo</groupId>
<artifactId>spring-retry-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Retry -->
<!-- https://mvnrepository.com/artifact/org.springframework.retry/spring-retry -->
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<!-- <version>1.1.2.RELEASE</version> -->
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<!-- <version>1.8.6</version> -->
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>

运行
...省略
16:33:26.561 [main] INFO org.springframework.jmx.export.annotation.AnnotationMBeanExporter - Registering beans for JMX exposure on startup
16:33:26.561 [main] DEBUG org.springframework.jmx.export.annotation.AnnotationMBeanExporter - Autodetecting user-defined JMX MBeans
16:33:26.561 [main] DEBUG org.springframework.context.annotation.AnnotationConfigApplicationContext - Unable to locate LifecycleProcessor with name 'lifecycleProcessor': using default [org.springframework.context.support.DefaultLifecycleProcessor@170da4c]
16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'lifecycleProcessor'
16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor'
16:33:26.561 [main] DEBUG org.springframework.core.env.PropertySourcesPropertyResolver - Could not find key 'spring.liveBeansView.mbeanDomain' in any property source
16:33:26.561 [main] DEBUG org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning cached instance of singleton bean 'remoteService'
16:33:26.577 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=0
do something...
16:33:26.592 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:33:31.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=1
16:33:31.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=1
do something...
16:33:31.592 [main] DEBUG org.springframework.retry.backoff.ExponentialBackOffPolicy - Sleeping for 5000
16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=2
16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry: count=2
do something...
16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Checking for rethrow: count=3
16:33:36.592 [main] DEBUG org.springframework.retry.support.RetryTemplate - Retry failed last attempt: count=3
RPC调用异常
代码下载
https://gitee.com/danni3/spring-retry-demo
参考
https://blog.csdn.net/u014513883/article/details/52371198
https://github.com/spring-projects/spring-retry
https://www.cnblogs.com/EasonJim/p/7684649.html
spring-retry简单demo(附完整代码)的更多相关文章
- Spring的简单demo
---------------------------------------- 开发一个Spring的简单Demo,具体的步骤如下: 1.构造一个maven项目 2.在maven项目的pom.xml ...
- Android 监听双卡信号强度(附完整代码)
Android 监听双卡信号强度 监听单卡信号强度 监听单卡的信号强度非常简单直接用TelephonyManager.listen()去监听sim卡的信号强度. TelephonyManager = ...
- JPG学习笔记3(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- 【仿真】Carla之收集数据快速教程 (附完整代码) [7]
收集过程可视化展示,随后进入正文: 参考与前言 看到仿真群对这类任务下(用carla收集数据然后再做训练等) 需求量大,顺手马上写一个好了,首先收集数据需要考虑清楚: 收集什么数据,需要什么样的数据格 ...
- JPG学习笔记2(附完整代码)
#topics h2 { background: rgba(43, 102, 149, 1); border-radius: 6px; box-shadow: 0 0 1px rgba(95, 90, ...
- spring、mybatis、事务项目整合,附完整代码和数据库文件
配置依赖项 pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http:/ ...
- 一文讲透为Power Automate for Desktop (PAD) 实现自定义模块 - 附完整代码
概述 Power Automate for Desktop (以下简称PAD)是微软推出的一款针对Windows桌面端的免费RPA(机器人流程自动化)工具,它目前默认会随着Windows 11安装,但 ...
- Spring基础学习,附例子代码讲解
什么是Spring.IOC.AOP.DI? Spring是一个基于IOC和AOP的结构J2EE系统的框架. IOC(Inversion Of Control)控制反转(Spring的基 ...
- Java Mail发送简单邮件,完整代码
依赖javax.mail.jar,地址:https://java.net/projects/javamail/pages/Home 完整示例代码如下: package com.jadic.utils; ...
随机推荐
- pcre7.0在vc6.0编译
(0)从http://gnuwin32.sourceforge.net/packages/pcre.htm (pcre windows)下下载最新的windows平台源代码pcre-7.0-src. ...
- 【OPPO主题制作系列 - 01】-- 写个小工具自动打包Theme文件
参考OPPO主题设计师站: http://dev.theme.oppomobile.com/user/user_start 想要打包成Theme文件,必须把需要打包的文件夹拖到oppo-themepa ...
- 搭建网络svn实战
工作中的问题(7) 转自:http://blog.csdn.net/xiaoting451292510/article/details/8562570 经常性我们和朋友写一些程序,大家在不同的城市确有 ...
- pip命令自动补全功能;设置代理;使用国内源
这是pip自带的功能 执行的脚本 把脚本写入.zshrc或者profile等里面,执行source立即生效 设置代理: pip --proxy=http://username:password@pro ...
- 搜索引擎keyword智能提示的一种实现
问题背景 搜索关键字智能提示是一个搜索应用的标配.主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户高速查找到目标 ...
- Java中正则Matcher类的matches()、lookAt()和find()的差别
參考博文地址:http://www.oseye.net/user/kevin/blog/170 1.matcher():仅仅有在整个字符串全然匹配才返回true,否则返回false. 可是假设部分匹配 ...
- 使用literal语法格式化字符串
支持arm64之后,格式化字符串的时候会遇到一些问题,主要与NSInteger的定义有关: #if __LP64__ || (TARGET_OS_EMBEDDED && !TARGET ...
- Linux下通过find命令进行rm文件删除的小技巧
我们常常会通过find命令进行批量操作.如:批量删除旧文件.批量改动.基于时间的文件统计.基于文件大小的文件统计等.在这些操作其中,因为rm删除操作会导致文件夹结构变化,假设要通过find结合r ...
- 2016/2/26 jQuery的技术 1,安装 2,语法选择器$ 事件触发 3,常用jQuery函数
在<网页制作Dreamweaver(悬浮动态分层导航)>中,运用到了jQuery的技术,轻松实现了菜单的下拉.显示.隐藏的效果,不必再用样式表一点点地修改,省去了很多麻烦,那么jQuery ...
- MVC优缺点
1.通过把项目分成model view和controller,使得复杂项目更加容易维护. 2.没有使用view state和服务器表单控件,可以更方便的控制应用程序的行为 3.应用程序通过contro ...