<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content="text/html; charset=utf-8" http-equiv=Content-Type>
<STYLE>
BLOCKQUOTE {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px; MARGIN-LEFT: 2em
}
OL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
UL {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
P {
        MARGIN-TOP: 0px; MARGIN-BOTTOM: 0px
}
BODY {
        LINE-HEIGHT: 1.5; FONT-FAMILY: 宋体; COLOR: #000080; FONT-SIZE: 10.5pt
}
</STYLE>
<META name=GENERATOR content="MSHTML 8.00.7601.17874"></HEAD>
<BODY style="MARGIN: 10px">
<DIV>Thanks Matthias and Francesco.</DIV>
<DIV> </DIV>
<DIV>Your second approach is prefect and should work fine. I will choose this
approach.</DIV>
<DIV> </DIV>
<DIV>However, I have little concern about the first approach. The
channel.queueDeclarePassive method returns the consumer count of the specified
channel, but the total number of the consumers connected to the rabbitmq server.
So if I start two consumers using the following code in two different machines,
i.e., two different JVMs, each consumer needs to create a
new connection and a new channel. In this way, the
channel.queueDeclarePassive method always return the consumer count == 1,
not 2. </DIV>
<DIV> </DIV>
<DIV>
<DIV> factory = new ConnectionFactory();</DIV>
<DIV> connection = factory.newConnection(address);</DIV>
<DIV> channel = connection.createChannel();</DIV></DIV>
<DIV> </DIV>
<DIV>I also try to find a API that can retrieve the existing connection or
channel. Then the second consumer can reuse the connection or channel the first
consumer created. But it seems no such APIs.</DIV>
<DIV> </DIV>
<HR style="WIDTH: 210px; HEIGHT: 1px" align=left color=#b5c4df SIZE=1>
<DIV><SPAN>johnson</SPAN></DIV>
<DIV> </DIV>
<DIV
style="BORDER-BOTTOM: medium none; BORDER-LEFT: medium none; PADDING-BOTTOM: 0cm; PADDING-LEFT: 0cm; PADDING-RIGHT: 0cm; BORDER-TOP: #b5c4df 1pt solid; BORDER-RIGHT: medium none; PADDING-TOP: 3pt">
<DIV
style="PADDING-BOTTOM: 8px; PADDING-LEFT: 8px; PADDING-RIGHT: 8px; BACKGROUND: #efefef; COLOR: #000000; FONT-SIZE: 12px; PADDING-TOP: 8px">
<DIV><B>From:</B> <A href="mailto:matthias@rabbitmq.com">Matthias
Radestock</A></DIV>
<DIV><B>Date:</B> 2012-08-31 19:39</DIV>
<DIV><B>To:</B> <A
href="mailto:rabbitmq-discuss@lists.rabbitmq.com">Discussions about
RabbitMQ</A></DIV>
<DIV><B>CC:</B> <A href="mailto:francesco@rabbitmq.com">Francesco
Mazzoli</A>; <A href="mailto:johnson@edocom.cn">johnson</A></DIV>
<DIV><B>Subject:</B> Re: [rabbitmq-discuss] How to achieve HA
consumers?</DIV></DIV></DIV>
<DIV>
<DIV>On 31/08/12 12:27, Francesco Mazzoli wrote:</DIV>
<DIV>> * The second consumer polls the first in some way and starts consuming when it</DIV>
<DIV>> realises the first consumer is dead. This is simple and should serve you</DIV>
<DIV>> right, but it's not that nice performance wise.</DIV>
<DIV> </DIV>
<DIV>A passive queue.declare, which returns the consumer_count could </DIV>
<DIV>accomplish this. You'd need to be mindful of races when consumers start up.</DIV>
<DIV> </DIV>
<DIV>> * You create a second queue that we'll call "token queue", and you publish one</DIV>
<DIV>> dummy message in it, with acks enabled. Then, a consumer wishing to consume</DIV>
<DIV>> from your queue, will:</DIV>
<DIV>></DIV>
<DIV>> - Consume from the token queue</DIV>
<DIV> </DIV>
<DIV>More specifically, start consuming and wait for the token message to </DIV>
<DIV>arrive. And then...</DIV>
<DIV> </DIV>
<DIV>> - Start consuming from the actual queue</DIV>
<DIV>> - When shutting down, publish a dummy message to the token queue</DIV>
<DIV> </DIV>
<DIV>No need for the last step (and in fact it will lead to token </DIV>
<DIV>duplication). Simply leave the message unack'ed and let the automatic </DIV>
<DIV>requeue on connection closure/failure take care of the rest.</DIV>
<DIV> </DIV>
<DIV>Matthias.</DIV>
<DIV> </DIV></DIV></BODY></HTML>