Application shutdown hooks
See original GitHub issueDescription
I would like have the ability to register shutdown hooks to have the Quarkus shutdown tasks wait until all registered hooks have terminated or timed out. This creates the possibility to implement graceful shutdown for end-user tasks to properly shutdown before Quarkus starts shutting down e.g. entity managers, executors and other services that could still be needed while properly shutting down the end-user tasks .
Of course there is @PreDestroy, but that doesn’t seem to block the Quarkus shutdown process in my experience. It can be done now listening to the ShutdownEvent
, but it seems a bit cumbersome and I am not sure it would work in all circumstances.
@ApplicationScoped
public class ServiceLifeCycle {
@Inject
SampleService service;
void onStop(@Observes ShutdownEvent ev) {
service.shutdown()
.completeOnTimeout(null, 10, TimeUnit.SECONDS)
.join();
}
}
Quarkus would need to guarantee all user defined shutdownhooks are called in the order they are registered and finished or timed out before starting the Quarkus shutdown tasks.
Implementation ideas
One idea could be to do this using an extension and have it register ShutdownBuildItems
, but I think it deserves a more prominent position. I would rather see something like the following options:
Quarkus.addShutdownHook( Runnable r )
Quarkus.addShutdownHook( Runnable r, long timeout, TimeUnit unit )
Quarkus.addShutdownHook( CompletableFuture<Void> cf )
Quarkus.addShutdownHook( CompletableFuture<Void> cf, long timeout, TimeUnit unit )
Issue Analytics
- State:
- Created 3 years ago
- Comments:10 (4 by maintainers)
Probably best cook something up and open a draft PR so we can discuss
@liuzhaozhao the best you can do for time being is do something like below. If that doesn’t work for you, you could try the solution suggested by @pedrolopix here.
The
@Priority(1)
will make sure it runs as first.