1

I created a simple spring boot application with graceful shutdown functionality by adding the following to the properties file:

server.shutdown=graceful
spring.lifecycle.timeout-per-shutdown-phase=1m

It works as expected.
However, I would like to log information when the timeout expires before the running tasks were completed.
Is there a way to do this?

  • 2
    You can use the `@PreDestroy` annotation. A method annotated with `@PreDestroy` runs only once, just before Spring removes the bean from the application context. or you can refer to this StackOverflow question https://stackoverflow.com/questions/26678208/spring-boot-shutdown-hook – Akhil Ravindran Jun 12 '22 at 14:12
  • @AkhilRavindran yeah but how do I know if the bean is being destroyed because all tasks have finished or due to timeout having expired? – Anthony Omiunu Jun 12 '22 at 16:12

1 Answers1

0

Looking at DefaultLifecycleProcessor.LifecycleGroup which is using spring.lifecycle.timeout-per-shutdown-phase, if Spring fails to shutdown beans in specified time, then it'll log it:

if (latch.getCount() > 0 && !countDownBeanNames.isEmpty() && logger.isInfoEnabled()) {
    logger.info("Failed to shut down " + countDownBeanNames.size() + " bean" +
        (countDownBeanNames.size() > 1 ? "s" : "") + " with phase value " +
        this.phase + " within timeout of " + this.timeout + "ms: " + countDownBeanNames);
}

If you want customised implementation, you can look into implementing your own LifecycleProcessor.

Marcin Szulc
  • 1,171
  • 1
  • 10
  • 21