[rabbitmq-discuss] JAVA client / non daemon threads / shutdown hook

Steve Powell steve at rabbitmq.com
Thu May 24 16:06:47 BST 2012


Bartłomiej,

Shutdown hooks are, as I think you know, only executed when an orderly
shutdown is decided by the Jvm. In turn, this only happens when the last
non-daemon thread terminates, so the shutdown hooks you installed will
not be called, because the MainLoop thread is non-daemon.

With the proviso that the Consumer threads (if any) need some way to
shutdown first, the hook would work if the MainLoop thread is daemon.

It is possible to supply your own ExecutorService to run the Consumers,
and for you to supply one that uses daemon threads (instead of
non-daemon ones).

This is not a lot of effort, since the Executors classes in the standard
Java libraries allow you to supply a ThreadFactory when constructing a
standard ExecutorService. Therefore, by controlling this you can have
the Consumer callbacks running in daemon threads also. This means that
you can guarantee your hook will be called before the daemon threads are
killed during orderly shutdown.

Just be sure that you shutdown your executor service in your orderly
shutdown hook, since this will not be done on a connection close for a
user-supplied ExecutorService.

Steve Powell  (a happy bunny)
----------yet more definitions from the SPD----------
corrugate (n.) T.V. soap scandal.
olympic (n.) A camp road-digger.
jamboree (n.) A conserve made from French cheese.

On 24 May 2012, at 14:25, Steve Powell wrote:
> (elided)---------------------
> Would this meet your requirements?
> 
> Steve Powell  (a happy bunny)
> ----------yet more definitions from the SPD----------
> corrugate (n.) T.V. soap scandal.
> olympic (n.) A camp road-digger.
> jamboree (n.) A conserve made from French cheese.
> 
> On 24 May 2012, at 10:41, Bart Prokop wrote:
> 
>> (elided)-------------------------
>> 
>> On Wed, May 23, 2012 at 5:18 PM, Steve Powell <steve at rabbitmq.com> wrote:
>> (elided)---------------
>> On 18 May 2012, at 16:27, Bartłomiej Prokop wrote:
>> (elided)---------------------------


More information about the rabbitmq-discuss mailing list