App Engine 推送队列的错误处理程序

Error handlers for App Engine push queues

我正在使用错误处理程序来获取有关 App Engine 应用程序中每个错误的通知。它适用于 Cloud Endpoints 和 servlet,但我不知道如何处理推送队列调用的 servlet 中的错误。

web.xml中的配置

<error-page>
    <exception-type>java.lang.Exception</exception-type>
    <location>/admin/error-notification</location>
</error-page>

我想实现的场景

问题是一旦任务被删除,就不会调用任何东西。有没有办法像我上面描述的那样配置它?我不想收到关于每一次失败的通知,只希望收到从推送队列中删除任务之前的最后一次失败。

答案基于@tx802 提示使用X-AppEngine-TaskRetryCount

如果不在之前的 运行 中抛出异常,则无法重试任务,因此这是基于将某些异常标记为已抑制的简单 hack。在自定义错误处理程序中,您必须检查异常是否为 SuppressedException 类型。如果它被抑制,你可以安全地跳过这个异常,这意味着任务将很快重试。如果异常未包含在 SuppressedException 中,则表示已达到重试限制,这是应该以某种方式处理的最终异常。

public abstract class PushQueueHttpServlet extends HttpServlet {

    @Override
    protected final void doGet(HttpServletRequest req, HttpServletResponse res) throws
            IOException, ServletException {
        try {
            doGet2(req, res);
        } catch (Throwable throwable) {
            String retryCountString = req.getHeader("X-AppEngine-TaskRetryCount");

            int retryCount;
            try {
                retryCount = Integer.parseInt(retryCountString);
            } catch (NumberFormatException e) {
                // Probably running outside of the task queue
                throw throwable;
            }

            if (retryCount == getTaskRetryLimit()) {
                throw throwable;
            } else {
                throw new SuppressedException(throwable);
            }
        }
    }

    protected abstract void doGet2(HttpServletRequest req, HttpServletResponse res) throws
            IOException, ServletException;

    protected abstract int getTaskRetryLimit();
}

SuppressedException class

public class SuppressedException extends IOException {

    public SuppressedException(Throwable cause) {
        super(cause);
    }
}