[rabbitmq-discuss] The idiom for shutting down a consumer in a thread task

Steve Powell steve at rabbitmq.com
Tue Jan 17 11:47:13 GMT 2012


Hi Mark,

This is very odd code -- basicConsume() *starts* a Consumer (which will run on
other threads not this one) it doesn't actually 'consume' anything itself. Why
are you looping on this in a Runnable?

The way to stop a consumer is to issue basicCancel(consumerTag). The consumerTag
is returned from the basicConsume() call, so you ought to remember this
somewhere (or you can supply a tag of your own devising). The Consumer will
receive a handleCancelOk() call as the last thing that drives it. Should you
wish to wait for this (it doesn't run synchronously with the cancel call), you
can use some Java latchery (CountdownLatch(1) for example) or some other Java
synchronisation mechanism to allow the caller to wait for the Consumer to
finish.

If you have an application running on another Java thread, then there is no
built-in clean Java way of stopping that thread. It is necessary to write your
own application-specific way of interrupting another thread's progress. Careful
use of the InterruptedException in Java can do it well, but you have to know
exactly what thread to interrupt and get all the parts to cooperate properly for
this to work reliably.

More information about your application might help us assist you further.

Steve Powell  (a chappy bunny)
----------some more definitions from the SPD----------
vermin (v.) Treating the dachshund for roundworm.
chinchilla (n.) Cooling device for the lower jaw.
socialcast (n.) Someone to whom everyone is speaking but nobody likes.

On 16 Jan 2012, at 14:43, Mark Petrovic wrote:

Hello.

I'm looking for the idiom for how to gracefully shutdown a consumer
running in a Java thread.  Given a Runnable's run() method that looks
like this

   @Override
   public void run() {
       while (doConsume) {
           try {
               channel.basicConsume(queue, false, busReader);
           } catch (IOException e) {
               throw new RuntimeException("Error in reader", e);
           }
       }
   }

what is the recommended way to cancel this logical task?  Call
connection.close() and exit out the catch-block?

Thank you.

-- 
Mark
_______________________________________________
rabbitmq-discuss mailing list
rabbitmq-discuss at lists.rabbitmq.com
https://lists.rabbitmq.com/cgi-bin/mailman/listinfo/rabbitmq-discuss



More information about the rabbitmq-discuss mailing list