【问题标题】:Webapp startup fails but Jetty LifeCycle claims "started"Webapp 启动失败,但 Jetty LifeCycle 声称“已启动”
【发布时间】:2010-12-01 21:29:37
【问题描述】:

我正在使用嵌入式 Jetty 来启动标准 Java webapp。我的启动器是这样的:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.component.LifeCycle.Listener;
import org.eclipse.jetty.webapp.WebAppContext;

...

Listener listener = ...;
Server server = new Server(8080);
WebAppContext webapp = new WebAppContext();
...
webapp.addLifeCycleListener(listener);

server.setHandler(webapp);
server.start();
...

这一切都很好,因为我可以启动我的应用并浏览到它,一切似乎都在工作。

但现在我正在尝试向我的启动器添加错误报告。我暂时将我的 web 应用程序设置为在 ServletContextListenercontextInitialized() 方法中引发异常。抛出异常,我收到一条日志消息

ERROR org.eclipse.jetty.util.log Failed startup of context WebAppContext@...

但我的 LifeCycleListener 没有收到任何失败事件。实际上,它接收到一个启动事件,并且传递给侦听器的WebAddContext 对于LifeCycle#isFailed() 返回false,对于LifeCycle#isRunning() 返回true。

浏览到我的 web 应用会导致 503 Service Unavailable 错误。

这发生在 Jetty 版本 7.0.1.v20091125 和 7.2.1.v20101111 中。见Jetty 7 api docs

【问题讨论】:

    标签: java web-applications jetty embedded-jetty


    【解决方案1】:

    根据我对 Heri 的回答,WebAppContext 会吞下异常。否则它们将被 AbstractLifeCycle 捕获并发出失败事件。这让我大部分时间到达那里:

    public class ThrowyWebAppContext extends WebAppContext {
        @Override
        protected void doStart() throws Exception {
            super.doStart();
            if (getUnavailableException() != null) {
                throw (Exception) getUnavailableException();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果我没记错的话,上下文元素的失败生命周期不会传播到上下文本身的生命周期(就像失败的上下文不会传播到服务器本身的生命周期一样)。还要检查上下文的SecurityHandlerServletHandlerSessionHandler 的生命周期。

      【讨论】:

      • 我已经尝试将听众附加到所有没有运气的人身上。我在看source for WebAppContext。此 Log.warn 与我看到的日志消息匹配(除了我在错误级别看到它......)。如果我没看错,这个错误就完全被吞没了。当子类doStart() 中抛出异常时,AbstractLifeCycle 中的相关部分设置失败。
      • 我想我会尝试扩展 WebAppContext 使其更容易处理异常。
      • 这或多或少有用。请参阅我发布的答案。感谢您的提示。
      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 2013-02-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-25
      • 1970-01-01
      • 2021-10-02
      相关资源
      最近更新 更多