启动时中止 java webapp
我的 web 应用程序是部署到 websphere 服务器中的更大 EAR 的一部分。 该服务器在同一虚拟服务器上托管许多其他应用程序。 我的 web 应用程序在 servletContextListener->contextInitialized 方法中有一些初始化/运行状况检查。 如果初始化/运行状况检查失败,我想让网络应用程序不可用。 做到这一点的可靠方法是什么? 从 contextInitialized 中抛出 RuntimeException 就足够了吗? EAR 的其余部分预计仍然可用吗? 谢谢。
My webapp is part of a larger EAR that is deployed into a websphere server. The server hosts number of other apps on the same virtual server. My webapp has some initialisation/health checks in a servletContextListener->contextInitialized method. I want to make the webapp unavailable if initialisation/health checks fail. What is a realiable way of doing this? Will throwing a RuntimeException from within contextInitialized suffice? Is the rest of the EAR still expected to be available? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议从
ServletContextListener.contextInitialized
抛出RuntimeException
。Servlet 2.3 对此还不是很清楚,但 Servlet 2.4 添加了以下细节:
由于它表示 servlet 引擎“可能”禁用对应用程序的访问,因此您可能会找到执行其他操作的服务器。 然而,Tomcat 和 WebLogic 都禁用了该应用程序,我能想到的唯一合理的事情就是忽略该异常。 我看不出有哪个容器能够做到这一点,所以您最好自己在 WebSphere 中测试它。
I'd recommend throwing a
RuntimeException
fromServletContextListener.contextInitialized
.Servlet 2.3 wasn't very clear on this, but Servlet 2.4 added the following detail:
Since it says that the servlet engine "may" disable access to application, you might find a server that does something else. However, Tomcat and WebLogic both disable the application, and the only other reasonable thing I can think of would be to ignore the exception. I can't see a container that did that being very popular—so you'd better test it in WebSphere yourself.
抛出 RuntimeException 可能只会导致该 servlet 不可用。 更安全的方法可能是实现类似 Spring 拦截器的东西,如果检查没有成功,它将转发到错误页面或其他页面。 这样,您不需要阻止应用程序加载,而是可以在运行时更优雅地处理它。
Throwing a RuntimeException will probably make only that servlet unavailable. A safer way might be to implement something like a Spring interceptor that will forward to an error page or something if the checks didn't pan out. That way, you don't need to prevent the app from loading, but can handle it more gracefully at run time.