How to shutdown Spring Boot applications gracefully

This article will teach you how to gracefully shut-down a Spring Boot application using the Actuator endpoints. We will also show how to configure a minimal security layer to protect access to this resource.

Configuring Spring Boot dependencies

Firstly, build a Spring Boot project which includes the following dependencies:

spring boot shutdown gracefully

Within the project, you will now find the following Spring Boot starters:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
</dependency>

Configuring application properties

Next, we we need to enable the shutdown endpoint. We will also expose all available Actuator Endpoints:

management.endpoint.shutdown.enabled=true
management.endpoint.info.enabled=true
management.endpoints.web.exposure.include=* 

Adding the Security Configuration Class

Finally, we need a Class to handle the Authentication.

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@Configuration
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
    .csrf().disable()
    .authorizeRequests().anyRequest().authenticated()
    .and()
    .httpBasic();
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {                
        auth.inMemoryAuthentication()
        .withUser("admin").password("{noop}password").roles("USER");
    }

}

Testing application shutdown

With our Classes and Configuration in place, we can now test the shutdown of our Spring Boot application using the Actuator /shutdown endpoint.

mvn install spring-boot:run

Next, shutdown the application as follows:

$ curl -i -X POST http://localhost:8080/actuator/shutdown -u admin:password

{"message":"Shutting down, bye..."}

Conclusion

This article discussed how to gracefully shutdown a Spring Boot application using the Actuator and the Security Starter. You can find the application source code here: https://github.com/fmarchioni/masterspringboot/tree/master/security/shutdown