Spring Boot Graceful Shutdown

Add graceful shutdown support to Spring Boot applications.

License

License

Categories

Categories

Spring Boot Container Microservices
GroupId

GroupId

me.silvernine
ArtifactId

ArtifactId

spring-boot-graceful-shutdown
Last Version

Last Version

1.1.3
Release Date

Release Date

Type

Type

jar
Description

Description

Spring Boot Graceful Shutdown
Add graceful shutdown support to Spring Boot applications.
Project URL

Project URL

https://github.com/silvernine/spring-boot-graceful-shutdown
Source Code Management

Source Code Management

https://github.com/SilverNine/spring-boot-graceful-shutdown

Download spring-boot-graceful-shutdown

How to add to project

<!-- https://jarcasting.com/artifacts/me.silvernine/spring-boot-graceful-shutdown/ -->
<dependency>
    <groupId>me.silvernine</groupId>
    <artifactId>spring-boot-graceful-shutdown</artifactId>
    <version>1.1.3</version>
</dependency>
// https://jarcasting.com/artifacts/me.silvernine/spring-boot-graceful-shutdown/
implementation 'me.silvernine:spring-boot-graceful-shutdown:1.1.3'
// https://jarcasting.com/artifacts/me.silvernine/spring-boot-graceful-shutdown/
implementation ("me.silvernine:spring-boot-graceful-shutdown:1.1.3")
'me.silvernine:spring-boot-graceful-shutdown:jar:1.1.3'
<dependency org="me.silvernine" name="spring-boot-graceful-shutdown" rev="1.1.3">
  <artifact name="spring-boot-graceful-shutdown" type="jar" />
</dependency>
@Grapes(
@Grab(group='me.silvernine', module='spring-boot-graceful-shutdown', version='1.1.3')
)
libraryDependencies += "me.silvernine" % "spring-boot-graceful-shutdown" % "1.1.3"
[me.silvernine/spring-boot-graceful-shutdown "1.1.3"]

Dependencies

compile (9)

Group / Artifact Type Version
org.apache.tomcat.embed : tomcat-embed-core Optional jar
org.slf4j : slf4j-api jar
org.springframework : spring-beans jar
org.springframework : spring-context jar
org.springframework : spring-core jar
org.springframework.boot : spring-boot jar
org.springframework.boot : spring-boot-actuator Optional jar
org.springframework.boot : spring-boot-autoconfigure jar
org.springframework.boot : spring-boot-configuration-processor Optional jar

provided (2)

Group / Artifact Type Version
com.fasterxml.jackson.core : jackson-annotations jar
org.projectlombok : lombok jar 1.18.10

test (9)

Group / Artifact Type Version
junit : junit jar
org.assertj : assertj-core jar 3.13.2
org.asynchttpclient : async-http-client jar 2.10.2
org.mockito : mockito-core jar
org.springframework : spring-web jar
org.springframework.boot : spring-boot-starter-actuator jar
org.springframework.boot : spring-boot-starter-logging jar
org.springframework.boot : spring-boot-starter-test jar
org.springframework.boot : spring-boot-starter-web jar

Project Modules

There are no modules declared in this project.

Spring Boot Graceful Shutdown

Maven Central

This project adds graceful shutdown behavior to Spring Boot.

The project referred to "https://github.com/timpeeters/spring-boot-graceful-shutdown" and tried to improve it further.

Versions

Multiple branches are maintained to support multiple Spring Boot versions. The following tables show the relation between the Spring Boot version and the Spring Boot Actuator Server Config version.

Spring Boot Spring Boot Actuator Server Config Branch
2.1.x 2.1.x master

Flow

  1. The JVM receives the SIGTERM signal and starts shutting down the Spring container.
  2. A Spring EventListener listens for a ContextClosedEvent and is invoked once the shutdown is started.
  3. The EventListener updates a Spring Boot HealthIndicator and puts it "out of service".
  4. The context shutdown is delayed using a Thread.sleep to allow the load balancer to see the updated HealthIndicator status and stop forwarding requests to this instance.
  5. When the Thread.sleep is finished, the Tomcat container is gracefully shutdown. First by pausing the connector, no longer accepting new request. Next, by allowing the Tomcat thread pool a configurable amount of time to finish the active threads.
  6. Finally, the Spring context is closed.

Limitations

  1. Currently this project only supports Tomcat as embedded web container for Spring Boot. ( Undertow and/or Jetty are not yet supported. )
  2. org.springframework.boot:spring-boot-starter-actuator is required

Installation

for Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>X.X.X.RELEASE</version>
</dependency>
<dependency>
    <groupId>me.silvernine</groupId>
    <artifactId>spring-graceful-shutdown</artifactId>
    <version>X.X.X</version>
</dependency>

for Gradle:

compile 'org.springframework.boot:spring-boot-starter-actuator:X.X.X.RELEASE'
compile 'me.silvernine:spring-boot-graceful-shutdown:X.X.X'

Configuration

Key Default value Description
graceful.shutdown.enabled false Indicates whether graceful shutdown is enabled or not.
graceful.shutdown.timeout 60s The number of seconds to wait for active threads to finish before shutting down the Tomcat connector.
graceful.shutdown.wait 30s The number of seconds to return "out of service" on the health page before starting the graceful shutdown.

Alternative implementations

We found several alternatives for graceful shutdown behavior in Spring Boot.

References

Versions

Version
1.1.3
1.1.2
1.1.1