Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What governs a URL connection timeout?

843842Feb 12 2010 — edited Feb 12 2010
Hi all

Hoping someone can shed some light on a little puzzle I have. One of our servlets is opening a URL connection to hit an external URL, the external URL can sometimes take a while to respond and when that happens the URL connection throws a socket timeout exception (see the stack trace below). What I can't work out is what determines how long the connection waits before it times-out, we don't set anything explicitly in our code and it doesn't seem to be related to the servlet timeout in the connector, does anyone know what determines the timeout length and how it can be changed? The code is simply -

public void outputUrl(OutputStream p_out, String p_url) {
try {
URL t_url = new URL(p_url);
InputStream t_inputStream = t_url.openStream();
// Read from the input stream, and write to the output stream
byte[] l_buffer = new byte[10]; // buffer holding bytes to be transferred
int l_nbytes = 0; // Number of bytes read
while ((l_nbytes = t_inputStream.read(l_buffer)) != -1)
p_out.write(l_buffer,0,l_nbytes);
t_inputStream.close();
}
catch (Exception e)
{
nsglog.error(String.valueOf(e), e);
}
}

The error trace is -

java.net.SocketTimeoutException: Read timed out
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:256)
at java.io.BufferedInputStream.read(BufferedInputStream.java:313)
at sun.net.www.http.HttpClient.parseHTTPHeader(HttpClient.java:659)
at sun.net.www.http.HttpClient.parseHTTP(HttpClient.java:604)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:961)
at java.net.URL.openStream(URL.java:1007)
at ep.ext.outputUrl(ext.java:446)

So it's the attempt to open the input stream on the URL that is timing out, what governs that timeout?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Mar 12 2010
Added on Feb 12 2010
2 comments
2,687 views