0

我正在运行一个嵌入式 Jetty 8 服务器,它在启动时会加载一些 *.war 文件:

for (File aWebAppDirectory : aWebAppDirectories) {
      if (aWebAppDirectory.exists() && aWebAppDirectory.isDirectory()) {
        for (File warFile : aWebAppDirectory.listFiles(new WarFileFilter())) {            String basename = warFile.getName().replaceFirst("\\.war$", "");
         fHandlers.addHandler(new WebAppContext(warFile.getAbsolutePath(), "/" + basename));
       }
     }
   }

这些战争文件对类路径中可能存在或不存在的一些类有一些依赖关系。

现在,如果我的一个 servlet 缺少依赖项,我的整个嵌入式 Jetty 服务就会失败。(因为 NoClassDefFoundExceptions)

我需要一种方法,它允许我捕获失败的 servlet 的异常并且根本不激活它们。我正在寻找与当 servlet 加载失败时 TomCat 所做的相同的事情:它仍然加载其余部分。

在谷歌上搜索了一段时间后,我没有找到任何解决方案。

有人知道我如何使用嵌入式 Jetty 8 解决这个问题吗?

4

1 回答 1

0

如果有人好奇我是如何解决这个问题的,我只是确保我所有的 servlet 都有一个基本上没有依赖关系的包装 servlet。包装器尝试使用依赖项初始化委托并显式检查 NoClassDefFountException。如果发生这种情况,委托将设置为 null,并且对包装器接口的所有调用都将导致异常。所以在高层次上:

public class ServletWrapper extends HttpServlet{
  private ServletDelegate fDelegate;
  //If this is false, the delegate does not work, and we should not forward anything to it.
  private boolean fAvailable = false;

  public ServletWrapper(){
    try{
      fDelegate = new ServletDelegate();
      fAvailable = true;
    } catch (NoClassDefFoundError e) {
      fAvailable = false;
    }
  }

  @Override
  protected void doPost( HttpServletRequest request, HttpServletResponse response )
      throws ServletException, IOException {
    if ( !fAvailable || fDelegate==null ) {
      response.sendError( HttpServletResponse.SC_SERVICE_UNAVAILABLE, LSP_MISSING_ERROR_MESSAGE );
      return;
    }

   fDelegate.doPost(request,response);
  }

}

这很简单,而且很有效。

于 2014-08-25T08:37:31.837 回答