gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:30:07 24 4
gpt4 key购买 nike

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

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

最佳答案

这个想法是基于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

关于java - 检测嵌入 Jetty 时处理程序何时无法启动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2621194/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com