网站首页 > 技术教程 正文
对于很多业务,为了使处理更加健壮并减少失败的可能性,有时自动重试失败的操作会有所帮助,使用这种机制它可能使我们的操作在随后的尝试中成功。在本文中,我们将学习如何在 Spring 应用程序中使用 Spring Retry 功能。
一、项目配置
为了启用 Spring Retry 的支持,首先要在pom.xml 文件中添加以下依赖项
<properties>
<version.spring.retry>1.3.1</version.spring.retry>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>${version.spring.retry}</version>
</dependency>
</dependencies>
</dependencyManagement>
?
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>
</dependencies>
?
复制代码
在这里我们使用的是maven中央仓库中最新的制品1.3.1。除了其本身的依赖,正常使用Spring Retry还需要 依赖AOP。对于Spring Boot 项目,在 pom.xml中添加 Spring-Boot-starter-aop starter
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
复制代码
二、启用Spring Retry支持
为了启用Spring Retry的功能,需要向配置类添加@EnableRetry注释。
@SpringBootApplication
@EnableRetry
public class Launcher {
public static void main(String[] args) {
SpringApplication.run(Launcher.class, args);
}
}
复制代码
使用Retry的功能,有两种常见的方式,包括@Retryable注解到方法,以及RetryTemplate配置策略后手动去执行。
三、@Retryable 注解形式
3.1 @Retryable
注解方式就是在启用重试特性的方法上使用@Retryable注释。
我们提供一个接口,提供一个需要重试的方法,并为这个方式赋于改注解。
public interface RetryService {
/**
* 指定异常CustomRetryException重试,重试最大次数为4(默认是3),重试补偿机制间隔200毫秒
* 还可以配置exclude,指定异常不充实,默认为空
* @return result
* @throws CustomRetryException 指定异常
*/
@Retryable(value = {CustomRetryException.class},maxAttempts = 4,backoff = @Backoff(200))
String retry() throws CustomRetryException;
}
复制代码
注解参数的赋值内容解释见方法的注释。
- value属性告诉 Spring retry 在方法在CustomRetryException异常出现时触发重试。
- maxAttempts设置重试的最大次数,如果没有指定默认值为3。
- backoff指定下次重试的延迟时间,默认值为1秒。
CustomRetryException异常是自定义的异常,代码如下
public class CustomRetryException extends Exception{
public CustomRetryException(String error){
super(error);
}
}
复制代码
3.2 @Recover
当被@Retryable注解的方法由于指定的异常而失败时,用于定义单独恢复方法的@Recover注释。
@Service
@Slf4j
public class RetryServiceImpl implements RetryService {
?
private static int count = 1;
?
@Override
public String retry() throws CustomRetryException {
log.info("retry{},throw CustomRetryException in method retry",count);
count ++;
throw new CustomRetryException("throw custom exception");
}
?
@Recover
public String recover(Throwable throwable) {
log.info("Default Retry service test");
return "Error Class :: " + throwable.getClass().getName();
}
?
}
复制代码
少年,没看够?点击小编主页,随便点点看看,说不定有惊喜呢?有你们的支持是我最大的动力!
链接:https://juejin.cn/post/7037347914787586085
猜你喜欢
- 2024-09-25 屡败屡战RETRY第六关关卡攻略 屡屡败屡战的意思
- 2024-09-25 基于redis实现的分布式队列 基于 redis 实现分布式锁是?
- 2024-09-25 .NET 使用 mysql 时 EnableRetryOnFailure() 函数的作用及注意事项
- 2024-09-25 新连载漫画大作导视(十月篇) 十日十月漫画作家
- 2024-09-25 基于guava的重试组件Guava-Retryer
- 2024-09-25 我是怎样得到AO3内容的 我是怎样得到ao3内容的英文
- 2024-09-25 Spring Boot 中使用 spring-retry 轻松解决重试
- 2024-09-25 Java重试利器之Guava-Retryer retry java
- 2024-09-25 springCloud负载均衡机制 springcloud负载均衡策略有哪些
- 2024-09-25 LabVIEW调用DLL中一、二级指针参数及打包exe运行异常的解决办法
你 发表评论:
欢迎- 07-03收到“老板”QQ指令转账,厦门多名财务人员被骗!
- 07-03手机QQ红包BUG 发送不能被领取的口令红包方法
- 07-03“老板”发QQ指令要求转账 财务被骗42万……最后民警出手了!
- 07-03微信推广引流108式:利用QQ自动强制加好友代码从网站导流
- 07-03围观!有人发现QQ新玩法,一天进账近百万!
- 07-03“领导”QQ发来指令 她就转出16万元
- 07-03如何自定义样式营销qq在线咨询代码
- 07-03利用代码发QQ空消息 只闪动没有消息内容
- 最近发表
- 标签列表
-
- sd分区 (65)
- raid5数据恢复 (81)
- 地址转换 (73)
- 手机存储卡根目录 (55)
- tcp端口 (74)
- project server (59)
- 双击ctrl (55)
- 鼠标 单击变双击 (67)
- debugview (59)
- 字符动画 (65)
- flushdns (57)
- ps复制快捷键 (57)
- 清除系统垃圾代码 (58)
- web服务器的架设 (67)
- 16进制转换 (69)
- xclient (55)
- ps源文件 (67)
- filezilla server (59)
- 句柄无效 (56)
- word页眉页脚设置 (59)
- ansys实例 (56)
- 6 1 3固件 (59)
- sqlserver2000挂起 (59)
- vm虚拟主机 (55)
- config (61)
本文暂时没有评论,来添加一个吧(●'◡'●)