views:

82

answers:

4

If I have a service that makes RPC calls (say to web services, or restful services, or just to scrape data from a url, etc), is it possible for me to have it timeout after 5 seconds?

I don't want it to hang and then crash if the remote service is down, rather try for x seconds, if its down, then just carry on to the next remote service call.

A: 

If it's a stand-alone app then you can use the OS to schedule it (for example Task Scheduler in Windows). If it's not a stand-alone, then you can make it into a Java Task and use Java's Timer to schedule it.

Have a look a Java.util.Timer and here is an example http://www.java2s.com/Code/Java/Development-Class/UsejavautilTimertoscheduleatasktoexecuteonce5secondshavepassed.htm

del.ave
A: 

Client libraries for RPC usually have optional timeout parameter available. If the request is not finished in required time, the client library simply closes the connection. There is probably nothing about timeouts in those protocols themselves.

How are you doing your RPC class? What libs you are using?

Juha Syrjälä
A: 

What protocol are you using? It is http or RMI? The best way will be to implement timeout on the connection channel (like Socket). If this is something you want to do take a look at http://download.oracle.com/javase/1.5.0/docs/guide/net/properties.html

Baski
A: 

If you use Apache HTTP Components then do this:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
Romain Hippeau