Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Google App Engine中设置连接超时?_Java_Google App Engine_Timeout - Fatal编程技术网

Java 如何在Google App Engine中设置连接超时?

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

我使用以下几行代码从GAE获取网页,但这需要很长时间,如何提高超时限制

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