javamultithreadingspring-bootstart-stop-daemon

How to gracefuly shutdown a Spring Boot application by start-stop-daemon


We have a multithreaded Spring Boot Application, which runs on Linux machine as a daemon. When I try to stop the application by start-stop-daemon like this

start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME

The SIGTERM signal is sent and application immetiately ends. However I want the application to wait, until every thread finishes it's work.

Is there any way, how to manage what happens, when SIGTERM signal is received?


Solution

  • Spring Boot app registers a shutdown hook with the JVM to ensure that the ApplicationContext is closed gracefully on exit. Create bean (or beans) that implements DisposableBean or has method with @PreDestroy annotation. This bean will be invoked on app shutdown.

    http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit