Hello,
A few times I mentioned that HttpConnection hangs when it encounters
servers that are down, unreachable, etc.
I did a bit of debugging and it appears that it is calls to 'new
Socket(....)' that are blocking. Furthermore, it looks like there is no
simple cure for that in Java's version 1.3 of the API.
However, JDK 1.4 exposes a 'connect' method in Socket class, which
allows a timeout value to be specified.
I modified my local version of HttpClient to use that and it works
perfectly. I was doing tests with a set of about 1150 URLs and 10
threads. Before this change it would take about 16 minutes to fetch
1150 pages, and now it takes about 3. Note that I hard-coded the connect
timeout value, but I realize that the default value should probably be
in a static final int and that we should probably expose this via a
public method that allows one to set the timeout value.
I understand that not everyone will want to use it, but it may be good
to apply this patch anyway and then comment it out, so that it is easy
for people to uncomment it out and use it. Or perhaps HttpConnection
could check one of the System properties that shows the JVM version and
use either the old or the new code, depending on the property.
What do you think?
Otis
Index: HttpConnection.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpConnection.java,v
retrieving revision 1.5
diff -u -r1.5 HttpConnection.java
--- HttpConnection.java 5 Jan 2002 11:16:00 -0000 1.5
+++ HttpConnection.java 13 Jan 2002 18:56:46 -0000
@@ -64,6 +64,8 @@
import java.net.Socket;
import java.net.SocketException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
@@ -271,7 +273,11 @@
if(_ssl) {
_socket =
SSLSocketFactory.getDefault().createSocket(_host,_port);
} else {
- _socket = new Socket(_host,_port);
+ //_socket = new Socket(_host,_port);
+ InetAddress ia= InetAddress.getByName(_host);
+ InetSocketAddress isa = new InetSocketAddress(ia, _port);
+ _socket = new Socket();
+ _socket.connect(isa, 5000);
}
} else {
if(_ssl) {
--
To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>