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. If I run the application in a production machine, I have to use the command
java -jar ProApplicaton.jar
. But I can't close the shell terminal, otherwise it will close the process.
If I run command like nohup java -jar ProApplicaton.jar &
, I can't use ctrl+c
to shutdown it gracefully.
What is the correct way to start and stop a Spring Boot Application in the production environment?
If you are using maven you could use the Maven App assembler plugin.
The daemon mojo (which embed JSW) will output a shell script with start/stop argument. The
stop
will shutdown/kill gracefully your Spring application.The same script can be used to use your maven application as a linux service.
You can make the springboot application to write the PID into file and you can use the pid file to stop or restart or get the status using a bash script. To write the PID to a file, register a listener to SpringApplication using ApplicationPidFileWriter as shown below :
Then write a bash script to run the spring boot application . Reference.
Now you can use the script to start,stop or restart.
Spring Boot provided several application listener while try to create application context one of them is ApplicationFailedEvent. We can use to know weather the application context initialized or not.
Add to above listener class to SpringApplication.
As of Spring Boot 1.5, there is no out-of-the box graceful shutdown mechanism. Some spring-boot starters provide this functionality:
I am the author of nr. 1. The starter is named "Hiatus for Spring Boot". It works on the load balancer level, i.e. simply marks the service as OUT_OF_SERVICE, not interfering with application context in any way. This allows to do a graceful shutdown and means that, if required, the service can be taken out of service for some time and then brought back to life. The downside is that it doesn't stop the JVM, you will have to do it with
kill
command. As I run everything in containers, this was no big deal for me, because I will have to stop and remove the container anyway.Nos. 2 and 3 are more or less based on this post by Andy Wilkinson. They work one-way - once triggered, they eventually close the context.
As to @Jean-Philippe Bond 's answer ,
here is a maven quick example for maven user to configure HTTP endpoint to shutdown a spring boot web app using spring-boot-starter-actuator so that you can copy and paste:
1.Maven pom.xml:
2.application.properties:
All endpoints are listed here:
3.Send a post method to shutdown the app:
Security Note:
if you need the shutdown method auth protected, you may also need
configure details:
I don't expose any endpoints and start (with nohup in background and without out files created through nohup) and stop with shell script(with KILL PID gracefully and force kill if app is still running after 3 mins). I just create executable jar and use PID file writer to write PID file and store Jar and Pid in folder with same name as of application name and shell scripts also have same name with start and stop in the end. I call these stop script and start script via jenkins pipeline also. No issues so far. Perfectly working for 8 applications(Very generic scripts and easy to apply for any app).
Main Class
YML FILE
Here is the start script(start-appname.sh):
Here is the stop script(stop-appname.sh):