Skip to main content

Reusing socket connections with HttpURLConnection

8 replies [Last post]
byhisdeeds
Offline
Joined: 2006-01-06
Points: 0

Does anybody know if I have a server listening for HTTP requests, and my server does not close the socket connection between requests, and I set the HttpURLConnection setRequestHeader("Connection", "keep-alive") on the client side, that multiple loops of creating new client side HttpUrlConnections, and writing or reading only portions of the content, will re-use the same socket from the underlying "keep alive cache".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
alanb
Offline
Joined: 2005-08-08
Points: 0

A similar question asking about the behavior when the application doesn't read the entire response was posted here:
http://forums.java.net/jive/thread.jspa?threadID=16184&tstart=0

sdo
Offline
Joined: 2005-05-23
Points: 0

To a degree: the HTTPUrlConnection (not directly, but in the underlying classes) keeps a small pool of open keep-alive sockets. It will keep a maximum of five of them, so if you have multiple threads accessing servers, or access a lot of servers, some of them will get closed. But in the simple cases, you will re-use the socket.

byhisdeeds
Offline
Joined: 2006-01-06
Points: 0

> To a degree: the HTTPUrlConnection (not directly, but
> in the underlying classes) keeps a small pool of open
> keep-alive sockets. It will keep a maximum of five of
> them, so if you have multiple threads accessing
> servers, or access a lot of servers, some of them
> will get closed. But in the simple cases, you will
> re-use the socket.
So if I only have one thread that talks to one or two servers, multiple calls to these servers using new HTTPUrlConnection (...) will re-use these open keep-alive sockets in the underlying classes. Yes?

sdo
Offline
Joined: 2005-05-23
Points: 0

Yes, that's correct.

One other thing that I should have mentioned: the keep alive sockets are subject to a timeout. If the server sends back a timeout in the HTTP header, then that timeout value is used. Otherwise, the timeout is only 5 seconds, after which the socket will not be reused (though it won't be immediately closed, either, so it can appear from netstat or other OS utilities that the socket is open even if it won't be reused).

byhisdeeds
Offline
Joined: 2006-01-06
Points: 0

But this timeoue value I can set to say 600 secs etc, to ensure that the socket is re-used for the various application data requests.

sdo
Offline
Joined: 2005-05-23
Points: 0

Unfortuantely, you cannot set the timeout; it is hardwared in the bowels of the implementation. If you can make sure that the webserver returns a timeout value in the header (Apache does that, for example), then that value will be used. Otherwise, you're stuck with 5 seconds [Or writing your own HTTP client handler on top of sockets that you manage yourself.]

byhisdeeds
Offline
Joined: 2006-01-06
Points: 0

> Unfortuantely, you cannot set the timeout; it is
> hardwared in the bowels of the implementation. If you
> can make sure that the webserver returns a timeout
> value in the header (Apache does that, for example),
> then that value will be used.
But this would require that the response to the first request be returned within 5 seconds, YES?

sdo
Offline
Joined: 2005-05-23
Points: 0

No, the timeout applies to the length of time the socket lives in the cache. It's a cache timeout, not a socket timeout that you may be used to.

It works logically like this: the URLConnection opens the socket, sends the GET/POST request, and processes the header. When you close the input stream, the opened socket is placed in the cache with its associated timeout (either the default 5 seconds or the value read from the server when the header was processed). Next time a URLConnection needs a socket, if the socket is still in the cache, that socket is used. Otherwise, a new socket is opened.

So you have 5 (or whatever the server said) seconds from the time you close the connection's input stream to when you open a new URLConnection if you want the socket to be reused.