Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a ServletContextListener stop the Java EE application?

I have a ServletContextListener which performs some database management functions when my Java EE application starts. This runs in my application before JPA and other pieces of the application are started/loaded. If the database maintenance fails I am logging the errors. If the database maintenance fails the application will not function properly and I would like to halt the application.

How can I gracefully and correctly stop the application from ServletContextListener.contextInitialized?

Solution given by Viven below is close but not quite. When I throw a RuntimeException Glassfish is left in an inconsistent state where its admin console is not accessible but some process is still running and keeping port 3700 (IIOP?) open which then prevents a restart.

like image 594
Freiheit Avatar asked Jul 11 '11 22:07

Freiheit


People also ask

What is ServletContextListener in Java?

void contextInitialized(ServletContextEvent sce) Receives notification that the web application initialization process is starting. All ServletContextListeners are notified of context initialization before any filters or servlets in the web application are initialized.

What are the mechanism available in ServletContextListener interface?

It provides the notification for the initialization process of the web application. All ServletContextListeners are notified of context initialization before any filter or servlet in the web application is initialized.

Which listener's should be used to log the request?

A simple implementation of ServletRequestListener interface to log the ServletRequest IP address when request is initialized and destroyed.

Which of the following options is correct about filter and listener?

Filter is for the Servlet, intercepting the requests and responses. Listener is for the Web Application, doing important tasks on events in context-level, session-level etc. Save this answer.


2 Answers

In your listener, catch any exceptions and use servlet context attributes to store flags or other useful information about the error. You should probably also log something to indicate that the app is non-functional.

At this point, your options may be dictated by the architecture of your app. If all requests are handled by a single controller/dispatcher servlet, it might make sense to have its init method check the context attributes and throw an UnavailableException. Just be aware that the exception only applies to the specific servlet throwing it. This makes the approach less manageable if your app contains many servlets or allows direct access to other resources.

Another option would be to create a filter that intercepts every request, checks the context attributes and then throws an exception. Other variations are certainly possible.

like image 191
kschneid Avatar answered Sep 29 '22 01:09

kschneid


If your ServletContextListener throws an exception, the webapp won't load correctly and the application server may block all subsequent request (and respond with a 500 error).

It isn't exactly preventing the application to start, nor stopping the application, but it prevents any further usage of the app and could be useful in your case.

After proper verification in the spec, this behaviour isn't mandatory in the specification. The server may (not must) return 500 errors. This solution has to be used carefully, therefore.

See this Answer for a quote from the Servlet spec.

like image 34
Vivien Barousse Avatar answered Sep 29 '22 01:09

Vivien Barousse