Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use HttpClient connection Pool in HttpComponents

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Today, I will talk to you about how to use HttpClient connection pool in HttpComponents. Many people may not know much about it. In order to make you understand better, the editor has summarized the following for you. I hope you can get something according to this article.

Generally consider the following points when using httpclient connection pooling:

Request a connection timeout from the connection pool

The timeout of connection establishment, that is, the timeout of 3-way handshake by socket to establish a connection

Connection timeout, i.e. socket read and write timeout

Set the maximum number of redirect

Whether to turn on availability check

Maximum number of connections in the global connection pool

Maximum number of connections in the individual route connection pool

Number of retries requested

Set the certificate trust policy and cn host name verification policy for ssl requests

Enable cleaning of idle connections and expired connections, and set the duration of idle connections

Whether to reuse pooled objects and use long connections

We set up the above items with the following code:

RequestConfig requestConfig = RequestConfig.custom () .setConnectionRequestTimeout (10000) / / sets the timeout for connection pool requests. Default-1 is unlimited. SetConnectTimeout (5000) / sets the timeout for socket to make a 3-way handshake to establish a connection. SetSocketTimeout (8000) / / sets the connection timeout That is, the socket read and write timeout .setMaxRedirects (50) / sets the maximum number of redirect. By default, availability check is enabled for setStaleConnectionCheckEnabled (Boolean.TRUE) / /, and .build () is not enabled by default.

CloseableHttpClient htttpClient = HttpClients.custom () .setDefaultRequestConfig (requestConfig) .setMaxConnPerRoute (50) / sets the maximum number of connections in the individual route connection pool, defaults to 2.setMaxConnTotal / / sets the maximum number of connections in the global connection pool, and defaults to 20.setConnectionTimeToLive (- 1, TimeUnit.MICROSECONDS) / / sets the connection lifetime in the connection pool The default-1 stands for unlimited survival, and after the connection is used, it is determined by response header "Keep-Alive: timeout". .evictIdleConnections (60000, TimeUnit.MILLISECONDS) / / enable the air raid connection cleanup thread, set the maximum idle time of the connection pool, and the sleep time of the connection cleanup thread. The default is 10 seconds. EvictExpiredConnections () / starts the expired connection cleanup thread. The expiration time defaults to-1, which is determined by response header "Keep-Alive: timeout" after the connection is used. / / .setRetryHandler (retryHandler) / / set retry policy. Default is 3 retries / / .setSSLContext (sslContext) / / set ssl request context / / .setSSLHostnameVerifier (hostnameVerifier) / / set ssl certificate cn hostname verification policy, default to verify cn hostname .build () If you want to reuse pooled objects and maintain persistent connections, be sure to call the static methods toByteArray (), toString (), consume (), consumeQuietly (), and so on, in the EntityUtils class. If you don't want to reuse pooled objects and don't want to use persistent connections, call the close () method of CloseableHttpResponse. In addition, we often use Spring's RestTemplate to send https requests, and for RestTemplate, we also integrate Apache HttpComponents HttpClient components, so consider the above items settings when using RestTemplate. After reading the above, do you have any further understanding of how to use HttpClient connection pooling in HttpComponents? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report