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; }; }
  • 相关阅读:
    Win10 anaconda python3.6 安装pcap
    跨平台 GUI可视化 网络调试工具
    参考文献相关概念
    linux压缩和解压缩命令大全[转]
    PowerShell 惠普打印机双面驱动自动设置已安装
    CentOS7 一键安装KMS服务【整理】
    pdf转word OCR
    CentOS7 MySQL
    PDF文件比对工具
    pdf文件中截取eps图片并压缩
  • 原文地址:https://www.cnblogs.com/tonyauto/p/11225985.html
Copyright © 2011-2022 走看看