【问题标题】:How is DeadlineExceededException implemented in Google App Engine for Java?DeadlineExceededException 在 Google App Engine for Java 中是如何实现的?
【发布时间】:2010-11-03 20:00:24
【问题描述】:

Google App Engine 上的应用程序必须具有在 30 秒内返回响应数据的 Web 请求。当超过这个时间时,会抛出一个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)

它是如何完成的?有开源代码吗?

【问题讨论】:

  • 说白了,你为什么在乎?
  • 嗯...因为人类很好奇?
  • @Nick Johnson:因为它对于在其他(自托管)应用程序中实现可能很有用,其中应用程序可能与其他子系统做很多事情(从其他站点获取 url,巨大的 lucene 索引搜索,数据库死锁),您可能需要使用这种(蛮力)方式避免线程饥饿。

标签: java http google-app-engine servlets


【解决方案1】:

我想这个异常背后的机制是在应用引擎基础设施中实现的,而不是开源的。

但是,您可以将此功能用于网络或更一般地使用 java.nio(例如 Selector)的任何 I/O 有界代码。对于 CPU 受限代码,您可以使用 java.util.concurrent(例如 Future)。

【讨论】:

    【解决方案2】:

    The Request Timer

    请求处理程序的数量有限 时间来生成和返回 响应请求,通常 大约 30 秒。一旦截止日期 已到达,请求处理程序 被打断了。

    Java 运行时环境 通过抛出一个中断 servlet com.google.apphosting.api.DeadlineExceededException。 如果请求处理程序没有捕获 这个例外,和所有未捕获的一样 异常,运行时环境 将返回 HTTP 500 服务器错误 给客户。

    请求处理程序可以捕获这个 自定义响应的错误。这 运行时环境给出请求 处理程序多一点时间(少一点 超过一秒)后提高 准备自定义的异常 回应。

    虽然一个请求可能需要长达 30 秒响应,App Engine 是 针对应用程序进行了优化 短暂的请求,通常是那些 这需要几百毫秒。 一个高效的应用程序可以快速响应 大多数请求。一个应用程序 不会与 App 很好地扩展 引擎的基础架构。

    部分运行时 == 不使用线程。这个功能肯定是对 VM 的修改,我不会屏住呼吸等待源代码。

    【讨论】:

      【解决方案3】:

      这可以如下完成,使用non-blocking IO,并创建一个看门狗线程,记录启动时间并在经过的时间过去时抛出异常。非阻塞 IO 教程展示了如何使用 Ping.java 示例完成此操作。

      【讨论】:

      【解决方案4】:

      可能是一个很晚的回复,但对新学习者很有用。

      @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!");
          }
      
      }
      

      【讨论】:

      • 问题是关于如何为每个请求/servlet > 30s 抛出异常,而不是如何捕获它。
      猜你喜欢
      • 2010-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-05
      • 2010-10-28
      • 2011-03-11
      • 2011-10-22
      • 2014-06-09
      相关资源
      最近更新 更多