Wrapping try-catch that fetches from internet, in a while - java

So I have a try-catch statement in a java program that fetches things from the internet. How do I handle timeouts? Would I just wrap the try catch in a while statement and after some number of failed iterations tell the user to try later?

How do I handle timeouts? Would I just wrap the try catch in a while statement and after some number of failed iterations tell the user to try later?
I don't think that would be a good idea. IMO, the best thing to do is to pick a timeout that corresponds to the time that you think that the user should have to wait, and not use a loop. As #BalusC points out, any decent Http client API will give you a way to set the timeout before you make the request. Use it.
The problem with using a loop is that you are potentially adding load to an already overloaded server. Suppose that the real reason for the timeout is that the server is trying to handle too many requests in parallel, and each request is taking a long time. If you (the client) time out a request and then immediately retry it, you are probably just adding extra load ... making things worse.
The chances are that some users will hammer the retry button anyway. You don't need to do the hammering for them.

Related

Increasing HTTP client timeout

I'll get right into the subject
I have a server that works a music recommendation system ( for some kind of application)
the server has a very large database
So i made a singleton constructor of the recommendation system.
My problem is
the first time this constructor is being created it has to run a training data and it connects to the database a lot which is a time consuming operation
This has to run only the first time according to my singleton object and then afterwards, it'll be able to use the results of the constructor right away
My problem is that on the first HTTP request from my PC to the server, the explorer times out and the singleton object is never created on the server
I think my solution would be in extending the wait time of the explorer until the server finishes computation and returns with result, however
if someone has a better solution i'd be greatly in his dept
I really need an easy applicable solution that requires minimal effort because the delivery deadline is closing up and i need to wrap the project as fast as possible
Thanks again
Few comments/suggestions
Increasing timeout is one way but its not sure shot way of solving the problem. The time taken by the recommendation system may not always be same over the time.
I suggest another approach to solve this. Not sure if its an option for you, but Will it be possible to create the recommendation system asynchronously in a separate thread so that the server start up is not held back by this ?
If you could do above, then provision a flag which indicates that recommendation system has started.
Meanwhile if you receive any request, first check the flag if the flag indicate that the recommendation system has not yet started, the return some meaningful message/status.
This way you will get the response immediately and based on the response you can work out retries on the client side.
Please note that this will be substantial change on the server side. Just an opinion to improve the things further and full proof way of avoiding timeout.
You can increase the connection time out using below
HttpResponse response = null;
final HttpParams httpParams = new BasicHttpParams();
// 60 second connection timeout
HttpConnectionParams.setConnectionTimeout(httpParams, 60000);
HttpClient httpClient = new DefaultHttpClient(httpParams);

Thread Handling after exception

used thread.sleep when exception occurs in my application
if any thing exception occured in any one of the thread it should wait for some time and have to try again
what i am facing the issue means for 100 request if exception occurs for 50 means that should be wait for some time and after try again if any
exception occurs means it should also wait till success
am feeling this going to affect the performance and system usage.
Please let me know your suggestions. Is it better to create new thread if any exception occurs?
Or Any other way?
Retrying on exception isn't a bad idea, but you should make sure that:
The exception is transient (i.e. you have reasons to believe it may
succeed on retry, like a network error)
You have ways to prevent retrying forever (like "max retry")
You don't overload the system by too frequent retries by e.g. using exponential back-off strategy (or even just a reasonably long wait time)
Exception to rule 2 is possible (sometimes you do want stuff to retry forever).
From the question, I understand that, you are talking about RunTimeExceptions.
1) Firstly I would like to suggest that, you should examine the code areas which may really throw the exceptions at run time i.e processing of files.
2) another thing is that you have to handle this scenarios so that exception cases are reduced. i.e first check file is available then only try to read it, check permissions before writing it etc. In this way frequency of retrying may be reduced.
3) If both above actions fails, means the situation which can never be handled such as services are down etc., you can retry the considering all necessary aspects like - MAX try, MAX time out foe which you are re-trying, leave the attempt after certain amount of try OR time etc.
Hope this helps.

Gathering strings from a website that starts refusing your connection after a certain amount of time

I am currently creating a program that, based on constantly changing variables, connects to a website, and gathers information. It must connect to the website up to 400 times. The subject website seems to display a blank screen after a certain amount of connections, about 10-30. Does anyone know the best way find how long to wait between connections?
public static String pullString(int id) {
return null;
}
I can't get there from work, but google runescape api. They have one here, and I'll bet they expect you to use it.
Once it starts blocking you, does it eventually let you reconnect? You might be able to do some sort of algorithm to dynamically find how fast to try again.
You could consider something similar to what TCP congestion control does: start with some wait time between connections. When one completes successfully, decrease the wait time by a constant. When you get an error, double the wait time (or multiply by a constant).
It's very likely, though, that they're doing something more complicated than just rate-limiting connections. Without knowing what you have to get around, it's hard to know how to get around it.
If the website is giving you random block time after you reach certain limit it's almost impossible to find the best wait time. I think your best bet is to use a pool of http proxy to access the website in round robin way. Though that's not very nice... But technically it should be the best way to access a website programmatically if it blocks you after a certain amount of traffic.
Here is a link about how to use proxy: http://docs.oracle.com/javase/6/docs/technotes/guides/net/proxies.html
You can also use HttpClient which is simpler.
Try to google around and you can find a lot of free proxy server list.

Java program and network connection timeout

I have a java program that gather data from the web.
Unfortunately, my network has a problem and it goes off and on.
I need a trick to ask my program wait until the connection is on again and continue its job.
I use the "URLConnection" library to connect. I made a loop to reconnect when it a "ConnectException" is catched, but it doesn't work.
Any suggestions?
my network has a problem and it goes off and on. I need a trick to ask
my program wait until the connection is on again and continue its job
It depends on what your program's purpose is.
How long are these intermittent failures? If they are short enough you could use setConnectTimeout(0) to indicate an infinite timeout period while trying to connect but if your program has to report something back then it is not a good option for the end user.
You could set a relatively low timeout so that when you start to lose the network you will get a java.net.SocketTimeoutException.
When you catch this, you could wait for a period and try again in a loop e.g. for 3 times and then perhaps report a failure.
It depends on what you are trying to do.

SocketTimeoutException problem - how to continue after exception

I write and read in my function using Socket class. I used
synchronized(socket){
.//write;
//read;
}
I am doing this (repeat) every 50-1000 ms. Problem is when somebody ( unknown reason ) pluged off cable ( I got SocketTimeoutException). When he pluged in again, I need to continue.
What to do ? Do I need to close this socket in catch block and create new ? Or something else ?
You don't have to do anything. Just continue. If you get any other exception, close the Socket and restart (if appropriate).
I'd create a Decorator implementation that was willing to catch a SocketTimeoutException and retry. It could retry a certain number of times, over a certain interval before actually passing the exception along to indicate a "true" (to be defined) error condition. It could even retry indefinitely if you want. The retry logic may even encapsulate re-establishing the socket, though a timeout isn't enough to require that.
Another option is the CircuitBreaker Pattern, though it's not quite designed for what you are describing. CircuitBreaker is a bit better for avoiding costly errors that may be occurring over a period of time.

Categories