OkHttp实现延时重试
2017-09-30 18:04
141 查看
场景分析
很多项目由于有callback,且失败重试和重定向需求,因此需要实现OkHttp失败重试解决方案
package com.gomefinance.esign.httpretry; import lombok.extern.slf4j.Slf4j; import okhttp3.Interceptor; import okhttp3.Request; import okhttp3.Response; import java.io.IOException; import java.io.InterruptedIOException; import java.util.List; /** * User: Administrator * Date: 2017/9/19 * Description: */ @Slf4j public class MyOkHttpRetryInterceptor implements Interceptor { public int executionCount;//最大重试次数 private long retryInterval;//重试的间隔 MyOkHttpRetryInterceptor(Builder builder) { this.executionCount = builder.executionCount; this.retryInterval = builder.retryInterval; } @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); Response response = doRequest(chain, request); int retryNum = 0; while ((response == null || !response.isSuccessful()) && retryNum <= executionCount) { log.info("intercept Request is not successful - {}",retryNum); final long nextInterval = getRetryInterval(); try { log.info("Wait for {}",nextInterval); Thread.sleep(nextInterval); } catch (final InterruptedException e) { Thread.currentThread().interrupt(); throw new InterruptedIOException(); } retryNum++; // retry the request response = doRequest(chain, request); } return response; } private Response doRequest(Chain chain, Request request) { Response response = null; try { response = chain.proceed(request); } catch (Exception e) { } return response; } /** * retry间隔时间 */ public long getRetryInterval() { return this.retryInterval; } public static final class Builder { private int executionCount; private long retryInterval; public Builder() { executionCount = 3; retryInterval = 1000; } public MyOkHttpRetryInterceptor.Builder executionCount(int executionCount){ this.executionCount =executionCount; return this; } public MyOkHttpRetryInterceptor.Builder retryInterval(long retryInterval){ this.retryInterval =retryInterval; return this; } public MyOkHttpRetryInterceptor build() { return new MyOkHttpRetryInterceptor(this); } } }
实现方法
MyOkHttpRetryInterceptor myOkHttpRetryInterceptor = new MyOkHttpRetryInterceptor.Builder() .executionCount(3) .retryInterval(1000) .build(); new OkHttpClient.Builder() .retryOnConnectionFailure(true) .addInterceptor(myOkHttpRetryInterceptor) .connectionPool(new ConnectionPool()) .connectTimeout(3000, TimeUnit.MILLISECONDS) .readTimeout(10000, TimeUnit.MILLISECONDS) .build();
实现请求失败切换IP重试的方案
相关文章推荐
- 使用Guava retryer实现延时重试机制
- RabbitMQ发布订阅实战-实现延时重试队列
- spring boot Rabbitmq集成,延时消息队列实现
- javascript实现延时显示提示框效果
- PHP如何实现订单的延时处理详解
- TASKCTL作业执行错误,实现自动重试方法
- js 实现延时或定时 运行某个函数 -- setTimeout
- RxJava retryWhen操作符实现错误重试机制
- linux驱动中实现延时
- js实现网页图片延时加载 提升网页打开速度
- 利用okhttp框架实现包含验证码的用户登录,保持session操作(下)
- Rxjava+Retrofit+okhttp+mvp实现
- Android使用okhttpfinal+gson实现网络请求
- 用HTML/JS/PHP方式实现页面延时跳转的简单实例
- 优雅设计封装基于Okhttp3的网络框架(二):多线程下载功能原理设计 及 简单实现
- OkHttp 3.7源码分析(二)——拦截器&一个实际网络请求的实现
- 【Android】- OkHttp实现断点续传
- jQuery实现带延时功能的水平多级菜单效果【附demo源码下载】
- Jquery实现图片的预加载与延时加载
- 一段实现页面上的图片延时加载的js代码