What is the best way for pooling httpclient?
If you refer to Apache
HTTP client then you may follow the steps below:
Initiate
Apache
HTTP client only once, since it is thread safe you can safely reuse it. If you use Spring then it should be safe to store it in the Spring Context as a Bean. See following link for the thread-safety.Despite the fact that HTTP client instance itself is not pooled (since you gonna use single instance of it) what you can do to increase the performance is configuring pooled connection manager on the HTTP client. See following link for the details.
(search for the 'Pooling connection manager' on that page) . The actual code should be something similar to the snippet bellow :
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("localhost", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 50);
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
Hope this helps .
Connection pooling in java using HttpClient
[assuming Java, and Apache's HttpClient]
Use a ThreadSafeClientConnManager. Pass a single global instance to the constructor of every HttpClient instance. I don't think there's any point in pooling the HttpClients themselves.
Related Topics
Sending Variable from Adapter to Activity
Best Way to Run Jar Application With Arguments from Env Variables
How to Find the Longest Word in a Given String
Set Drawable Size Programmatically
Spring MVC - Get Httpservletresponse Body
Way to Check If Two Collections Contain the Same Elements, Independent of Order
Value Annotation Not Working in Junit Test
Search Particular Column Value from CSV File Using Java
Spring Boot Controller Not Mapping
How to Find Out the Exact Requested Url in Servletrequest If That Url Contains Path Variables
How to Refresh a Stale Selenium Element If I Don't Have the Original Xpath
Calculating and Printing the Nth Prime Number
Testing Two Json Objects for Equality Ignoring Child Order in Java
How to Return Different Results When Calling the Same Mocked Method
Android - Running a Background Task Every 15 Minutes, Even When Application Is Not Running
Ora-00942 Sqlexception With Hibernate (Unable to Find a Table)