Guava retryer优雅的实现接口重调机制
2018-03-22 19:47
369 查看
今天遇到一个问题,在调用第三方接口的时候,会遇到断网、获取不到数据等各种异常信息,需要对该信息进行分析,然后进行接口的重新调用,通过网上找资料,使用的谷歌的一个插件,使用Guava retryer进行接口的重调,具体见代码。package com.yooyii.qdp.test;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.base.Predicates;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.requests.Requests;
@Slf4j
public class GuavaRetry {
static Retryer<String> retryer;
static int count = 0;
static {
retryer = RetryerBuilder.<String>newBuilder().retryIfResult(Predicates.equalTo("")) // 返回false时重试
.withWaitStrategy(WaitStrategies.fixedWait(1000, TimeUnit.MILLISECONDS)) // 1000ms后重试
.withStopStrategy(StopStrategies.stopAfterAttempt(20)) // 重试20次后停止
.build();
}
public static void main(String[] args) throws InterruptedException {
sendSMS();
}
public static String sendSMS() {
try {
return retryer.call(new Callable<String>() {
@Override
public String call() thro
4000
ws Exception {
//
count++;
//
String res = "";
try {
res = Requests.get("https://www.baidu.com/").send().readToText();
} catch (Exception e) {
log.debug(e.getMessage());
}
log.debug(count + "--" + String.valueOf(res.length()));
//
return res;
}
});
} catch (Exception e) {
return "";
}
}
}
这个方法可以修改为一个通用的工具类,进行多数接口的重调机制判断。
大家可以参考以下两个人的:
https://www.cnblogs.com/jianzh5/p/6651799.html
http://blog.csdn.net/aitangyong/article/details/53840719
有很多可以借鉴的地方,
https://www.cnblogs.com/onlychang92/p/5203139.html
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import com.github.rholder.retry.Retryer;
import com.github.rholder.retry.RetryerBuilder;
import com.github.rholder.retry.StopStrategies;
import com.github.rholder.retry.WaitStrategies;
import com.google.common.base.Predicates;
import lombok.extern.slf4j.Slf4j;
import net.dongliu.requests.Requests;
@Slf4j
public class GuavaRetry {
static Retryer<String> retryer;
static int count = 0;
static {
retryer = RetryerBuilder.<String>newBuilder().retryIfResult(Predicates.equalTo("")) // 返回false时重试
.withWaitStrategy(WaitStrategies.fixedWait(1000, TimeUnit.MILLISECONDS)) // 1000ms后重试
.withStopStrategy(StopStrategies.stopAfterAttempt(20)) // 重试20次后停止
.build();
}
public static void main(String[] args) throws InterruptedException {
sendSMS();
}
public static String sendSMS() {
try {
return retryer.call(new Callable<String>() {
@Override
public String call() thro
4000
ws Exception {
//
count++;
//
String res = "";
try {
res = Requests.get("https://www.baidu.com/").send().readToText();
} catch (Exception e) {
log.debug(e.getMessage());
}
log.debug(count + "--" + String.valueOf(res.length()));
//
return res;
}
});
} catch (Exception e) {
return "";
}
}
}
这个方法可以修改为一个通用的工具类,进行多数接口的重调机制判断。
大家可以参考以下两个人的:
https://www.cnblogs.com/jianzh5/p/6651799.html
http://blog.csdn.net/aitangyong/article/details/53840719
有很多可以借鉴的地方,
https://www.cnblogs.com/onlychang92/p/5203139.html
相关文章推荐
- 使用Guava retryer优雅的实现接口重调机制
- 使用guava-retry优雅的实现接口重试
- 优雅实现INotifyPropertyChanged接口——利用Lambda表达式
- Qomolangma实现篇(七):Qomo的接口机制
- 接口实现的继承机制
- Delphi 的接口机制——接口操作的编译器实现过程(1)
- Google Guava 缓存实现接口的限流
- Qomolangma实现篇(七):Qomo的接口机制
- RecyclerView3-面向接口优雅地实现多类型列表
- 由service层接口有什么用?引申到基于JDK原生和CGLIB动态代理实现spring事务管理的机制的思考
- Delphi 的接口机制——接口操作的编译器实现过程(2)
- Qomolangma实现篇(七):Qomo的接口机制
- 反射机制实现接口多态
- Delphi 的接口机制——接口操作的编译器实现过程(1)
- Java中的接口interface实现、与继承extends机制。
- Guava类库中的Multisets的实现机制源码分析
- (转)优雅的微博时间显示接口(Java实现)
- 如何使用Lock接口来实现等待/通知机制
- Delphi 的接口机制——接口操作的编译器实现过程(2)
- 安卓自定义控件(View)(二)之接口回调机制实现TopBar的点击事件(从新手角度出发)