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. B - EXCEL排序(sort+结构体)

    Excel可以对一组纪录按任意指定列排序.现请你编写程序实现类似功能. Input测试输入包含若干测试用例.每个测试用例的第1行包含两个整数 N (<=100000) 和 C,其中 N 是纪录的 ...

  2. Spring MVC零配置(全注解)(版本5.0.7)

    // 核心配置类 package spittr.config; import org.springframework.web.servlet.support.AbstractAnnotationCon ...

  3. Redis源码阅读---连接建立

    对于并发请求很高的生产环境,单个Redis满足不了性能要求,通常都会配置Redis集群来提高服务性能.3.0之后的Redis支持了集群模式. Redis官方提供的集群功能是无中心的,命令请求可以发送到 ...

  4. Iterator 遍历器

    1.遍历器(Iterator)是一种接口,为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署Iterator接口,就可以完成遍历操作(即依次处理该数据结构的所有成员). 2.Iterator ...

  5. 大众点评CAT开源监控系统剖析

    参考文档: 大众点评的实时监控系统分析(一) CAT_source_analyze 透过CAT,来看分布式实时监控系统的设计与实现 深度剖析开源分布式监控CAT [分布式监控CAT] Client端源 ...

  6. (Python OpenGL)【3】着色器 PyOpenGL

    (Python OpenGL)现在开始我们使用着色器来进行渲染.着色器是目前做3D图形最流行的方式. OpenGL的渲染管线流程: 数据传输到OpenGL—>顶点处理器—>细分着色—> ...

  7. 梯度下降&随机梯度下降&批梯度下降

    梯度下降法 ​ 下面的h(x)是要拟合的函数,J(θ)损失函数,theta是参数,要迭代求解的值,theta求解出来了那最终要拟合的函数h(θ)就出来了.其中m是训练集的记录条数,j是参数的个数. 梯 ...

  8. C# 添加vertical 属性上下边框消失问题

    点击这里的曲别针就好了.... 自定义控件主题..... #学习地址: http://www.cnblogs.com/anding/p/4993655.html

  9. JavaScript权威指南--闭包讲解摘记

    不积跬步无以至千里,不积小流无以成江河. 关于闭包的解释,在<JavaScript权威指南>中讲的很透彻了.今天看了书中的一个段讲解,更加深了对闭包的理解,特此记下,以备查阅. 在同一个作 ...

  10. 关于c语言的位运算&,|,^(看懂汉字的都能看懂)

    其中|,&可以当作逻辑运算符,当|,&当成逻辑运算符时,与||,&&的用法基本相似,&&,||运算时会当前面的表达式能够决定整个表达式,则不进行对后面的 ...