connection pool shutdown错误修改

近几天在做一个项目时,遇到一个问题:前端如果在短时间内(2秒内)有多个相同的请求发到后台,后台再去通过restTemplate发送请求给其他服务的时候,会报错“connetion pool shutdown”。

通过查阅资料,大概是因为由于rest连接过于频繁,导致上一个获取不到连接。具体原因需要进一步分析。

stackoverflow:

1:https://stackoverflow.com/questions/29488106/java-lang-illegalstateexception-connection-pool-shut-down-while-using-spring-re

try to change your code like this:

HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(true).build();

the setConnectionManagerShared will defines the connection manager is to be shared by multiple client instances.

2:https://stackoverflow.com/questions/25889925/apache-poolinghttpclientconnectionmanager-throwing-illegal-state-exception

This behavior is due to a bug in HC 4.3. It has already been fixed in HC 4.4a1. As of 4.4 CloseableHttpClient#close should automatically shut down the connection pool only if exclusively owned by the client

解决方式:

第一步:将httpclient升级至4.4及以上版本,我的环境升级到了4.5.5.

第二步:httpclient配置共享连接池,供剩下打开的连接去请求。

client = HttpClients.custom()
                .setConnectionManager(ccm)
                .setDefaultCredentialsProvider(credentialsProvider)
                .setDefaultRequestConfig(defaultRequestConfig)
                .setConnectionManagerShared(true) //设置共享连接池
                .build();

 

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值