simonC
simonC

Reputation: 4337

Controlling Spring Boot graceful shutdown

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.

Upvotes: 7

Views: 6824

Answers (1)

Niraj Sonawane
Niraj Sonawane

Reputation: 11115

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:

Upvotes: 3

Related Questions