嵌入 Jetty 时检测处理程序何时无法启动

发布于 2024-08-29 05:51:06 字数 308 浏览 7 评论 0原文

我以与 此处。当 RequestLogHandler 无法打开指定的日志文件时,它会抛出一个异常,不幸的是,该异常被 org.eclipse.jetty.server.Server 捕获并吞噬(但首先记录,至少)。这意味着我没有明显的方法来判断日志处理程序是否已正确启动。

我是否缺少一种方法来检测处理程序何时无法启动?

I'm embedding Jetty in a similar manner as described here. When the RequestLogHandler can't open the specified logfile, it throws an exception which is unfortunately caught by org.eclipse.jetty.server.Server and swallowed (but logged first, at least). This means that there's no obvious way for me to tell if the log handler was started correctly.

Is there a way that I'm missing to detect when a handler couldn't start?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒废 2024-09-05 05:51:06

这个想法基于 的实现WebAppContext 您可以在其中使用 WebAppContext.getUnavailableException() 判断上下文是否初始化成功。

只需将 Server 和 Context 的默认实现替换为您自己的:

public static class MyContext extends Context {

    private Exception _exception;

    @Override
    protected void doStart() throws Exception {
        try {
            super.doStart();
        } catch (final Exception e) {
            _exception = e;
        }
    }

    @Override
    protected void doStop() throws Exception {
        try {
            super.doStop();
        } finally {
            _exception = null;
        }
    }

    public Exception getException() {
        return _exception;
    }

}

public static class MyServer extends Server implements InitializingBean {

    public void afterPropertiesSet() throws Exception {
        start();

        for (final Handler h : getHandlers()) {
            if (h instanceof MyContext) {
                final MyContext c = (MyContext) h;
                if (c.getException() != null) {
                    throw new RuntimeException("failed to init context " + c.getDisplayName(),
                            c.getException());
                }
            }
        }
    }
}

在 beans.xml 中,只需替换 org.mortbay.jetty.Server (并删除 init-method="start") 和 org.mortbay.jetty.servlet.Context 以及您自己的实现。

这段代码适用于 Jetty 6(正如您链接到的示例),因为这就是我所拥有的。虽然我没有测试它,但它与我们成功地与 WebAppContext 结合使用几乎相同。为了将其扩展到 RequestLogHandler,您可以对您正在使用的任何处理程序执行相同的操作,或者创建一个装饰器来包装任何处理程序。为此,您可能需要查看 org.mortbay.jetty.handler.HandlerWrapper 。

This idea is based on the implementation of WebAppContext where you can use WebAppContext.getUnavailableException() to determine whether the context was initialized successfully.

Simply replace the default implementation of Server and Context with your own:

public static class MyContext extends Context {

    private Exception _exception;

    @Override
    protected void doStart() throws Exception {
        try {
            super.doStart();
        } catch (final Exception e) {
            _exception = e;
        }
    }

    @Override
    protected void doStop() throws Exception {
        try {
            super.doStop();
        } finally {
            _exception = null;
        }
    }

    public Exception getException() {
        return _exception;
    }

}

public static class MyServer extends Server implements InitializingBean {

    public void afterPropertiesSet() throws Exception {
        start();

        for (final Handler h : getHandlers()) {
            if (h instanceof MyContext) {
                final MyContext c = (MyContext) h;
                if (c.getException() != null) {
                    throw new RuntimeException("failed to init context " + c.getDisplayName(),
                            c.getException());
                }
            }
        }
    }
}

In your beans.xml, simply replace org.mortbay.jetty.Server (and remove init-method="start") and org.mortbay.jetty.servlet.Context with your own implementations.

This code is for Jetty 6 though (as is the example you linked to), as that's what I have around. I didn't test it though, but it's pretty much the same as we are successfully using in conjunction with WebAppContext. In order to extend this to RequestLogHandler, you could either do the same for just any handler you are using or create a decorator to wrap any handler. You may want to look at org.mortbay.jetty.handler.HandlerWrapper for this purpose.

云归处 2024-09-05 05:51:06

修改jetty代码怎么样?您可以在 RequestLogHandler 的重要位置添加一些简单的 println 语句,这将指示您处理程序是否已启动。

How about modifying the jetty code? You could add some simple println statements in strategic places in the RequestLogHandler which would indicate to you whether or not the handler was started.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文