• RestTemplate可以自定义重试次数


    /**
             * 重试处理
             * 默认是重试3次
             */
            //禁用重试(参数:retryCount、requestSentRetryEnabled)
            HttpRequestRetryHandler requestRetryHandler = new DefaultHttpRequestRetryHandler(0, false);
            //自定义重试策略
            HttpRequestRetryHandler myRetryHandler = new HttpRequestRetryHandler() {
     
                public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
                    //Do not retry if over max retry count
                    if (executionCount >= 3) {
                        return false;
                    }
                    //Timeout
                    if (exception instanceof InterruptedIOException) {
                        return false;
                    }
                    //Unknown host
                    if (exception instanceof UnknownHostException) {
                        return false;
                    }
                    //Connection refused
                    if (exception instanceof ConnectTimeoutException) {
                        return false;
                    }
                    //SSL handshake exception
                    if (exception instanceof SSLException) {
                        return false;
                    }
                    
                    HttpClientContext clientContext = HttpClientContext.adapt(context);
                    HttpRequest request = clientContext.getRequest();
                    boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
                    //Retry if the request is considered idempotent
                    //如果请求类型不是HttpEntityEnclosingRequest,被认为是幂等的,那么就重试
                    //HttpEntityEnclosingRequest指的是有请求体的request,比HttpRequest多一个Entity属性
                    //而常用的GET请求是没有请求体的,POST、PUT都是有请求体的
                    //Rest一般用GET请求获取数据,故幂等,POST用于新增数据,故不幂等
                    if (idempotent) {
                        return true;
                    }
                    
                    return false;
                }
            };
  • 相关阅读:
    人月神话阅读笔记01
    梦断代码阅读笔记03
    构建之法阅读笔记03
    构建之法阅读笔记02
    个人课程总结
    第十六周进度总结
    计算最长英语单词链
    第十五周进度总结
    浪潮之巅阅读笔记03
    冲刺2-10
  • 原文地址:https://www.cnblogs.com/qingmuchuanqi48/p/14170856.html
Copyright © 2020-2023  润新知