yf0994 / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Add Services.addShutdownHook(Service) #690

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
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.

Original issue reported on code.google.com by jer...@gustie.com on 17 Aug 2011 at 2:41

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

Original comment by kevin...@gmail.com on 1 Sep 2011 at 5:29

GoogleCodeExporter commented 9 years ago

Original comment by fry@google.com on 10 Dec 2011 at 4:15

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

Original comment by wasserman.louis on 25 Apr 2012 at 6:34

GoogleCodeExporter commented 9 years ago
wasserman.louis: I'd suggest stopServiceOnJvmShutdown() or 
stopServiceOnJvmShutdown(boolean)

Original comment by d...@urbanairship.com on 25 Apr 2012 at 6:49

GoogleCodeExporter commented 9 years ago
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

Original comment by jer...@gustie.com on 26 Apr 2012 at 2:57

GoogleCodeExporter commented 9 years ago
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.

Original comment by jer...@gustie.com on 30 May 2012 at 12:45

GoogleCodeExporter commented 9 years ago

Original comment by kevinb@google.com on 30 May 2012 at 7:43

GoogleCodeExporter commented 9 years ago
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.

Original comment by jer...@gustie.com on 31 Jan 2013 at 12:31

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:15

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:18

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:09