user3594922
user3594922

Reputation:

How do I go about performing a graceful shutdown (Java)?

I understand that graceful shutdowns complete pre-existing requests whilst denying new requests.

How would I go about performing a graceful shutdown of a Java application running on an Apache Tomcat instance? Unsure whether I need to extend my current application or if it's a simple terminal command.

Upvotes: 2

Views: 1715

Answers (1)

Zielu
Zielu

Reputation: 8552

I hope you mean a WebApp running on your Tomcat.

You can define in the web.xml a listener:

<listener>
    <listener-class>
         your.project.ContextListener
     </listener-class>
</listener>

which can handle shutting down of the context (WebApp).

import javax.servlet.ServletContextListener;
public class ContextListener implements ServletContextListener {

   @Override
   public void contextDestroyed(ServletContextEvent sce) {
    cleanYourResources();
   }

}

When Tomcat is being shutdown or when in Tomcat manager you stop the webapp, the contextDestroyed event is sent and your listener which can do necessary housekeeping. Typically, it involves stopping background threads (like scheduled tasks), saving cache contents, closing resources pools etc).

Upvotes: 1

Related Questions