users@glassfish.java.net

How to stop application deployment after failed initialization

From: <glassfish_at_javadesktop.org>
Date: Fri, 08 May 2009 09:18:32 PDT

I'm using an instance of 'ServletContextListener' to do some initialization work not just on the first request but immediately after deployment. As it happens in real world, such kind of initialization can fail (because a required database is down or alike).

Now the question that arises: Like in any other runtime environment, the most natural reaction upon an initialization failure would be do abort the application start (which in case of an web app means: stop deployment or somehow shutdown the non-working web-app, of course after emitting an appropriate error message, which would be sent back to the deployment tool or go to the log file). Unfortunately the 'contextInitialized' method doesn't have a signature that allows an exception to be thrown.

Does anybody have an idea how to accomplish this? A fallback solution that I can see would be to set an error flag and upon this redirect any request to an additional web page that says that even though requests being served the application is nonetheless not working. Not too elegant.

most probably the code used is clear to readers of this forum, but to be on the safe side:

public class ApplicationInitializer implements ServletContextListener {
   public void contextInitialized(ServletContextEvent event) {
      // how to handle fatal error here (no Exception in method signature)
   }
}

in web.xml:
   <listener>
      <listener-class>
         packagepath.ApplicationInitializer
      </listener-class>
   </listener>
[Message sent by forum member 'frankwhofmann' (frankwhofmann)]

http://forums.java.net/jive/thread.jspa?messageID=345550