解决httpclient的NoHttpResponseException异常
来源:互联网 发布:南通大学网络 编辑:程序博客网 时间:2024/06/07 11:07
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.
意思就是 当服务器端由于负载过大等情况发生时,可能会导致在收到请求后无法处理(比如没有足够的线程资源),会直接丢弃链接而不进行处理。此时客户端就回报错: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();
参考资料:
官方文档
httpclient4.4 出现NoHttpResponseException的异常解决
[get NoHttpResponseException for load testing
](https://stackoverflow.com/questions/10570672/get-nohttpresponseexception-for-load-testing/10680629#10680629)
阅读全文
1 0
- 解决httpclient的NoHttpResponseException异常
- NoHttpResponseException
- solr SearchHandler扩展,解决httpclient连接问题以及对连接异常的容错处理
- 解决Httpclient.exec(method)不执行或抛出异常的方法
- ClassNotFoundException: NoHttpResponseException
- HttpClient的CircularRedirectException异常原因及解决办法
- HttpClient的CircularRedirectException异常原因及解决办法
- HttpClient的CircularRedirectException异常原因及解决办法
- 解决httpClient
- 使用HttpClient setConnectionTimeout无效的解决
- 解决Apache HttpClient Cookie rejected的问题
- UnsupportedClassVersionError异常的解决
- LogicalConnection异常的解决
- 异常的解决
- HttpClient异常处理手册
- HttpClient 异常出现不关闭引发的血案
- HttpClient的”javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated”异常
- JMeter报错:NoHTTPResponseException
- Python文件头模板
- php黑魔法
- ubuntu系统下wireshark普通用户抓包设置
- 229. Majority Element II
- Java反射机制
- 解决httpclient的NoHttpResponseException异常
- request & response
- matplotlib——3D绘图教程
- Java新增修改保存在数据库乱码问题
- C#as关键字
- 3.C++进化后的 const
- jquery validate
- Word表格外的第一个空行如何删除
- 异步加载