Controlling Spring Boot graceful shutdown
Asked Answered
B

1

7

Is there a way in Spring Boot to control the graceful shutdown of the app?

I know that you can have @PreDestroy methods in beans, but how can you control the ordering in which those @PreDestroy methods are called?

You can have multiple beans depending on each other will the shutdown of the context look for this dependency already and call the @PreDestroy methods in the right order or not?

For example what I would like to accomplish is:
1) stop listening for new requests on rest endpoints
2) prevent rabbit message listeners to accept new messages
3) wait for all processing that has started before the shutdown but is not finished yet.

Bensky answered 8/2, 2019 at 1:31 Comment(0)
A
3

Spring Boot 2.3.0 has added support for graceful shutdown.

You can enable graceful shutdown by setting up server.shutdown=graceful property.

To configure the timeout period you can use:

spring.lifecycle.timeout-per-shutdown-phase=20s

Spring Boot documentation


If you cannot upgrade to Spring Boot 2.3, then you can check the below project:

Absquatulate answered 28/6, 2020 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.