An unreachable network will often hang the client JVM.  The socket
timeout only applies to "normal" communication errors...if you'll allow
that kind of term.  We've done extensive testing of network
communications in this kind of failure use case.  I do not believe there
is a way for HttpClient to work around this because the hang is deep
inside the JVM native code.

There is a method in InetAddress (isReachable(long ms)) to test if a
host is reachable.  But, even that will provide varying and misleading
results.

If you expect to encounter this often the only way to work around it is
to execute this in a daemon thread and abandon the thread if it does not
complete within the defined timeout period.

...Pete
Starbucks Coffee Co. - MS IT-5
2401 Utah Ave S
Seattle, WA. 98134
(w) 206-318-5933

-----Original Message-----
From: Vitaly Baranovsky [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, August 15, 2007 7:57 AM
To: [email protected]
Subject: Why connection timeouts doesn't works?

Good day!

Connection timeouts of httpClient in my program doesn't works. I set
timeout to 10 000 milliseconds, but my program waits for about 65
seconds before answering.

Here is stacktrace of my unit test:
java.net.SocketException: Network is unreachable: connect
 at java.net.PlainSocketImpl.socketConnect(Native Method)
 at java.net.PlainSocketImpl.doConnect(Unknown Source)
 at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
 at java.net.PlainSocketImpl.connect(Unknown Source)
 at java.net.SocksSocketImpl.connect(Unknown Source)
 at java.net.Socket.connect(Unknown Source)
 at java.net.Socket.connect(Unknown Source)
 at java.net.Socket.<init>(Unknown Source)
 at java.net.Socket.<init>(Unknown Source)
 at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.crea
teSocket(Unknown
Source)
 at
org.apache.commons.httpclient.protocol.DefaultProtocolSocketFactory.crea
teSocket(Unknown
Source)
 at org.apache.commons.httpclient.HttpConnection.open(Unknown Source)
 at
org.apache.commons.httpclient.MultiThreadedHttpConnectionManager$HttpCon
nectionAdapter.open(Unknown
Source)
 at
org.apache.commons.httpclient.HttpMethodDirector.executeWithRetry(Unknow
n
Source)
 at
org.apache.commons.httpclient.HttpMethodDirector.executeMethod(Unknown
Source)
 at org.apache.commons.httpclient.HttpClient.executeMethod(Unknown
Source)
 at org.apache.commons.httpclient.HttpClient.executeMethod(Unknown
Source)
 at net.liga.commons.HttpGetter.runGetCore(HttpGetter.java:58)
 at net.liga.commons.HttpGetter.getFromURLAsString(HttpGetter.java:88)
 at
net.liga.commons.test.HttpGetterTest.testGetFromUrlAsString(HttpGetterTe
st.java:38)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)
 at junit.framework.TestCase.runTest(TestCase.java:164)
 at junit.framework.TestCase.runBare(TestCase.java:130)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:120)
 at
org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUn
it3TestReference.java:130)
 at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.ja
va:38)
 at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:460)
 at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:673)
 at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:386)
 at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:196)



Here is the code of my class:

public class HttpGetter {

private static final int DEFAULT_SOCKET_TIMEOUT = 10000;
private HttpClient httpClient;
private GetMethod method;

public HttpGetter() {
super();
MultiThreadedHttpConnectionManager cm = new
MultiThreadedHttpConnectionManager();
cm.getParams().setSoTimeout(DEFAULT_SOCKET_TIMEOUT);

httpClient = new HttpClient(cm);
}

private String getFromURLAsString(String url) throws HttpException,
IOException {
  URI uri = new URI(url, false);
  method = new GetMethod(uri.getEscapedURI());
  method.getParams().setSoTimeout(DEFAULT_SOCKET_TIMEOUT);
  int result = httpClient.executeMethod(method);
  return method.getResponseBodyAsString();;
}
}



Here is the code of my TestCase:

public class HttpGetterTest extends TestCase {
        public void testGetFromUrlAsString() throws HttpException,
IOException{
                HttpGetter httpGetter = new HttpGetter();
        
System.out.println(httpGetter.getFromURLAsString("http://google.com";));
        }
}


Why timeouts doesn't works?

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to