Skip to Main Content

Java APIs

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!

Code hangs while calling URLConnection.getInputStream()

843790Sep 23 2006 — edited Oct 5 2006
Below is the code I'm using to establish an input stream from an http:// source. It is part of an application that connects to various web pages on the internet at a rate of about 7 every minute, bursting up to about 20 in 1 minute.

The applications will run for days (sometimes over a week) but eventually, the thread in which this method is called, will hang permanently, despite the use of connection and read timeouts of 5 seconds.

I've put a number logging statements in so I can tell you the last message logged. See comment in code

No exception is thrown.

Can anyone tell me where I've gone wrong? Or is there a bug in URLConnection (or HttpConnection)?

The application runs as a servlet inside Apache Tomcat/5.5.17 on a Linux 2.6.15-1.2054_FC5smp under Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_07-b03)
/**
* Note: sourceUrl is http://...
*/
private StreamSource getStreamSource(String sourceUrl)
{
  StreamSource streamSource = null;

  try
  {
    logger.info("Opening connection to: " + sourceUrl);

    URL url = new URL(sourceUrl);
    URLConnection connection = url.openConnection();

    logger.info("Calling connect: " + sourceUrl);
    connection.setConnectTimeout(5000); // give them 5 seconds
    connection.connect();

    logger.info("Getting input stream: " + sourceUrl);  // LAST LOGGED MESSAGE
    connection.setReadTimeout(5000); // give them 5 seconds
    InputStream is = connection.getInputStream();

    streamSource = new StreamSource(is);
  }
  catch (Exception e)
  {
    throw new LoggedRuntimeException(logger,
       "Could not establish   connection for URL '" + sourceUrl + "'", e);
  }

  logger.info("Opened stream source for: " + sourceUrl);

  return streamSource;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 2 2006
Added on Sep 23 2006
8 comments
611 views