Frank
Frank

Reputation: 31086

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=" + System.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) { ... }

Upvotes: 1

Views: 1964

Answers (4)

Guido
Guido

Reputation: 47675

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

Upvotes: 2

Dibyendu Basu
Dibyendu Basu

Reputation: 51

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

Upvotes: 0

systempuntoout
systempuntoout

Reputation: 74054

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

Upvotes: 1

Nick Johnson
Nick Johnson

Reputation: 101139

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.

Upvotes: 0

Related Questions