jiafu
jiafu

Reputation: 6546

how to make sure something happen once the tomcat stop or restart

Sometimes, tomcat restart will cause my application stop immediately so that my consuming data in oracle db keep one error state. So I want know if has any methods to help handling the state once the tomcat stop. Due to unknown the actual events happen on the stop procedure of tomcat. I can't know how to make sure something happen when tomcat stopped.

Upvotes: 1

Views: 2105

Answers (2)

Ricardo Marimon
Ricardo Marimon

Reputation: 10687

Perhaps you can register a LifecycleListener for your tomcat application (link). A lifecycle listener will be called when your context is loaded and right after it is being closed.

Here is a more involved explanation link. You might want to look at the contextInitialized and contextDestroyed methods of the ServletContextListener interface.

Upvotes: 2

Paul Cichonski
Paul Cichonski

Reputation: 388

If you are doing a normal restart you should be able to register a shutdownhook and have it run before the server kills the app.

Check out the javadocs for more detailed info: http://docs.oracle.com/javase/6/docs/api/java/lang/Runtime.html#addShutdownHook(java.lang.Thread).

You can add a shutdown hook like this:

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable(){
    public void run() {
         // clean-up logic
    }
}));

Upvotes: 2

Related Questions