google / guava

Google core libraries for Java
Apache License 2.0
50.26k stars 10.91k forks source link

Add Services.addShutdownHook(Service) #690

Open gissuebot opened 10 years ago

gissuebot commented 10 years ago

Original issue created by jer...@gustie.com on 2011-08-17 at 02:41 AM


Feature request for a one-liner that registers a shutdown hook which invokes the stopAndWait method on the supplied service. This is similar to something I saw in the Guice best practices guide for modules.

gissuebot commented 10 years ago

Original comment posted by kevinb9n on 2011-09-01 at 05:29 PM


Seems reasonable, although it might make more sense to group shutdown-related methods in a Shutdown or ShutdownHooks class.


Status: Accepted Labels: Type-Enhancement

gissuebot commented 10 years ago

Original comment posted by fry@google.com on 2011-12-10 at 04:15 PM


(No comment entered for this change.)


Labels: Package-Concurrent

gissuebot commented 10 years ago

Original comment posted by wasserman.louis on 2012-04-25 at 06:34 PM


I'm not sure what's being suggested here, or what the signature of this method would be...?

gissuebot commented 10 years ago

Original comment posted by d...@urbanairship.com on 2012-04-25 at 06:49 PM


wasserman.louis: I'd suggest stopServiceOnJvmShutdown() or stopServiceOnJvmShutdown(boolean)

gissuebot commented 10 years ago

Original comment posted by jer...@gustie.com on 2012-04-26 at 02:57 AM


I was originally thinking along the lines of a Services.addShutdownHook(Service) call which at a high level might look something like:

public static void addShutdownHook(final Service service) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            service.stopAndWait();
        }
    });
}

Though it would probably be cleaner if there was an inner class with a thread name. There probably also only needs to be one thread for all services which are being stopped on shutdown (which would also be more convenient for supporting a removeShutdownHook(Service) call).

The example I was referencing is at the bottom of this page: http://code.google.com/p/google-guice/wiki/ModulesShouldBeFastAndSideEffectFree

gissuebot commented 10 years ago

Original comment posted by jer...@gustie.com on 2012-05-30 at 12:45 AM


Scratch my previous comment about a single thread for all the services: it occurred to me that services can be stopped prior to shutdown in which case a strong reference should be avoided so the terminated service can be reclaimed. It also seems like there should not really be a need for removing the shutdown hook (the service itself could always be stopped ahead of time).

It looks like MoreExecutors already has some shutdown hook support for ExecutorService instances, I'm not sure if that could be combined like kevin suggested or if a general Services utility class makes more sense. A third option could an AbstractExitingService.

gissuebot commented 10 years ago

Original comment posted by kevinb@google.com on 2012-05-30 at 07:43 PM


(No comment entered for this change.)


Labels: -Type-Enhancement, Type-Addition

gissuebot commented 10 years ago

Original comment posted by jer...@gustie.com on 2013-01-31 at 12:31 AM


I recently updated my own implementation of this functionality to use a service listener: starting registers the hook (which is just a thread that calls stopAndWait), terminated and failed both remove it (and discards the reference to the thread). Since past state changes aren't replayed to listeners, I also make sure the hook is registered if the service is already up.