开发者

How to set connection timeouts in Google App Engine?

I use the following lines to get a web page from GAE, but it takes a long time, how to raise the timeout limit ?

try
{
  URL url=new URL(Url + "?r=" + S开发者_StackOverflow中文版ystem.currentTimeMillis());
  BufferedReader reader = new BufferedReader(
                          new InputStreamReader(url.openStream()));

  while ((line=reader.readLine())!=null) { Result += line + "\n"; }
  reader.close();
}
catch (MalformedURLException e) { ... }
catch (IOException e) { ... }


GAE/J offers two APIs:

Option 1. The java.net API, where you can use the URLConnection (or HttpURLConnection) class:

URLConnection conn = url.openConnection();
conn.setConnectTimeout(timeoutMs);
conn.setReadTimeout(timeoutMs);

Option 2. The GAE Low Level API offers a FetchOptions#setDeadline method to set the deadline for the fetch request.

As a third alternative, you could also use a specific library such as HttpClient, but you would have to check if that library works with the inherent limitations of GAE/J.

HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
HttpClient httpClient = new DefaultHttpClient(httpParams);


url.openStream() is just a shortcut to call openConnection().getInputStream() but without the possibility to set the proper timeout statements.

You should use the openConnection() method instead with something like this:

URL url=new URL(Url+"?r="+System.currentTimeMillis());
URLConnection conn = url.openConnection();
conn.setConnectTimeout(timeoutMs);
conn.setReadTimeout(timeoutMs);
in = conn.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(in));


To set the timeout, you have to use the low level API. To do so, instantiate an HttpRequest, call getFetchOptions() on it, and call setDeadline on the returned object.


Are you using GAE/J SDK 1.4? If so, then Task Queue timing out in 30 sec instead of the promised 10 min. I believe you need to change something in queue.xml file. Also you can take a look this mail-archive

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜