[rabbitmq-discuss] How to tell Java Client running in a separate thread to stop consuming

Steve Powell steve at rabbitmq.com
Tue Oct 25 17:02:43 BST 2011


dusura,

As I said, there is no 'safe' general mechanism for stopping
one Java Thread from another.  The Interrupt mechanism in Java is
simply a convenience for *very simple cases*, or for debugging,
and cannot be regarded as safe on public interfaces.

In particular:

* changes to the design of a service might involve invoking more
  or fewer other threads (in which case you might be interrupting
  the wrong one);
* services might be invoked which themselves quietly consume
  interrupts;
* interrupts which fail to be trapped (due to timing considerations)
  can contaminate the thread for the next time it is run;
* it is not possible to tell the difference between spurious interrupts
  (part of the Java VM rules for wait(timeout)) and deliberate
  external interrupts.

It is much better to architect a solution. For example, in your case,
send a message to your client application which causes it to perform 
an orderly shutdown (including, one would expect, cancelling consumers 
and closing connections as we have discussed).  The client might
already have such a message architected, or if not, design one.

In the next release of RabbitMQ the thread upon which consumers
perform their callbacks need neither be the client thread, nor the 
connection thread, nor a thread dedicated to a particular channel.

In these circumstances, you are much better off calling a standard
interface like basicCancel().

Steve Powell  (a happy bunny)
----------some definitions from the SPD----------
Rigatoni n. A prime-ministerial grin.
Nigella n. The next bottle-size up from a Nebuchadnezzar.
Homily adv. Rather like a frenchman.

On 21 Oct 2011, at 01:28, dusura wrote:
> http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/2011-October/015649.html

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20111025/7dcc704e/attachment.htm>


More information about the rabbitmq-discuss mailing list