guava的重试机制guava-retrying使用
1,添加maven依赖
<dependency>
<groupId>com.github.rholder</groupId>
<artifactId>guava-retrying</artifactId>
<version>2.0.0</version>
</dependency>
2,定义重试机制
Retryer<CMSResultDTO> smsRetryer = RetryerBuilder.<CMSResultDTO>newBuilder()
.retryIfResult(cmsResultDTO->cmsResultDTO.getCode() != SM_SUCCESS_CODE) // 短信返回的码不是200要重试
.retryIfResult(Predicates.<CMSResultDTO>isNull()) // 返回的数据是null要重试
.retryIfExceptionOfType(Exception.class) // 返回的异常错误类
.withStopStrategy(StopStrategies.stopAfterAttempt(ATTEMPT_NUM))
.withWaitStrategy(WaitStrategies.fixedWait(SLEEP_TIME, TimeUnit.SECONDS)) // 隔1秒重试
.withRetryListener(new SMRetryListener<>())
.build();
3,定义要重试的任务
Callable<CMSResultDTO> task = ()->{
log.info("sm input param:type=>{}, interCode=>{}, mobile=>{}, pair=>{}",
type, interCode, mobile, pair);
CMSResultDTO cmsResultDTO = cmsService.sendMessage(type, interCode, mobile, pair);
log.info("sm return data:{}", JSON.toJSONString(cmsResultDTO));
return cmsResultDTO;
};
4,重试机制重试任务
CMSResultDTO cmsResultDTO = null;
try {
cmsResultDTO = smsRetryer.call(task);
} catch (ExecutionException e) {
log.error("SM ExecutionException", e);
} catch (RetryException e) {
log.error("SM RetryException", e);
}
return cmsResultDTO;
以下是一个关于重试发短信的完整例子
/**
* 重试机制 发送短信接口支持国际码
* @param type 模版号
* @param interCode 国际码
* @param mobile 手机号码
* @param pair 参数对
* @return 消息发送结果,包括发送状态和消息标识
*/
public CMSResultDTO retrySendMessage(Integer type, String interCode, String mobile, Map<String, String> pair) throws CMSQueueException, CMSSendException {
Preconditions.checkNotNull(type, "type不能为null");
Preconditions.checkNotNull(interCode, "interCode不能为null");
Preconditions.checkNotNull(mobile, "mobile不能为null");
Preconditions.checkNotNull(pair, "pair不能为null"); Callable<CMSResultDTO> task = ()->{
log.info("sm input param:type=>{}, interCode=>{}, mobile=>{}, pair=>{}",
type, interCode, mobile, pair);
//调用第三方发短信接口,得到返回值,第一时间记录到log中
CMSResultDTO cmsResultDTO = cmsService.sendMessage(type, interCode, mobile, pair);
log.info("sm return data:{}", JSON.toJSONString(cmsResultDTO));
return cmsResultDTO;
};
//定义重试的机制原理
Retryer<CMSResultDTO> smsRetryer = RetryerBuilder.<CMSResultDTO>newBuilder()
.retryIfResult(cmsResultDTO->cmsResultDTO.getCode() != SM_SUCCESS_CODE) // 短信返回的码不是200要重试
.retryIfResult(Predicates.<CMSResultDTO>isNull()) // 返回的数据是null要重试
.retryIfExceptionOfType(Exception.class) // 返回的异常错误类
.withStopStrategy(StopStrategies.stopAfterAttempt(ATTEMPT_NUM))
.withWaitStrategy(WaitStrategies.fixedWait(SLEEP_TIME, TimeUnit.SECONDS)) // 隔1秒重试
//监听器
.withRetryListener(new SMRetryListener<>())
.build(); CMSResultDTO cmsResultDTO = null;
try {
//执行任务的重试,得到返回结果
cmsResultDTO = smsRetryer.call(task);
} catch (ExecutionException e) {
log.error("SM ExecutionException", e);
} catch (RetryException e) {
log.error("SM RetryException", e);
}
return cmsResultDTO;
} //自定义的监听器
/**
* 重试监听器
* @param <CMSResultDTO>
*/
private class SMRetryListener<CMSResultDTO> implements RetryListener { @Override
public <CMSResultDTO> void onRetry(Attempt<CMSResultDTO> attempt) {
log.info("[retry]time=" + attempt.getAttemptNumber());
if (attempt.hasException()) {
log.error("retry exception", attempt.getExceptionCause());
}
if (attempt.hasResult()) {
if (attempt.getResult() == null) {
log.info("retry return data is null");
} else {
log.info("retry return data is:{}", JSON.toJSONString(attempt.getResult()));
}
}
} }
guava的重试机制guava-retrying使用的更多相关文章
- 使用Guava retryer优雅的实现接口重试机制
转载自: 使用Guava retrying优雅的实现接口重调机制 Guava retrying:基于 guava 的重试组件 实际项目中,为了考虑网络抖动,加锁并发冲突等场景,我们经常需要对异常操作进 ...
- spring-retry 重试机制
业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...
- Java之Retry重试机制详解
应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务上传数据后对返回的结果进行处理:第二步拿到第一步结 ...
- ENode 1.0 - 消息的重试机制的设计思路
项目开源地址:https://github.com/tangxuehua/enode 上一篇文章,简单介绍了enode框架中消息队列的设计思路,本文介绍一下enode框架中关系消息的重试机制的设计思路 ...
- 【Dubbo 源码解析】07_Dubbo 重试机制
Dubbo 重试机制 通过前面 Dubbo 服务发现&引用 的分析,我们知道,Dubbo 的重试机制是通过 com.alibaba.dubbo.rpc.cluster.support.Fail ...
- Spring Cloud 请求重试机制核心代码分析
场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...
- Volley超时重试机制
基础用法 Volley为开发者提供了可配置的超时重试机制,我们在使用时只需要为我们的Request设置自定义的RetryPolicy即可. 参考设置代码如下: int DEFAULT_TIMEOUT_ ...
- SpringCloud | FeignClient和Ribbon重试机制区别与联系
在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...
- Ribbon重试机制与Hystrix熔断机制的配置问题1
Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...
随机推荐
- P4094 [HEOI2016/TJOI2016]字符串 后缀数组+主席树+二分答案
$ \color{#0066ff}{ 题目描述 }$ 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须 ...
- jq 使用手册
翻译整理:Young.J官方网站:http://jquery.com jQuery是一款同prototype一样优秀js开发库类,特别是对css和XPath的支持,使我们写js变得更加方便!如果你不是 ...
- 灾后重建 Floyd
题目背景 BBB地区在地震过后,所有村庄都造成了一定的损毁,而这场地震却没对公路造成什么影响.但是在村庄重建好之前,所有与未重建完成的村庄的公路均无法通车.换句话说,只有连接着两个重建完成的村庄的公路 ...
- Android studio 混淆打包
AndroidStudio中的项目可以用compile的形式引入github上的开源项目,可以引用module,而不一定都要用libs文件夹中添加jar包的形式. 在最终realease打包时,混淆的 ...
- 百度地图sdk使用
1.android开发百度地图定位,我怎么老是定到几内亚湾 权限问题,首先安卓6.0之后的Android的系统需要动态申请权限. 然后百度地图的sdk的不同功能,申请的权限不同,每个功能都需要看官方文 ...
- kubernetes相关命令
关闭防火墙 [关闭swap] 执行swapoff -a可临时关闭,但系统重启后恢复 编辑/etc/fstab,注释掉包含swap的那一行即可永久关闭 [关闭SeLinux] sed -i 's/SEL ...
- bootstrap-table 选择行,并且获得选中行的所有数据内容
html代码如下: <table id="table" data-toggle="table" th:attr="data-url=@{/vie ...
- ie浏览器(Internet Explorer)不播放背景音乐
这里就不说bgsound了,支持格式较少 一个网站要加背景音乐,好些年没加背景音乐了,用embed标签把背景音乐加上了,Mozilla Firefox,Google Chrome,Safari都正常, ...
- Flask&&人工智能AI -- 8 HTML5+ 初识,HBuilder,夜神模拟器,Webview
昨日内容回顾 1.增删改查: 增: db.collections.insert({a:1}) // 官方不推荐了 db.collections.insertMany([{a:1},{b:1}]) in ...
- Java中的ThreadLocal使用
ThreadLocal用于下面的场景: 1. 不允许多个线程同时访问的资源 2. 单个线程存活过程只使用一个实例 官方定义如下: This class provides thread-local va ...