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 aPostConstruct
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?