Configure HttpClient correctly

本文针对使用Apache HttpClient遇到的性能问题进行了解析,并分享了如何通过调整连接池参数MaxTotal和MaxPerRoute来优化客户端配置,从而提高消息发送速度。

References:

[1] http://dev.bizo.com/2013/04/sensible-defaults-for-apache-httpclient.html

 We have hit an issue recently that the httpClient is too slow to send messages to the hosts. Finally, we found that we just use 

CloseableHttpClient httpClient = HttpClients.custom().build();

to create a default httpClient and not even configure it. We shoud have set at least MaxTotal and MaxPerRoute.

MaxTotal is the maximum total number of connections in the pool. MaxPerRoute is the maximum number of connections to a particular host. If the client attempts to make a request and either of these maximums have been reached, then by default the client will block until a connection is free. Unfortunately the default for MaxTotal is 20 and the default MaxPerRoute is only 2.

Finally we solved th issue by setting

CloseableHttpClient httpClient = HttpClients.custom()
        .setMaxConnTotal(threadpoolSize) // threadpoolSize = 100
        .setMaxConnPerRoute(threadpoolSize)
        .build();

  

转载于:https://www.cnblogs.com/codingforum/p/6099173.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值