Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does spring have a shutdown process to put cleanup code?

Tags:

When my spring web app shuts down, is there an event I can wireup to somehow that I can perform some cleanup code to empty out some pools etc.

like image 977
Blankman Avatar asked Dec 21 '11 05:12

Blankman


People also ask

How do I gracefully shutdown Spring services?

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 shutdown hook in Spring?

registerShutdownHook() method in Spring In Java programming language you can create shutdown hooks, where you create a new thread and provide logic that is executed when the JVM is shutting down. Then you can register your thread class instance as a shutdown hook to the VM using Runtime.

Which actuator endpoint is used to shutdown the application?

Spring Boot Actuator comes with many production-ready features which include /shutdown endpoint. By default, all /shutdown endpoint is not enabled in the Actuator. To use this endpoint in our application, we should include spring-boot-starter-actuator starter and enable this endpoint in our application.


1 Answers

You Could use the following

  1. destroy-method as @amir75 recommends

  2. @PreDestroy annotation

  3. Implement DisposableBean and override destroy method.

All the deatails about these can be found at Disposable Callbacks.

like image 79
Aravind A Avatar answered Oct 07 '22 00:10

Aravind A