transferred from: http://luan.iteye.com/blog/1820054I/O exception (java.net.SocketException) caught when processing request:connect
Check out the HttpClient official exception documentation (http://hc.apache.org/httpclient-3.x/exception-handling.html) to see the following paragraph:
In some circumstances, usually when under heavy load, the Web server is able to receive requests but unable to process them. A lack of sufficient resources like worker threads is a good example. This could cause the server to drop the connection to the client without giving any response. HttpClient throws nohttpresponseexception when it encounters such a condition. In the most cases it's safe to retry a method, that failed with nohttpresponseexception.
In some cases, when under heavy load, the Web server may be able to receive requests, but cannot process them. Lack of sufficient resources, such as worker threads, may cause the server disconnects the client to give no response. When it encounters such a condition httpclient will throw nohttpresponseexception. This exception is due to a server-side overload that denies accepting requests (no longer responding).
The foreigner has an article, very good describes the similar code the performance hidden Danger: "The httpclient easy to overlook the detail--Connection closes"
1. English Original: http://www.codeweblog.com/httpclient-s-easy-to-overlook-the-details-the-connection-is-closed/
2, Chinese translation: http://www.iteye.com/topic/234759
General: The implementation of an HTTP interface is not a difficult thing, but how to make such an HTTP interface under high pressure (large data volume in a short time) also has a stable and good performance, it is not only the HTTP server needs to be well designed and optimized, and the HTTP client side also needs to be very cautious and aware of some code details. Otherwise, it is very likely because of (the two or one-sided) code or configuration in the presence of performance problems, in the hardware and software environment with a number of "supernatural" failure.
"HTTP protocol Knowledge
To make it easier for the reader to understand, the basics of HTTP protocols that are closely related to HTTP performance and that are often not delved into by engineers are described first.
One, what is HTTP Keepaliv e-mail
The HTTP keepalive is what is commonly called a long connection. The keepalive is the server side that maintains the connection for the same client for a period of time (not shutting down immediately) so that more subsequent requests from this client continue to take advantage of this connection until the connection times out.
There is support for keepalive in both the HTTP1.0 and HTTP1.1 agreements. Where HTTP1.0 needs to be supported by adding "Connect: Keep active" in the request header, and HTTP1.1 is supported by default.
More elaboration of this attribute:
1, the next request is issued when the requested response is received by the client before completion. Therefore, you need to write the response of the previous request to the client before it can be triggered.
2, the HTTP protocol is based on the TCP protocol, so the server side and the client may close the connection. KeepAlive just indicates a server-side connectivity optimization strategy, and the client can be actively shut down (not to use).
Second, the advantages and disadvantages of keepalive
The benefit of keepalive is that it can reduce the overhead of HTTP connections and improve performance. For example, the same page, if there are many embedded images, JS, CSS and other requests, you can take advantage of this new, using a small number of connections (ie is generally 2) faster download, so that the page display faster.
The disadvantages of qeepalive are:
If a large number of different clients simultaneously (or instantaneously) request the server side, and each client's long-term use of the connection (such as: Do not shut down and connectiontimeout set too long) or the server side is not fast failure connection (keepalivetimeout parameter settings too large), May quickly fill up the server connection resources, causing more requests to be queued or rejected or the server down.
Summary: The browser as an HTTP client, full and good use of the HTTP protocol keepalive, let us browse more quickly, and our self-written HTTP client program in the KeepAlive feature (server is turned on), When a HTTP interface needs to be accessed with a high amount of data, each request should be closed as soon as possible (the key recommendation) or the appropriate number of requests on the same connection several times (not recommended).
"Policies for high-performance HTTP Applications"
So, when we need a high-performance HTTP interface application:
1, server-side: Turn off the keepalive feature.
2, server-side: It is best to directly support the HTTP protocol (pay attention to post, do not get), rather than any packaged protocol, such as: Hessian/soap.
3, server-side: In a request, it is best to design: Support multiple instructions batch, to save the number of connections.
4, Server side: The processing of requests should be as fast as possible (for example, within 150ms).
5. Client: In code, the connection should be actively closed after all requests in the same client instance are completed (without having to set the client's connectiontimeout parameters beforehand).
6, the client: If the server does not shut down keepalive, in the same client instance can make more than one request (the total time should be slightly less than the server keepalivetimeout parameters). This method requires precise operation and is not recommended.
Finally, in the interface design, for some asynchronous operation, try not to design as a unilateral polling mode (reduce the number of unnecessary requests), should be designed to be the caller's asynchronous result callback mode.
"Some optimization details"
On the server side, we generally choose a combination of apache+tomcat/jboss. For JBoss configuration and optimization, refer to JBoss website.
The main thing is about Apache optimization, and we recommend reading two articles:
1, Apache performance optimization: http://www.aliwo.net:8080/2009/12/apache/
2. Maintain proper use of the active Apache configuration: http://www.net527.cn/a/caozuoxitong/Linux/5283.html
In the Java code of the client, we use the HttpClient Toolkit most often.
There are some details to note:
1, after each HttpClient instance sends the request, (if no longer uses) should close the connection in time.
The simplest way is to send (connect: Close) in the HTTP request header, which instructs the server to close the current connection.
The code is as follows:
Method.setrequestheader ("Connectio", "close");//change this and find that performance is poor
2, can be designed as a singleton mode: No need to create a httpclient instance each time, can send requests multiple times (Request header settings See the first
HttpClient I/O exception (java.net.SocketException) caught when processing request:connect