Apache HttpClient, which is said to be strong, can support a request with a peak of more than 70,000 a second. However, you need to be aware of some resource release and timeout issues.
Issue 1: Thread resources cannot be freed, resulting in insufficient memory, or the thread pool being used up. Problem code:
private static HttpClient httpclient = Httpclientbuilder.create (). build ();
private void Sendhttppost (String httpurl, Object body) {
try {
HttpPost request = new HttpPost (httpurl);
stringentity params = new Stringentity (new Objectmapper (). writevalueasstring (body), "UTF-8");
Request.addheader ("Content-type", "Application/json");
Request.setentity (params);
Httpclient.execute (Request); Problem code
} catch (Exception ex) {
logger.error ("Http post error. body={} ", body, ex);
}
Reason: The return value performed by Httpclient.execute () response not close, causing the thread to wait. WORKAROUND: Switch to closeablehttpclient and make sure that the response of Httpclient.execute () is finally turned off. The httpclient does not need close and can be reused. Corrected code:
private static HttpClient httpclient = Httpclientbuilder.create (). build ();
private static Closeablehttpclient httpclient = Httpclientbuilder.create (). build ();
private void Sendhttppost (String httpurl, Object body) {
try {
HttpPost request = new HttpPost (httpurl);
stringentity params = new Stringentity (new Objectmapper (). writevalueasstring (body), "UTF-8");
Request.addheader ("Content-type", "Application/json");
Request.setentity (params);
Httpclient.execute (request);
Try (closeablehttpresponse response = Httpclient.execute (Request)) {//response.close () is automatically invoked
//result = This.extractresultinfo (response);
} catch (Exception ex) {
logger.error ("Http post execute error.) body={} ", body, ex);
}
catch (Exception ex) {
logger.error ("Http post error. body={} ", body, ex);
}
Issue 2: If you use httpclient to access an external URL, sometimes a connection timeout or a response timeout occurs. If there is no timeout processing, it can also cause insufficient memory, or the thread pool is run out. Problem code: As above. Workaround: When creating httpclient, set several timeout times:
private static HttpClient httpclient = Httpclientbuilder.create (). build ();
private static Closeablehttpclient httpclient;//= Httpclientbuilder.create (). build (); No timeout handling
... static {
Requestconfig.builder Requestconfigbuilder = Requestconfig.custom ();
The client and server establish a connection to the timeout
requestconfigbuilder.setconnecttimeout (30000);
Obtain the Connected timeout
requestconfigbuilder.setconnectionrequesttimeout (30000) from the connection pool;
After the connection was established, the request did not respond to the timeout
requestconfigbuilder.setsockettimeout (30000);
Httpclientbuilder Clientbuilder = Httpclientbuilder.create ();
Clientbuilder.setdefaultrequestconfig (Requestconfigbuilder.build ());
Clientbuilder.setdefaultsocketconfig (Socketconfig.custom (). Setsotimeout (30000). build ()); After the connection was established, the request did not respond to the timeout
clientbuilder.setkeepalivestrategy (new Defaultconnectionkeepalivestrategy ());
HttpClient = Clientbuilder.build ();
}