DeadlineExceedexception是如何在Google App Engine for Java中实现的?

DeadlineExceedexception是如何在Google App Engine for Java中实现的?,java,http,google-app-engine,servlets,Java,Http,Google App Engine,Servlets,上的应用程序必须具有在30秒内返回响应数据的web请求。超过此时间时,将引发一个deadlineExceedexception异常: /time.jsp java.lang.ClassCastException: com.google.apphosting.api.DeadlineExceededException cannot be cast to javax.servlet.ServletException at org.apache.jasper.runtime.PageConte

上的应用程序必须具有在30秒内返回响应数据的web请求。超过此时间时,将引发一个
deadlineExceedexception
异常:

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

怎么做的?你有开源代码吗?

我想这个异常背后的机制是在appengine基础设施中实现的,它不是开源的

但是,您可以将此功能用于联网或更一般地使用(例如)的任何I/O绑定代码。相反,对于CPU限制的代码,您可以使用(例如)

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

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

请求处理程序可以捕捉到这一点 自定义响应时出错。这个 运行时环境发出请求 处理程序的时间多一点(更少 (超过一秒钟)在升起 准备自定义的异常 答复

而一个请求可能需要30分钟 响应时间为秒,应用程序引擎已启动 针对具有以下特性的应用程序进行了优化: 短期请求,通常是 这需要几百毫秒。 一个高效的应用程序可以快速响应 大多数请求都被拒绝。一个应用程序 不会与应用程序很好地扩展 引擎的基础设施


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

这可以按如下方式完成,使用并创建一个监视线程,该线程记录启动时间,并在经过时间后抛出异常。非阻塞IO教程展示了如何通过示例实现这一点

可能是一个很晚的答复,但对新手来说很有用

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

}
@覆盖
受保护的最终void doGet(最终HttpServletRequest请求,最终HttpServletResponse请求)引发IOException{
res.setContentType(“文本/html”);
最终打印输出=res.getWriter();
最终字符串a=req.getParameter(“a”);
试一试{
对于(int i=0;1<5;i++){
输出打印(“模式”+a+“运行”+i+“”+MainServlon.doubleIt(i));
睡眠(90000);
}
}捕获(最终中断异常e){
e、 printStackTrace();
}捕获(最终死线超出e){
打印(“火车要停了,现在让我结束这个故事吧!”);
}
}

直截了当地说,你为什么在乎呢?嗯。。。因为人类好奇?@Nick Johnson:因为它可能在其他(自托管)应用程序中很有用,在这些应用程序中,应用程序可能会与其他子系统(从其他站点获取url、巨大的lucene索引搜索、数据库死锁)做很多事情,并且您可能需要使用它来避免线程饥饿(蛮力)问题是GAE是如何做到的。谷歌说它是在运行时实现的。问题是如何为每个大于30秒的请求/servlet抛出异常,而不是如何捕获它。