热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

怎样解决httpclient中出现NoHttpResponseException异常

转载地址:https:www.2cto.comkf201710688548.htmlhttpclient版本:4.5.2在项目实际运行中,偶发异常:org.

转载地址: https://www.2cto.com/kf/201710/688548.html


httpclient版本:4.5.2

在项目实际运行中,偶发异常:org.apache.http.NoHttpResponseException。

官网解释是:

In some circumstances, usually when under heavy load, the web server may be able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This may cause the server to drop the connection to the client without giving any response. HttpClient throws NoHttpResponseException when it encounters such a condition. In most cases it is safe to retry a method that failed with NoHttpResponseException.


修改方案:

CloseableHttpClient httpClient = HttpClients.custom()
     .setDefaultRequestConfig(config)
     .setConnectionManager(getPoolManager())
 
     //不使用这种方式,不方便看日志,使用下面自定义的retry
//  .setRetryHandler(new DefaultHttpRequestRetryHandler(3,true))
 
     .setRetryHandler((exception, executionCount, context) -> {
         if (executionCount > 3 ) {
             log.warn( "Maximum tries reached for client http pool " );
             return false ;
         }
 
         if (exception instanceof NoHttpResponseException     //NoHttpResponseException 重试
                 || exception instanceof ConnectTimeoutException //连接超时重试
//              || exception instanceof SocketTimeoutException    //响应超时不重试,避免造成业务数据不一致
                 ) {
             log.warn( "NoHttpResponseException on " + executionCount + " call" );
             return true ;
         }
         return false ;
     })
     .build();


推荐阅读
author-avatar
wei5xiao5zou6bian6tian
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有