user8681
user8681

Reputation:

Get Jetty to gracefully shut down a failed-to-start webapp context

I have a simple Jetty server that runs a webapp from a web.xml. That file has some servlets, and a ServletContextListener. When everything starts up fine, this works perfectly, including graceful shutdown on exit.

But, if starting that webapp fails for some reason, I want the server to still gracefully shut down. That includes calling the contextDestroyed methods of the relevant ServletContextListeners.

WebAppContext context = ...;
Server server = ...;
server.start();

if (!context.isAvailable()) {
  // startup failed
  server.stop(); // I would expect this to call contextDestroyed
}

How do I get Jetty to properly clean up after itself?

If it matters, when running the same web application as a war in Tomcat, it works fine and actually shuts down correctly on a failed startup.

Upvotes: 0

Views: 351

Answers (1)

Jan
Jan

Reputation: 1287

You don't say what version of Jetty you are running. I've checked against the most recent version of jetty, and the behaviour is as expected: if a servlet fails to initialise correctly, then the webapp is marked as unavailable, and when server.stop() is called, the contextDestroy listener method is called. Can you provide more details, such as jetty version, log output, at what point the webapp startup fails etc.

Upvotes: 1

Related Questions