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

Categories

0 votes
162 views
in Technique[技术] by (71.8m points)

java - Register shutDownHook in web application

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

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

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...