question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Application shutdown hooks

See original GitHub issue

Description

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:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
geoandcommented, Mar 29, 2022

Probably best cook something up and open a draft PR so we can discuss

1reaction
bpassoncommented, Mar 22, 2022

@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.

@ApplicationScoped
public class ServiceLifeCycle {

    @Inject
    SampleService service;

    void onStop(@Observes @Priority(1) ShutdownEvent ev) {
        // do blocking work/cleanup here
    }
}

The @Priority(1) will make sure it runs as first.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Adding Shutdown Hooks for JVM Applications - Baeldung
Shutdown hooks are basically initialized but unstarted threads. When the JVM begins its shutdown process, it will start all registered hooks in ...
Read more >
JVM Shutdown Hook in Java - GeeksforGeeks
Shutdown Hooks are a special construct that allows developers to plug in a piece of code to be executed when the JVM is...
Read more >
Design of Shutdown Hooks API
The following Q&A addresses some of the design issues of the Shutdown Hooks API. Why don't you provide information as to why the...
Read more >
Useful example of a shutdown hook in Java? - Stack Overflow
1 Answer 1 · Let the shutdown hook set some AtomicBoolean (or volatile boolean) "keepRunning" to false · (Optionally, .interrupt the working ...
Read more >
Java ShutdownHook - Javatpoint
A special construct that facilitates the developers to add some code that has to be run when the Java Virtual Machine (JVM) is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found