org.apache.http.NoHttpResponseException故障排查

背景

最近公司的spark离线任务稳定有报错日志:

java.lang.RuntimeException: org.apache.http.NoHttpResponseException: demo.com:80 failed to respond

通过该报错日志我们可以判断是离线任务中使用的 httpclient 调用失败了,使用的组件版本如下:

  • httpclient-4.5.12
  • httpcore-4.4.13

问题分析

遇到问题首先是在对应的官网查找资料,而不是一头扎进各种博客论坛,这里引用官网的一段关于 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.

官网的描述很详细,即出现次异常的原因是,服务器负载过多,导致虽然服务器可以接受请求,但是没有足够多的工作线程来处理请求,因此直接断开了链接而不给客户端发送返回。出现这种情况后我们可以进行重试的处理。

解决方法

加入重试机制

可以通过如下代码实现自己的重试机制:

HttpRequestRetryHandler retryHandler = (exception, executionCount, context) -> {
                if (executionCount > 5) {
                    System.out.println("Maximum tries reached for client http pool ");
                    return false;
                }

                if (exception instanceof NoHttpResponseException     // NoHttpResponseException 重试
                        || exception instanceof ConnectTimeoutException // 连接超时重试
                        || exception instanceof SocketTimeoutException    // 响应超时重试
                ) {
                    System.out.println("http开始重试 " + executionCount + " call");
                    return true;
                }
                return false;
            };
            
httpClient = HttpClients.custom()
                    .setRetryHandler(retryHandler)
                    .build();

Reference

  1. 记HttpClient的NoHttpResponse问题
  2. 解决httpclient的NoHttpResponseException异常
  3. Exception handling
  4. get NoHttpResponseException for load testing
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值