Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

How we can registerShutdown hook in web application?

Is there any whays to register it in web.xml or in applicationContext.xml?

I know that if we are using application with main class it's simple.

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
    context.registerShutdownHook();

But what about web application? As it uses ContextListener

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
160 views
Welcome To Ask or Share your Answers For Others

1 Answer

registerShutdownHook() in standalone (non-web) application:

The @PreDestroy annotation is used on bean method to be notified when the bean is being removed from the context or when the context is shutting down.

Shut down event is fired when context.close() or context.registerShutdownHook() is invoked.

@Component(value="someBean")
public class SomeBean {

    @PreDestroy
    public void destroy() {
        System.out.println("Im inside destroy...");
    }
}

I hope you already know this.


registerShutdownHook() in web application:

In a web application, DispatcherServlet/ContextListener creates the ApplicationContext and it will close the context when the server shutdown. You don't need to explicitly invoke context.close() or context.registerShutdownHook().

When the server shutdown, @PreDestory methods on your bean will be notified automatically.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...