开发者

Can you make a rpc call timeout after x seconds?

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 an开发者_开发百科d 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.


I think the following link from GWT will be helpful: http://code.google.com/webtoolkit/doc/latest/DevGuideCodingBasicsDelayed.html


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


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?


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


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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜