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使用的更多相关文章

  1. 使用Guava retryer优雅的实现接口重试机制

    转载自: 使用Guava retrying优雅的实现接口重调机制 Guava retrying:基于 guava 的重试组件 实际项目中,为了考虑网络抖动,加锁并发冲突等场景,我们经常需要对异常操作进 ...

  2. spring-retry 重试机制

    业务场景 应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务逻辑包装给处理方法返回处理结果:第二步拿 ...

  3. Java之Retry重试机制详解

    应用中需要实现一个功能: 需要将数据上传到远程存储服务,同时在返回处理成功情况下做其他操作.这个功能不复杂,分为两个步骤:第一步调用远程的Rest服务上传数据后对返回的结果进行处理:第二步拿到第一步结 ...

  4. ENode 1.0 - 消息的重试机制的设计思路

    项目开源地址:https://github.com/tangxuehua/enode 上一篇文章,简单介绍了enode框架中消息队列的设计思路,本文介绍一下enode框架中关系消息的重试机制的设计思路 ...

  5. 【Dubbo 源码解析】07_Dubbo 重试机制

    Dubbo 重试机制 通过前面 Dubbo 服务发现&引用 的分析,我们知道,Dubbo 的重试机制是通过 com.alibaba.dubbo.rpc.cluster.support.Fail ...

  6. Spring Cloud 请求重试机制核心代码分析

    场景 发布微服务的操作一般都是打完新代码的包,kill掉在跑的应用,替换新的包,启动. spring cloud 中使用eureka为注册中心,它是允许服务列表数据的延迟性的,就是说即使应用已经不在服 ...

  7. Volley超时重试机制

    基础用法 Volley为开发者提供了可配置的超时重试机制,我们在使用时只需要为我们的Request设置自定义的RetryPolicy即可. 参考设置代码如下: int DEFAULT_TIMEOUT_ ...

  8. SpringCloud | FeignClient和Ribbon重试机制区别与联系

    在spring cloud体系项目中,引入的重试机制保证了高可用的同时,也会带来一些其它的问题,如幂等操作或一些没必要的重试. 今天就来分别分析一下 FeignClient 和 Ribbon 重试机制 ...

  9. Ribbon重试机制与Hystrix熔断机制的配置问题1

    Ribbon超时与Hystrix超时问题,为了确保Ribbon重试的时候不被熔断,我们就需要让Hystrix的超时时间大于Ribbon的超时时间,否则Hystrix命令超时后,该命令直接熔断,重试机制 ...

随机推荐

  1. LVS+OSPF 架构(转)

    http://blog.51cto.com/pmghong/1399385 LVS 和 LVS+keepalived 这两种架构在平时听得多了,最近才接触到另外一个架构LVS+OSPF.这个架构实际上 ...

  2. springboot整合actuator,进行运维监控

    首先引入依赖: <?xml version="1.0" encoding="UTF-8"?> <project xmlns="htt ...

  3. POJ1052 Plato's Blocks

    题目来源:http://poj.org/problem?id=1052 题目大意: 把1*1*1的小立方体通过粘接相邻面组成大的立方体的形状.如下图所示: 一层一层地堆叠,立方体从三个方向的投影会分别 ...

  4. C++_对象之间的关系与继承

    派生类和基类之间的特殊关系是基于C++继承的底层模型的. 实际上,C++有3种继承方式:公有继承.保护继承.私有继承. 公有继承是最常见的关系,它建立一种is-a的关系,即派生类对象也是一种基类,可以 ...

  5. sharepoint_study_10

    描述:想页面添加一段脚本效果如图所示 图示: 代码(脚本编辑器): <div class="index-links"> <a class=" index ...

  6. P3970 [TJOI2014]上升子序列

    传送门 DP 十分显然的DP,但是不好写 设 f[ i ] 表示以第 i 个数作结尾时的方案数,原序列为 a 如果不考虑相同的序列: 那么转移就是 Σ f[ j ] (0< j < i & ...

  7. HDU3555 区间的数里面有49的个数(数位dp)

    题目:区间的数里面有49的个数 分析: dp[pos][0]:长度为pos的数中,不包含49的,前一位不为4的有多少个:dp[pos][1]:长度为pos的数中,不包含49的,前一位为4的有多少个:d ...

  8. Jenkins自动化CI CD流水线之8--流水线自动化发布Java项目

    一.前提 插件:Maven Integration plugin 环境: maven.tomcat 用的博客系统代码: git clone https://github.com/b3log/solo. ...

  9. Go语言基础之11--Goroutine

    一.创建goroutine 1)在go语言中,每一个并发的执行单元叫做一个goroutine: 2)当一个程序启动时,其主函数即在一个单独的goroutine中运行,一般这个goroutine是主go ...

  10. md5,base64加密

    import java.security.MessageDigest; import org.apache.commons.codec.binary.Base64;import org.apache. ...