2009-06-17 22 views
7

Google App Engine上的應用程序必須具有在30秒內返回響應數據的Web請求。當超過這個時間,一個DeadlineExceededException拋出異常:如何在Google App Engine for Java中實現DeadlineExceededException?

/time.jsp 
java.lang.ClassCastException: com.google.apphosting.api.DeadlineExceededException cannot be cast to javax.servlet.ServletException 
    at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:754) 
    at org.apache.jsp.time_jsp._jspService(time_jsp.java:66) 
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94) 
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:806) 
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1093) 
    at com.google.apphosting.runtime.jetty.SaveSessionFilter.doFilter(SaveSessionFilter.java:35) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) 
    at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43) 
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1084) 
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:360) 
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216) 
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181) 
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:712) 
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405) 
    at com.google.apphosting.runtime.jetty.AppVersionHandlerMap.handle(AppVersionHandlerMap.java:237) 
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:139) 
    at org.mortbay.jetty.Server.handle(Server.java:313) 
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:506) 
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:830) 
    at com.google.apphosting.runtime.jetty.RpcRequestParser.parseAvailable(RpcRequestParser.java:76) 
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:381) 
    at com.google.apphosting.runtime.jetty.JettyServletEngineAdapter.serviceRequest(JettyServletEngineAdapter.java:125) 
    at com.google.apphosting.runtime.JavaRuntime.handleRequest(JavaRuntime.java:235) 
    at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4755) 
    at com.google.apphosting.base.RuntimePb$EvaluationRuntime$6.handleBlockingRequest(RuntimePb.java:4753) 
    at com.google.net.rpc.impl.BlockingApplicationHandler.handleRequest(BlockingApplicationHandler.java:24) 
    at com.google.net.rpc.impl.RpcUtil.runRpcInApplication(RpcUtil.java:359) 
    at com.google.net.rpc.impl.Server$2.run(Server.java:800) 
    at com.google.tracing.LocalTraceSpanRunnable.run(LocalTraceSpanRunnable.java:56) 
    at com.google.tracing.LocalTraceSpanBuilder.internalContinueSpan(LocalTraceSpanBuilder.java:510) 
    at com.google.net.rpc.impl.Server.startRpc(Server.java:756) 
    at com.google.net.rpc.impl.Server.processRequest(Server.java:348) 
    at com.google.net.rpc.impl.ServerConnection.messageReceived(ServerConnection.java:459) 
    at com.google.net.rpc.impl.RpcConnection.parseMessages(RpcConnection.java:319) 
    at com.google.net.rpc.impl.RpcConnection.dataReceived(RpcConnection.java:290) 
    at com.google.net.async.Connection.handleReadEvent(Connection.java:419) 
    at com.google.net.async.EventDispatcher.processNetworkEvents(EventDispatcher.java:762) 
    at com.google.net.async.EventDispatcher.internalLoop(EventDispatcher.java:207) 
    at com.google.net.async.EventDispatcher.loop(EventDispatcher.java:101) 
    at com.google.net.rpc.RpcService.runUntilServerShutdown(RpcService.java:251) 
    at com.google.apphosting.runtime.JavaRuntime$RpcRunnable.run(JavaRuntime.java:373) 
    at java.lang.Thread.run(Unknown Source) 

它怎麼辦?你有沒有開源的代碼?

+1

說穿了,你爲什麼在意? – 2009-06-18 12:30:42

+4

呃......因爲人類好奇? – wds 2009-10-08 09:52:56

+1

@Nick Johnson:因爲它可能對其他應用程序可能與其他子系統(從其他站點獲取URL,大量lucene索引搜索,數據庫死鎖)做很多事情的其他(自託管)應用程序有用,並且您可能需要使用這種(暴力)方式來避免線程匱乏。 – cherouvim 2011-04-04 10:19:33

回答

1

我想這種異常背後的機制是在應用程序引擎基礎結構中實現的,這不是開源的。

但是,您可以將此功能用於網絡或更一般地使用java.nio(例如Selector)的任何I/O有界代碼。而對於CPU有界代碼,您可以使用java.util.concurrent(例如Future)。

3

The Request Timer

請求處理具有有限的量的時間來生成並返回一個 響應於請求,典型地 大約30秒 。一旦達到截止日期 ,請求處理程序 中斷。

Java運行時環境 通過投擲 com.google.apphosting.api.DeadlineExceededException中斷的servlet。 如果請求處理程序沒有捕獲到 這個異常,與所有未捕獲的異常一樣,運行時環境 將向客戶端返回HTTP 500服務器錯誤 。

請求處理程序可以捕獲此 錯誤來自定義響應。該 運行環境給人的請求 處理器提高 例外編寫自定義的 響應之後多一點點的時間(不到一秒 )。

雖然請求可能需要長達30秒 響應,App Engine是 與 短暫的請求,通常那些 是需要幾百毫秒應用進行了優化。 一個高效的應用程序快速響應 大部分請求。應用程序 不會很好地與應用程序發動機的基礎設施。

部分運行時==不使用線程。這個功能肯定是VM的修改,我不會屏住呼吸等待源代碼。

1

這是可以做到如下,用non-blocking IO,並創建一個線程看門狗裏面記錄了啓動時間,當經過的時間已經過去了拋出異常。非阻塞IO教程展示瞭如何通過Ping.java示例完成此操作。

+1

這個問題問GAE如何做到這一點。谷歌表示它在運行時實現。 http://code.google.com/appengine/docs/java/runtime.html#The_Request_Timer – 2009-10-16 20:13:11

0

可能是一個非常晚的答覆,但對新學員很有用。

@Override 
protected final void doGet(final HttpServletRequest req, final HttpServletResponse res) throws IOException { 

    res.setContentType("text/html"); 
    final PrintWriter out = res.getWriter(); 
    final String a = req.getParameter("a"); 
    try { 

     for (int i = 0; 1 < 5; i++) { 
      out.print("Mode " + a + " Running " + i + " " + MainServlon.doubleIt(i)); 
      Thread.sleep(90000); 
     } 

    } catch (final InterruptedException e) { 
     e.printStackTrace(); 
    } catch (final DeadlineExceededException e) { 
     out.print("Train is going to stop. Let me close this story for now!"); 
    } 

} 
+0

問題是關於如何爲每個請求/ servlet> 30s引發異常,而不是如何捕獲它。 – Franck 2015-08-03 22:29:16

+0

嗨安靜同意! – 2016-01-01 09:16:31

相關問題