Java EE 应用程序启动失败

发布于 2024-11-29 08:00:39 字数 1560 浏览 1 评论 0原文

如果在应用程序初始化期间发生异常,是否有任何方法可以阻止 Java EE 应用程序启动?我基本上正在寻找一种方法使应用程序进入“j2ee.state.failed”状态(根据JSR-77) 在 ServletContextListener单例 Startup 应用程序初始化期间的 bean。

EJB 规范 似乎表明,如果在初始化期间发生异常>Singleton bean,应用程序将继续启动并运行,不会出现错误;然而,只有 Bean 本身可能处于无法调用的状态。不幸的是,这不是我正在寻找的行为。

4.8.4 单例错误处理

在 Singleton 初始化期间发生的错误被认为是致命的,并且必须导致 Singleton 实例被丢弃。可能的初始化错误包括注入失败、PostConstruct 方法抛出的系统异常或 PostConstruct 方法容器管理的事务未能成功提交。如果单例无法初始化,则尝试调用单例会导致第 3.4.3 节和第 3.4.4 节中定义的异常。

Servlet 规范 的要求有点模糊,看起来不是要求容器以任何特定方式运行,但仅仅是建议(通过使用术语“可以”)Web 模块继续启动,但任何请求都应导致内部服务器错误。同样,不幸的是这不是我正在寻找的行为。如果 Web 应用程序无法处理任何请求,为什么它还要继续启动并显示为正在运行?

11.6 监听器异常

容器可能会使用 HTTP 状态代码 500 来响应对 Web 应用程序的所有后续请求,以指示应用程序错误。

根据我的经验,我发现应用程序服务器以不同的方式处理此要求。在这些情况下,某些容器实际上会阻止应用程序启动,而其他容器只会抑制异常并响应带有 500 错误的请求,如规范中所建议的。

如果初始化期间发生异常,我是否忽略了规范中会阻止应用程序启动的任何部分?

Is there any method to prevent a Java EE application from starting if an exception occurs during application initialization? I'm basically looking for a way to cause the application to enter a "j2ee.state.failed" state (per JSR-77) following an unhandled exception thrown from a ServletContextListener or a Singleton Startup bean during application initialization.

The EJB specification seems to indicate that if an exception occurs during initialization of a Singleton bean, the application will continue to start and run without error; however, only the bean itself may be in a state where it cannot be invoked. Unfortunately, this is not the behavior that I'm looking for.

4.8.4 Singleton Error Handling

Errors occurring during Singleton initialization are considered fatal and must result in the discarding of the Singleton instance. Possible initialization errors include injection failure, a system exception thrown from a PostConstruct method, or the failure of a PostConstruct method container-managed transaction to successfully commit. If a singleton fails to initialize, attempted invocations on the Singleton result in an exception as defined by Section 3.4.3 and Section 3.4.4.

The Servlet specification is a bit more ambiguous in its requirements, seemingly not requiring a container to behave in any particular way, but merely suggesting (through the use of the term "may") that the web module continue to start, but any requests should result in an internal server error. Again, this is unfortunately not the behavior I'm looking for. Why should the web application continue to start and appear to be running if it cannot handle any requests?

11.6 Listener Exceptions

The container may respond to all subsequent requests to the Web application with an HTTP status code 500 to indicate an application error.

In my experience, I've seen application servers handle this requirement differently. Some containers will in fact prevent the application from starting in these cases, whereas others will merely suppress the exception and respond to requests with 500 errors, as suggested in the specification.

Am I overlooking any part of the specification that would prevent an application from starting if an exception occurs during initialization?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鯉魚旗 2024-12-06 08:00:39

我认为这取决于您使用的应用程序服务器。正如您所提到的,如何处理无效状态的部署取决于他。部署失败时不会处于活动状态,因此您必须提供一项服务来查找部署在成功启动时应提供的提示。如果没有,您可以通知管理员。但这都是特定于服务器的或需要外部工具。

在一些项目中,我们使用 [nagios][1] 和 [rhq][2] 来检查部署。例如,nagios 在 JBossAS 的 jmx-console 上检查所需的部署。当部署的应用程序在部署后几秒钟内没有显示时,IT 人员就会收到通知。

I think it depends on the application server you're using. It's up to him how to handle deployments with an invalid state, as you mentioned. The deployments aren't active when they fail so you've got to provide an service which looks for hints which your deployment should provide when started successfully. If not you can notify the administrators. But this is all serverspecific or needs an external tool.

In some projects we've used [nagios][1] and [rhq][2] for checking the deployments. For example nagios checks on JBossAS the jmx-console for the needed deployments. When the application which is deployed isn't shown within some seconds after deployment the it-staff is notified.

梦回旧景 2024-12-06 08:00:39

+1 给模因答案。也许它可能会有所帮助:根据我的经验组合,如果带有注释 @Startup 和 @Singleton 的 bean 在 @PostConstruct 方法中抛出异常,它会阻止整个应用程序(ear)在 JBoss AS 7.1.1 上启动

+1 to meme answer. And maybe it may help: in my expirience combination if bean with annotations @Startup and @Singleton throws exception in @PostConstruct method, it prevent whole application (ear) to start on JBoss AS 7.1.1

2024-12-06 08:00:39

对于 Web 应用程序 - 为什么不创建一个 servlet 过滤器,它将检查您自己的“失败”标志,并在引发此标志时为任何请求返回 500?从容器的角度来看,您的应用程序当然会启动。

in case of web apps - why not to create a servlet filter, which will check your own "failed" flag, and return 500 for any request, if this flag is raised? from container point of view, your app will be started of course.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文