Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring shutdown event that fires immediately before ApplicationContext is destroyed?

I am looking for an interceptor or a trigger to know that, all the context beans are destroyed and the applicationcontext instance is about to destroy itself. So I can do one process at the end of the application lifetime.

There is this event type ContextClosedEvent, which is close to the thing that I wanna do but, it throws the event after destruction of beans. I thing it comes with the close() method of the applicationcontext. So it doesn't fit to my need

Any ideas?

Regards

Ali

like image 622
Neron Avatar asked Jun 05 '12 18:06

Neron


People also ask

How do you trigger gracefully shutdown spring boot application?

In the Spring Boot Document, they said that 'Each SpringApplication will register a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit. ' When I click ctrl+c on the shell command, the application can be shutdown gracefully.

What is graceful shutdown in spring boot?

During a graceful shutdown Spring Boot allows some grace period to the application to finish all the current requests or processes. Once, the grace period is over the unfinished processes or requests are just killed. By default, Spring Boot allows a 30 seconds graceful shutdown timeout.

Which of the following commands is used to shut down a spring boot application?

Close ApplicationContext Another option to shutdown the Spring Boot application is to close Spring ApplicationContext using SpringApplication . SpringApplication. run(String…) method returns ApplicationContext as a ConfigurableApplicationContext We can use close() method to close ApplicationContext programmatically.


1 Answers

You can use registerShutDownHook() method of the abstract application context class. For more details have a look at this.

UPDATE

Then you should try @PreDestroy annotation on top of the method where you want to run something in the end when the spring context is about to destroy.

Hope this helps you. Cheers.

like image 150
Japan Trivedi Avatar answered Sep 28 '22 05:09

Japan Trivedi