How to prevent full connections of httpClient

Source: Internet
Author: User

Due to the many exceptions recently reported in my own project: org. apache. http. conn. connectionPoolTimeoutException: Timeout waiting for connection. So I have studied the connection information and summarized the following points: 1. Set the maximum number of connections static {httpParams = new BasicHttpParams (); // set the maximum number of connections ConnManagerParams. setMaxTotalConnections (httpParams, 600); // sets the maximum wait time for obtaining the connection. setTimeout (httpParams, 60000); // sets the maximum number of connections for each route. ConnPerRouteBean connPerRoute = new ConnPerRou TeBean (300); ConnManagerParams. setMaxConnectionsPerRoute (httpParams, connPerRoute); // you can specify the connection timeout value for HttpConnectionParams. setConnectionTimeout (httpParams, 10000); // sets the read timeout value for HttpConnectionParams. setSoTimeout (httpParams, 10000); 2. Close the public static String readNet (String urlPath) {StringBuffer sb = new StringBuffer (); HttpClient client = null; inputStream in = null; InputStreamReader isr = null; HttpGet = new HttpGet (); try {client = HttpConnectionManager. getHttpClient (); get. setURI (new URI (urlPath); HttpResponse response = client.exe cute (get); if (response. getStatusLine (). getStatusCode ()! = 200) {get. abort (); return null;} HttpEntity entity = response. getEntity (); if (entity! = Null) {in = entity. getContent ();......} return sb. toString ();} catch (Exception e) {get. abort (); e. printStackTrace (); return null;} finally {if (isr! = Null) {try {isr. close () ;}catch (IOException e) {e. printStackTrace () ;}} if (in! = Null) {try {in. close ();} catch (IOException e) {e. printStackTrace () ;}}} 3. Set tomcat Connections 1. Modify the maximum number of connections at/opt/probe/patrol/tomcat_plan_5535/conf/server. <Connector port = "80" maxHttpHeaderSize = "8192" maxThreads = "600" minSpareThreads = "25" maxSpareThreads = "75" enableLookups = "false" redirectPort = "8443" acceptCount = "100" connectionTimeout = "20000" disableUploadTimeout = "true" URIEncoding = "UTF-8"/>

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.