views:

22751

answers:

11

I'm getting a ConnectException: Connection timed out with some frequency from my code. The URL I am trying to hit is up. The same code works for some users, but not others. It seems like once one user starts to get this exception they continue to get the exception.

Here is the stack trace:

java.net.ConnectException: Connection timed out
Caused by: java.net.ConnectException: Connection timed out
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
    at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
    at java.net.Socket.connect(Socket.java:516)
    at java.net.Socket.connect(Socket.java:466)
    at sun.net.NetworkClient.doConnect(NetworkClient.java:157)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:365)
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:477)
    at sun.net.www.http.HttpClient.<init>(HttpClient.java:214)
    at sun.net.www.http.HttpClient.New(HttpClient.java:287)
    at sun.net.www.http.HttpClient.New(HttpClient.java:299)
    at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:796)
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:748)
    at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:673)
    at sun.net.www.protocol.http.HttpURLConnection.getOutputStream(HttpURLConnection.java:840)

Here is a snippet from my code:

URLConnection urlConnection = null;
OutputStream outputStream = null;
OutputStreamWriter outputStreamWriter = null;
InputStream inputStream = null;

try {
    URL url = new URL(urlBase);
    urlConnection = url.openConnection();
    urlConnection.setDoOutput(true);

    outputStream = urlConnection.getOutputStream(); // exception occurs on this line
    outputStreamWriter = new OutputStreamWriter(outputStream);
    outputStreamWriter.write(urlString);
    outputStreamWriter.flush();
    inputStream = urlConnection.getInputStream();
    String response = IOUtils.toString(inputStream);
    return processResponse(urlString, urlBase, response);
} catch (IOException e) {
    throw new Exception("Error querying url: " + urlString, e);
} finally {
    IoUtil.close(inputStream);
    IoUtil.close(outputStreamWriter);
    IoUtil.close(outputStream);
}
A: 

Why does it have to be your code ? Can't it be the server which is overloaded ? Or a network connection problem ? Simply to verify if you try to work with an url on another server ...

amo-ej1
A: 

There is a possibility that your IP/host are blocked by the remote host, especially if it thinks you are hitting it too hard.

larsivi
A: 

Do you need to close / dispose of the urlConnection as well? Perhaps a problem with too many open connections?

benefactual
+4  A: 

Connection Timeouts (assuming a local network and several client machines) typically result from

a) some kind of firewall on the way that simply eats the packets without telling the sender things like "No Route to host"

b) packet loss due to wrong network configuration or line overload

c) too many requests overloading the server

d) a small number of simultaneously available threads/processes on the server which leads to all of them being taken. This happens especially with requests that take a long time to run and may combine with c).

Hope this helps.

Jumpy
+3  A: 

If the URL works fine in the web browser on the same machine, it might be that the Java code isn't using the HTTP proxy the browser is using for connecting to the URL.

Alexander
A: 

HTTP proxy may be a good bet, actually. Many apps get this configuration automatically (i believe java through webstart will set it automatically from the browser config too), most java desktop style apps have to have this manually configured.

John Gardner
+2  A: 

I'd recommend raising the connection timeout time before getting the output stream, like so:

urlConnection.setConnectTimeout(1000);

Where 1000 is in milliseconds (1000 milliseconds = 1 second).

R. Bemrose
A: 

-try to do the telent to see any firewall issue -perform tracert/traceroute to find number of hops. -increase the timeout (not recommended)

A: 

are you trying to connect remotely? If so it may be a database issue.

A: 

I think i have somthing similar as sarha explain,but i am working over internet I have 15 long running requests all the time in the server because when they end a new one is sent. i do not think it is to much and i expect to have much more in the future.This exception appears after 20 minutes. what can i do in this situation?

A: 

I think i have somthing similar as sarha explain,but i am working over internet I have 15 long running requests all the time in the server because when they end a new one is sent. i do not think it is to much and i expect to have much more in the future.This exception appears after 20 minutes. what can i do in this situation?

martin de la torre