Apache httpclient Resource Release, request timeout, resulting in a thread pool running out of memory _apache

Source: Internet
Author: User
Tags http post
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 ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.