commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Allen, Michael B (RSCH)" <Michael_B_Al...@ml.com>
Subject RE: httpclient hangs indefinately
Date Sun, 03 Feb 2002 23:16:01 GMT


> -----Original Message-----
> From:	Ludwig, Sven [SMTP:SLudwig@CONET.DE]
> 
> I read about that issue of blocking IO in the constructors
> of java.net.Socket with JDKs up to 1.3 some time ago on javaworld.com
> It was easy to find the article again:
> 
> http://www.javaworld.com/javaworld/jw-09-1999/jw-09-timeout.html
> 
> Along with it there is an implementation "TimedSocket" available: 
> (seperate thread that interrupts the connection attempts)
> 
> http://www.javaworld.com/jw-09-1999/timeout/jw-09-timeout.zip 
> 
	Actually, the separate thread trys to do the connect while the caller sits there
	polling isConnected(). It does not "interrupt" the other thread (I have actually
	tried that and it doesn't work). Regardless, the idea behind this article is a good
	idea but it's a very poor inplementation. It would have been trivial to just do
	something like:

	public class TimedSocket() extends Thread {

		String host;
		int port;
		IOException e;
		Socket s;

		TimedSocket( host, port, long timeout ) IOException {
			this.host = host;
			this.port = port;
			e = null;
			synchronized( this ) {
				start();
				wait( timeout );
			}
			if (e == null) {
				throw e;
			}
		}

		public void run() {
			synchronized( this ) {
				try {
					s = new Socket( host, port );			
				} catch( IOException ioe ) {
					e = ioe;
				}
				notify();
			}
		}
	}

	Although I don't know about the synchronized( this ) in a constructor. That
	might be better off elsewhere. Also, the new Socket call will still be blocked
	long after the TimedSocket has thrown the IOException. Under certain
	conditions it's concievable that the application could just consume dozens of
	hung sockets running the app into memory overload.

	It's a good alternative if you don't have JDK 1.4's Socket constructors with the
	timeout parameter. Unfortunately for me I cannot use it because it creates an
	extra thread for every connection which under certain conditions would be an
	inappropriate way to deal with this rare Socket timeout corner case issue.

	Mike


--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message