SocketTimeoutException occurring non-stop after app has run for a while, but are instantly-solved by restarting it

StackOverflow https://stackoverflow.com/questions/8776443

Question

I have a crawler Java application which is supposed to connect to some HTTP servers, download the HTML content of their pages, then move on to other HTTP servers. For this task, I've used the Apache HTTP library.

At the first few hours of the run, things seem to work rather smoothly (there are some connection-related exceptions thrown around from time to time, but that's to be expected). Yet after a while, it seems like I keep getting SocketTimeoutException on every request I send out. The exception does not occur on the HttpClient class's "execute" method, but rather when I try to get the content of the Entity (which I retrieve from the HttpResponse object), or when I try to write that content to a file.

Then, if I stop the application, and start it over again, things seem to go back to working fine - even though it picks up from where it stopped at, meaning it's interacting with the same servers which I received the SocketTimeoutException when trying to interact with before.

I tried looking for all kinds of possible clean-ups that I might be missing and might be essential when using this library, but couldn't find anything.

Any help would be greatly appreciated. Thanks.

Was it helpful?

Solution

This sounds like the kind of thing which could be caused by connection pools where you're not closing things when you're done with them, if the timeout occurs while the client library waits to retrieve a pooled connection. Are you sure you're closing everything properly (in finally statements)?

If you run Wireshark to monitor your traffic, what network traffic occurs while it's "broken"?

OTHER TIPS

Make sure that you're not using a lot of http requests at the same time. For example, send 5 http requests, and wait for first response. Then you can make another request etc. Looks like your http requests opens too much sockets.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top