Search code examples
jakarta-eewebsphere

Prevent Java EE application start on WebSphere on exception


Is there a way with WebSphere 6.1.0.25 to prevent an enterprise application from starting due to an exception? I have a ServletContextListener that I rigged to throw a RuntimeException during contextInitialized(). This produces a stack trace in the server's log, however the WebSphere console still shows the application as starting successfully, and the WAR resources can still be accessed. Is there a mechanism that will prevent WebSphere as flagging the application from starting successfully that can be generated by in-WAR artifacts?

I also tried setting a servlet to load-on-startup, and threw an exception from init(), and this did not produce the effect I was looking for.


Solution

  • Yes there is a way to do it but I believe it is still WebSphere specific. We had this same problem a few years ago, an application that required a lot of configuration would still show as started even when a major error occurred. The only way to tell was to look at the log file and see if any exceptions had occurred (and the web UI didn't work very well).

    The standard way to handle life-cycle, which seems to work on most containers, is the ServletContextListener but as you have found there is no graceful way to handle start-up errors.

    In WebSphere (for a few versions anyway) there is the concept of a startup bean. Rather than trying to explain it in detail as it was a couple of years ago that I did this you can have a look at this link http://publib.boulder.ibm.com/inforcenter...

    This essentially allows you to create an EJB bean which implements two methods:

    public boolean start() {
        return new Delegate().start();
    }
    
    public void stop() {
        new Delegate().stop();
    }
    

    If you return true from the start method it returns a successful start, otherwise returning false stops the application starting so in the console the application will indicate that it failed to start. We implemented a Delegate to do the work so we could wire it up to either the startup bean or a ServletContextListener if the container didn't implement startup beans.

    The only additional thing with startup beans is that in WebSphere you need to enable the Startup beans service in the Administrative Console or else they won't run and your app won't do any initialisation but will still show as started when you try and start it up. There is information on how to do that from the above page.

    I am also sure there is more detailed information in an IBM Redbook about this but at the moment I can't seem to locate it.