Java 如何在Google App Engine中设置连接超时?
我使用以下几行代码从GAE获取网页,但这需要很长时间,如何提高超时限制Java 如何在Google App Engine中设置连接超时?,java,google-app-engine,timeout,Java,Google App Engine,Timeout,我使用以下几行代码从GAE获取网页,但这需要很长时间,如何提高超时限制 try { URL url=new URL(Url + "?r=" + System.currentTimeMillis()); BufferedReader reader = new BufferedReader( new InputStreamReader(url.openStream())); while ((line=reader.readLine()
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) { ... }
GAE/J提供了两种API:
选项1。java.net API,您可以在其中使用URLConnection
(或)类:
选项2。GAE提供了一个FetchOptions#setDeadline
方法来设置提取请求的截止日期
作为第三种选择,您也可以使用特定的库,例如,但您必须检查该库是否具有GAE/J的固有限制
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutMillis);
HttpClient httpClient = new DefaultHttpClient(httpParams);
要设置超时,必须使用。为此,实例化一个
HttpRequest
,对其调用getFetchOptions()
,并对返回的对象调用setDeadline
。url.openStream()
只是调用openConnection().getInputStream()
的快捷方式,但无法设置正确的超时语句
您应该使用openConnection()
方法来代替以下内容:
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));
您正在使用GAE/J SDK 1.4吗?如果是这样,那么任务队列将在30秒内超时,而不是承诺的10分钟。我相信您需要更改Queue.xml文件中的某些内容。你也可以看看这个关于选项(1)-引用“应用程序无法为请求设置显式连接超时”-那么这真的是一个选项吗?
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));