I want to shutdown spring boot application programmatically from multiple applications running on single tomcat server without stopping tomcat.
I have searched over Google found couple of solutions like
System.exit(0)
and
SpringbootApplication.exit()
causing shutting down tomcat. I don't want to shutdown tomcat. Just the particular application.
How can I do that.. programmatically is there any way to do this.
Pls help!
One way is to use the actuator.
Add this dependency in your pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Add these properties in your yml / properties file
management.endpoint.shutdown.enabled=true
endpoints.shutdown.enabled=true
management.endpoints.web.exposure.include=*
With this in place, you can hit this rest endpoint to shutdown the application
http://host:port/actuator/shutdown
This is a POST call. If you are using spring security in your application, then you might want to make a few adjustments to allow this endpoint to go through. you can invoke post call using curl like
curl -X POST http://host:port/actuator/shutdown
You can do that by registering an endpoint(highly secure though) to which your applications can send request for shutdown and you can code endpoint as below:
ConfigurableApplicationContext ctx = SpringApplication.run(YourApplicationClassName.class, args);
int exitCode = SpringApplication.exit(ctx, new ExitCodeGenerator() {
@Override
public int getExitCode() {
// no errors
return 0;
}
});
Security - I would recommend that if you want to send termination signal to an app through other apps, you can use app token that uniquely identify the apps priviledged to shutdown your application.
One way to do it is to kill the app process .
First, the application has to write its PID to a file (shutdown.pid):
SpringApplicationBuilder app = new SpringApplicationBuilder(Application.class)
.web(WebApplicationType.NONE);
app.build().addListeners(new ApplicationPidFileWriter("./bin/shutdown.pid"));
app.run();
Then you can create a file (shutdown.bat) and add this line:
kill $(cat ./bin/shutdown.pid)
The execution of shutdown.bat extracts the Process ID from the shutdown.pid file and uses the kill command to terminate the Boot application.
ps: stolen from here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.