当我向指定的网址发布帖子请求时,我遇到了Google App Engine超时的问题。当我在hurl.it处进行测试请求时,它完全正常。当我在本地服务器上运行代码时,运行完全正常。当我将其部署到Google App Engine并在线测试时,无论我给出多少超时限制(测试最多1分钟),它都会超时。我不知道是什么导致它在网上而不是在本地发生错误。这是我出错时的代码和堆栈跟踪:
package com.servlet.thing;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@SuppressWarnings("serial")
public class ServletThingServlet extends HttpServlet {
private static final String PASS = "testpassword123";
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
URL url = new URL("https://api.roblox.com/login/v1");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
//URLConnection con = url.openConnection();
con.setConnectTimeout(60000);
// Doesn't get past here online
resp.getWriter().println("connected");
con.setDoOutput(true);
con.setRequestMethod("POST");
resp.getWriter().println("checkpoint 1");
OutputStreamWriter writer = new OutputStreamWriter(con.getOutputStream());
writer.write("username=TestPromotionAcc&password=" + PASS);
writer.flush();
writer.close();
resp.getWriter().println("checkpoint 2");
HashMap<String, List<String>> map = (HashMap<String, List<String>>) con.getHeaderFields();
if (con.getResponseCode() == HttpURLConnection.HTTP_OK || con.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
StringBuffer response = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
while ((line = reader.readLine()) != null) {
response.append(line);
}
System.out.println("--- HEADER FIELDS ---\n");
for (Map.Entry<String, List<String>> entry : map.entrySet()) {
if (entry.getKey().equalsIgnoreCase("set-cookie")) {
System.out.println("Value : " + entry.getValue());
resp.getWriter().println("Set cookies: " + entry.getValue());
}
}
reader.close();
System.out.println(response.toString());
} else {
resp.getWriter().println(con.getResponseCode() + ": " + con.getResponseMessage());
}
}
}
这是堆栈跟踪:
来自servlet的未捕获异常 java.net.SocketTimeoutException:获取URL时超时:http://api.roblox.com/login/v1 在com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:179) 在com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:45) 在com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler $ Connection.fetchResponse(URLFetchServiceStreamHandler.java:543) 在com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler $ Connection.getInputStream(URLFetchServiceStreamHandler.java:422) 在com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler $ Connection.getResponseCode(URLFetchServiceStreamHandler.java:275) 在com.servlet.thing.ServletThingServlet.doGet(ServletThingServlet.java:30) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 在org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1166) 在com.google.apphosting.utils.servlet.ParseBlobUploadFilter.doFilter(ParseBlobUploadFilter.java:125) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) 在com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:37) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) 在com.google.apphosting.utils.servlet.JdbcMySqlConnectionCleanupFilter.doFilter(JdbcMySqlConnectionCleanupFilter.java:60) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) 在com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:48) 在org.mortbay.jetty.servlet.ServletHandler $ CachedChain.doFilter(ServletHandler.java:1157) 在org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388) 在org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) 在org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182) 在org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765) 在org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418) 在com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:257) 在org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152) 在org.mortbay.jetty.Server.handle(Server.java:326) 在org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542) at org.mortbay.jetty.HttpConnection $ RequestHandler.headerComplete(HttpConnection.java:923) 在com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76) 在org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404) 在com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:145) 在com.google.apphosting.runtime.JavaRuntime $ RequestRunnable.run(JavaRuntime.java:511) 在com.google.tracing.TraceContext $ TraceContextRunnable.runInContext(TraceContext.java:446) 在com.google.tracing.TraceContext $ TraceContextRunnable $ 1.run(TraceContext.java:453) 在com.google.tracing.CurrentContext.runInContext(CurrentContext.java:274) 在com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:312) 在com.google.tracing.TraceContext $ AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:304) 在com.google.tracing.TraceContext $ TraceContextRunnable.run(TraceContext.java:450) 在com.google.apphosting.runtime.ThreadGroupPool $ PoolEntry.run(ThreadGroupPool.java:235) 在java.lang.Thread.run(Thread.java:745)
答案 0 :(得分:2)
根据我的经验,截止日期不会在当地执行。如果您的请求exceeds 60 seconds,则会发生超时。为了克服这个问题,我将请求包装在一个任务中,这个任务有更宽松的10分钟来完成任务。要检查的其他事项 - 您是否正在运行应用实例,或者应用引擎是否必须启动新实例来处理openConnection
?