HttpComponents HttpClient连接池(2)-连接的申请

上一篇文章里我们主要介绍了 httpclient 连接池的关键类和数据结构,在这里我们主要介绍http连接的申请和释放。

http连接的申请

http 连接的申请主要调用上一篇文章 Cpool 对象(在父类AbstractConnPool)的 lease() 方法,该方法会返回 Future<CPoolEntry> 对象,该对象调用 get 方法得到 CPoolEntry ,而在 get 方法里又调用 CPool 实例的 getPoolEntryBlocking() 方法,所以该方法是核心,代码如下:

private E getPoolEntryBlocking(final T route, final Object state, final long timeout, final TimeUnit timeUnit, final Future<E> future) throws IOException, InterruptedException, TimeoutException {
        Date deadline = null;
        if (timeout > 0) {
            deadline = new Date (System.currentTimeMillis() + timeUnit.toMillis(timeout));
        }
        this.lock.lock();
        try {
            final RouteSpecificPool<T, C, E> pool = getPool(route);
            E entry;
            for (;;) {
                Asserts.check(!this.isShutDown, "Connection pool shut down");
                for (;;) {
                    entry = pool.getFree(state);
                    if (entry == null) {
                        break;
                    }
                    if (entry.isExpired(System.currentTimeMillis())) {
                        entry.close();
                    }
                    if (entry
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Spring Boot中使用HttpClient时,可以使用连接池来提高性能和可靠性。连接池可以管理多个HTTP连接,重用已经建立的连接,从而避免了每次请求都需要重新建立连接的开销。 以下是在Spring Boot中使用HttpClient连接池的步骤: 1. 首先,需要在pom.xml文件中添加以下依赖: ``` <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> ``` 2. 接下来,在application.properties文件中配置HttpClient连接池的参数,如下所示: ``` # HttpClient连接池最大连接数 http.maxTotal=200 # HttpClient连接池每个路由的最大连接数 http.maxPerRoute=20 # HttpClient连接池连接超时时间 http.connectionTimeout=5000 # HttpClient连接池请求超时时间 http.requestTimeout=5000 # HttpClient连接池等待数据超时时间 http.socketTimeout=5000 ``` 3. 然后,在Spring Boot的配置类中创建HttpClient连接池对象,并将其注入到需要使用的类中,如下所示: ``` @Configuration public class HttpClientConfig { @Value("${http.maxTotal}") private int maxTotal; @Value("${http.maxPerRoute}") private int maxPerRoute; @Value("${http.connectionTimeout}") private int connectionTimeout; @Value("${http.requestTimeout}") private int requestTimeout; @Value("${http.socketTimeout}") private int socketTimeout; @Bean public CloseableHttpClient httpClient() { PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(maxTotal); connectionManager.setDefaultMaxPerRoute(maxPerRoute); RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(connectionTimeout) .setConnectionRequestTimeout(requestTimeout) .setSocketTimeout(socketTimeout) .build(); return HttpClients.custom() .setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig) .build(); } } ``` 4. 最后,在需要使用HttpClient的类中注入HttpClient对象,如下所示: ``` @Service public class MyService { @Autowired private CloseableHttpClient httpClient; public void doRequest() throws Exception { HttpGet httpGet = new HttpGet("http://www.example.com"); CloseableHttpResponse response = httpClient.execute(httpGet); // 处理响应 response.close(); } } ``` 这样,就可以使用HttpClient连接池来管理HTTP连接,提高性能和可靠性。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值