guava-retrying重试工具库: Retryer.call()使用注意事项
2016-12-27 14:17
507 查看
前面我们介绍了如何通过RetryerBuilder构造一个Retryer对象,现在我们学习下如何使用 Retryer.call()。这个API很重要,对于这个方法的入参、返回值、可能抛出的异常要了解清楚。
入参:很简单,就是一个实现了业务逻辑的Callable对象;返回值:某次尝试成功后,Callable的返回值。我们主要关注下可能的异常:RetryException和ExecutionException。
ExecutionException产生:传入的Callable执行过程中产生了异常,但是我们在构建Retryer对象的时候并没有考虑这种情况,就会抛出这个异常。抛出这异常,也就意味着重试终止。
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
public static void main(String[] args) throws Exception {
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfExceptionOfType(IOException.class)
.withWaitStrategy(WaitStrategies.fixedWait(1,TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(5))
.build();
System.out.println("begin..." + df.format(new Date()));
try {
retryer.call(buildTask());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end..." + df.format(new Date()));
}
private static Callable<Boolean> buildTask() {
return new Callable<Boolean>() {
private int i = 0;
@Override
public Boolean call() throws Exception {
System.out.println("called");
i++;
if (i == 1) {
throw new IOException();
} else {
throw new NullPointerException();
}
}
};
}
RetryException就很简单了,当所有重试介绍后,依然不能成功,那么就会抛这异常。
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
public static void main(String[] args) throws Exception {
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException()
.withWaitStrategy(WaitStrategies.fixedWait(1,TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(5))
.build();
System.out.println("begin..." + df.format(new Date()));
try {
retryer.call(buildTask());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end..." + df.format(new Date()));
}
private static Callable<Boolean> buildTask() {
return new Callable<Boolean>() {
private int i = 0;
@Override
public Boolean call() throws Exception {
System.out.println("called");
i++;
if (i == 1) {
throw new IOException();
} else {
throw new NullPointerException();
}
}
};
}
入参:很简单,就是一个实现了业务逻辑的Callable对象;返回值:某次尝试成功后,Callable的返回值。我们主要关注下可能的异常:RetryException和ExecutionException。
ExecutionException产生:传入的Callable执行过程中产生了异常,但是我们在构建Retryer对象的时候并没有考虑这种情况,就会抛出这个异常。抛出这异常,也就意味着重试终止。
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
public static void main(String[] args) throws Exception {
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfExceptionOfType(IOException.class)
.withWaitStrategy(WaitStrategies.fixedWait(1,TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(5))
.build();
System.out.println("begin..." + df.format(new Date()));
try {
retryer.call(buildTask());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end..." + df.format(new Date()));
}
private static Callable<Boolean> buildTask() {
return new Callable<Boolean>() {
private int i = 0;
@Override
public Boolean call() throws Exception {
System.out.println("called");
i++;
if (i == 1) {
throw new IOException();
} else {
throw new NullPointerException();
}
}
};
}
RetryException就很简单了,当所有重试介绍后,依然不能成功,那么就会抛这异常。
private static SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss,SSS");
public static void main(String[] args) throws Exception {
Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
.retryIfException()
.withWaitStrategy(WaitStrategies.fixedWait(1,TimeUnit.SECONDS))
.withStopStrategy(StopStrategies.stopAfterAttempt(5))
.build();
System.out.println("begin..." + df.format(new Date()));
try {
retryer.call(buildTask());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("end..." + df.format(new Date()));
}
private static Callable<Boolean> buildTask() {
return new Callable<Boolean>() {
private int i = 0;
@Override
public Boolean call() throws Exception {
System.out.println("called");
i++;
if (i == 1) {
throw new IOException();
} else {
throw new NullPointerException();
}
}
};
}
相关文章推荐
- guava-retrying,重试工具使用
- 经验交流:Windows 7软改激活工具使用教程及注意事项
- 自然语言处理工具(Stanford-Parser)使用注意事项
- php中call_user_func函数使用注意事项
- facebook的Android调试工具Stetho介绍和使用注意事项
- iOS开发-开源类库工具Cocoapods使用注意事项及错误解决办法
- java生成exe工具之exe4j.exe(Java Exe Maker)的使用和注意事项
- 嵌入式开发中网络性能测试工具iperf的使用方法和注意事项
- 使用admodify工具修改用户主目录时的注意事项
- 关于网格比较工具metro使用的几点注意事项
- zip,collections.defaultdict lambda,csv.DictReader用法简介,及subprocess.call注意事项(需要使用chmod将待执行的文件变为可执行)
- AjaxControlToolkit工具的使用实例及注意事项
- 嵌入式开发中网络性能测试工具iperf的使用方法和注意事项
- 使用Git 作为项目管理工具开发时的方式和注意事项
- TC工具的使用注意事项
- MTK META工具的使用和注意事项(MT6252)
- libgdx引擎图片整合工具TexturePacker-Gui的使用注意事项
- django 数据库增强工具 south 的安装使用和注意事项。。。
- ngrok工具的介绍与使用,以及使用此工具时注意的事项
- Oracle的LOGMNR工具的使用及注意事项