okhttp3 自定义超时重试

需求描述

对一个url地址发起请求后,设置超时时长为3秒,若超时时重试3次后仍然超时视为宕机并返回自定义的code信息。

解决

这是我入职公司的第一个任务,上级说简单,不过我搞了有两天了,因为在网上搜的自定义超时重试次数看不懂。。后来还是问强哥,他给了我一篇博客链接,然后我才有了思路。

  1. 发起post请求
	        OkHttpClient client = new OkHttpClient
                .Builder()
                .connectTimeout(okhttpTimeOut, TimeUnit.SECONDS) // 设置超时时长,单位秒
                .addInterceptor(new OkhttpInterceptor(maxRetry)) //过滤器,设置最大重试次数
                .retryOnConnectionFailure(false) //不自动重连
                .build();

  1. 过滤器,通过递归达到超时重试的目的

    这里需要说明:原本以为超时后直接拿到response可以判断超时信息,简简单单做个循环就好。但是!超时后直接抛出异常 ConnectException,我原来的笨方法就是捕获这个异常,3次重试就做三层的try catch,代码写出来傻傻的。

    还好,递归可以解决这个问题,本质上没什么变化,但是用上了递归就是爽!

@Slf4j
class OkhttpInterceptor implements Interceptor{

    private int maxRentry;// 最大重试次数

    public OkhttpInterceptor(int maxRentry){
        this.maxRentry=maxRentry;
    }

    @NotNull
    @Override
    public Response intercept(@NotNull Chain chain) throws IOException {
        /* 递归 4次下发请求,如果仍然失败 则返回 null ,但是 intercept must not return null.
         * 返回 null 会报 IllegalStateException 异常
         * */
        return retry(chain,0);//这个递归真的很🐂
    }
    Response retry(Chain chain,int retryCent){
        Request request = chain.request();
        Response response =  null;
        try{
            log.info("messageId: "+messageId+"  第"+(retryCent+1)+"次执行下发NEF请求.");
            response = chain.proceed(request);
        }catch (Exception e){
            if ( maxRentry > retryCent ){
                return retry(chain,retryCent+1);
            }
        }finally {
            return response;
        }
    }
}
  1. 执行请求,捕获IllegalStateException异常
try{
    response = client.newCall(request).execute();//执行请求
    /*如果response可以正常返回,说明能连接上,接着写业务逻辑即可*/
}catch(IllegalStateException e){
    //捕获异常,intercept must not return null.过滤器不能返回null
            e.printStackTrace();
            log.info("重试3次后访问超时.");
            resMap.put("code","404");//在这里可以自定义返回信息
            return resMap;
}

posted on 2021-09-27 21:28  快乐撸代码  阅读(2292)  评论(0编辑  收藏  举报

导航