如果 servlet 上下文初始化失败,则跳过部署或停止 Web 应用程序

Skip deploying or stop web application if servlet context initialization fails

在我们的项目中,我们有几个基于 Spring 的模块,它们作为 Web 应用程序部署在 WAS 上。如果 Spring 上下文初始化失败(即 ContextLoaderListener#contextInitializedDispatcherServlet#init 抛出异常),我们需要跳过部署或停止模块。现在,如果发生这种情况,应用程序已部署并启动,但 returns HTTP 500 用于任何请求。

Websphere 8.5.5

相关问题:

你可以使用jenkins + maven。 在你的测试下添加你需要检查的部分,比如junit。 那么如果这个模块没有通过测试,jenkins 将不会部署它。

但我更喜欢在部署前修复错误

这个 APAR 似乎是相关的:

https://www-01.ibm.com/support/docview.wss?uid=swg1PI58875

来自 APAR 文本:

Listener exceptions typically should not stop the application
from starting up for service. However, some applications depend
on their listeners to do the necessary setup before the
application is started for service. Such applications prefer to
stop the application from starting up when there is any
exception in their listeners.

问题总结

The WebContainer Container code was modified to provide an
option to stop the application when there is any listener
exception during the application starting up process.

A new WebContainer custom property needs to be set to enable the
behavior provided by this APAR:

For Full Profiles

com.ibm.ws.webcontainer.stopappstartuponlistenerexception = true
(default is false)

For Liberty Profile

stopappstartuponlistenerexception=true

The fix for this APAR is currently targeted for inclusion in
WebSphere Application Server fix packs 8.5.5.11 and  9.0.0.2,
and Liberty 16.0.0.3

有关其他信息,请参阅 APAR link。

有一个非常相似的问题。 问题是 - webfear - 抱歉无法抗拒 ;-) 不会在启动时初始化所有内容。

为了触发受控请求,我在应用程序的启动中添加了一个 ScheduledEJB。这个 bean 本身触发了对定义的 URL 的 http 请求,它本身触发了:

  • 要在链中初始化的任何过滤器
  • 任何需要的上下文都已初始化

这本身确保我的应用程序(EAR 或 WAR)在部署后得到非常快速的测试。这适用于每分钟少量请求

如果您在高负载下工作,意味着每秒处理大量请求,您需要选择不同的方法。 在这种情况下,我在应用程序的@Startup 中添加了一个轮询机制,它每秒轮询一次或 250 毫秒(取决于应用程序的负载)。 这种对服务器的触发确保了我的@Startup bean 是第一个触发应用程序中可能的初始化问题的 bean。如果发生这种情况,我初始化了一个过滤器,它总是向请求者报告 500(或更好的拟合错误)。

当然,一拿到 500 就停止你的发射 bean,否则你的管理员可能想杀了你。 (发生在我身上,因为我产生了大量或监控问题;-)) 当然在常规操作上,在你的应用程序正常启动后,你也应该禁用轮询

在您的应用程序代码的顶层查找 try-catch,它正在捕获 Spring 异常并允许应用程序继续 运行。

如果允许抛出的 Spring 异常传播到堆栈的顶部,JVM 将停止并且它无法保持 运行,据我所知。