zoukankan      html  css  js  c++  java
  • 针对HttpClient 重写 HttpRequestRetryHandler针对特定异常 增加重试




    调用方法:
    public static String doGet(String url) { try { RequestConfig defaultRequestConfig = RequestConfig.custom() .setSocketTimeout(200) .setConnectTimeout(200) .setConnectionRequestTimeout(200) .build(); CloseableHttpClient httpclient = HttpClients.custom() .setDefaultRequestConfig(defaultRequestConfig) .setRetryHandler(retryHandler()) .build(); return httpGetString(url, httpclient); } catch (IOException e) { logger.warn("doGetTimeoutUrl:" + url, e); return ""; } }



    /** * 从写重试handler * @return */ private static HttpRequestRetryHandler retryHandler(){ return (exception, executionCount, context) -> { logger.warn("tryRequest: " + executionCount,exception); if (executionCount >= 3) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return true; } if (exception instanceof UnknownHostException) { // Unknown host return true; } if (exception instanceof SSLException) { // SSL handshake exception return true; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; }; }
  • 相关阅读:
    sql当前行数据和之前行数据相加减循环处理
    Sql 查询库、表、列名的语句
    sql 特殊字符替换
    pandas 篇
    JAVA学习--面向对象的特征二:继承性
    JAVA学习--super使用
    JAVA学习--方法的参数传递
    JAVA学习--可变个数的形参的方法
    JAVA学习--面向对象思想的落地法则
    JAVA学习--方法的重载
  • 原文地址:https://www.cnblogs.com/tonyauto/p/11225985.html
Copyright © 2011-2022 走看看