<!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>&nbsp;</DIV>
<DIV>Your second approach is prefect and should work fine. I will choose this 
approach.</DIV>
<DIV>&nbsp;</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,&nbsp;each consumer needs to create&nbsp;a 
new&nbsp;connection and a&nbsp;new channel. In this way, the 
channel.queueDeclarePassive method&nbsp;always return the consumer count == 1, 
not 2. </DIV>
<DIV>&nbsp;</DIV>
<DIV>
<DIV>&nbsp;&nbsp;factory&nbsp;=&nbsp;new&nbsp;ConnectionFactory();</DIV>
<DIV>&nbsp;&nbsp;connection&nbsp;=&nbsp;factory.newConnection(address);</DIV>
<DIV>&nbsp;&nbsp;channel&nbsp;=&nbsp;connection.createChannel();</DIV></DIV>
<DIV>&nbsp;</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>&nbsp;</DIV>
<HR style="WIDTH: 210px; HEIGHT: 1px" align=left color=#b5c4df SIZE=1>

<DIV><SPAN>johnson</SPAN></DIV>
<DIV>&nbsp;</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>&nbsp;<A href="mailto:matthias@rabbitmq.com">Matthias 
Radestock</A></DIV>
<DIV><B>Date:</B>&nbsp;2012-08-31&nbsp;19:39</DIV>
<DIV><B>To:</B>&nbsp;<A 
href="mailto:rabbitmq-discuss@lists.rabbitmq.com">Discussions about 
RabbitMQ</A></DIV>
<DIV><B>CC:</B>&nbsp;<A href="mailto:francesco@rabbitmq.com">Francesco 
Mazzoli</A>; <A href="mailto:johnson@edocom.cn">johnson</A></DIV>
<DIV><B>Subject:</B>&nbsp;Re: [rabbitmq-discuss] How to achieve HA 
consumers?</DIV></DIV></DIV>
<DIV>
<DIV>On&nbsp;31/08/12&nbsp;12:27,&nbsp;Francesco&nbsp;Mazzoli&nbsp;wrote:</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;The&nbsp;second&nbsp;consumer&nbsp;polls&nbsp;the&nbsp;first&nbsp;in&nbsp;some&nbsp;way&nbsp;and&nbsp;starts&nbsp;consuming&nbsp;when&nbsp;it</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;realises&nbsp;the&nbsp;first&nbsp;consumer&nbsp;is&nbsp;dead.&nbsp;&nbsp;This&nbsp;is&nbsp;simple&nbsp;and&nbsp;should&nbsp;serve&nbsp;you</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;right,&nbsp;but&nbsp;it's&nbsp;not&nbsp;that&nbsp;nice&nbsp;performance&nbsp;wise.</DIV>
<DIV>&nbsp;</DIV>
<DIV>A&nbsp;passive&nbsp;queue.declare,&nbsp;which&nbsp;returns&nbsp;the&nbsp;consumer_count&nbsp;could&nbsp;</DIV>
<DIV>accomplish&nbsp;this.&nbsp;You'd&nbsp;need&nbsp;to&nbsp;be&nbsp;mindful&nbsp;of&nbsp;races&nbsp;when&nbsp;consumers&nbsp;start&nbsp;up.</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;*&nbsp;You&nbsp;create&nbsp;a&nbsp;second&nbsp;queue&nbsp;that&nbsp;we'll&nbsp;call&nbsp;"token&nbsp;queue",&nbsp;and&nbsp;you&nbsp;publish&nbsp;one</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dummy&nbsp;message&nbsp;in&nbsp;it,&nbsp;with&nbsp;acks&nbsp;enabled.&nbsp;&nbsp;Then,&nbsp;a&nbsp;consumer&nbsp;wishing&nbsp;to&nbsp;consume</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from&nbsp;your&nbsp;queue,&nbsp;will:</DIV>
<DIV>&gt;</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Consume&nbsp;from&nbsp;the&nbsp;token&nbsp;queue</DIV>
<DIV>&nbsp;</DIV>
<DIV>More&nbsp;specifically,&nbsp;start&nbsp;consuming&nbsp;and&nbsp;wait&nbsp;for&nbsp;the&nbsp;token&nbsp;message&nbsp;to&nbsp;</DIV>
<DIV>arrive.&nbsp;And&nbsp;then...</DIV>
<DIV>&nbsp;</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;Start&nbsp;consuming&nbsp;from&nbsp;the&nbsp;actual&nbsp;queue</DIV>
<DIV>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;-&nbsp;When&nbsp;shutting&nbsp;down,&nbsp;publish&nbsp;a&nbsp;dummy&nbsp;message&nbsp;to&nbsp;the&nbsp;token&nbsp;queue</DIV>
<DIV>&nbsp;</DIV>
<DIV>No&nbsp;need&nbsp;for&nbsp;the&nbsp;last&nbsp;step&nbsp;(and&nbsp;in&nbsp;fact&nbsp;it&nbsp;will&nbsp;lead&nbsp;to&nbsp;token&nbsp;</DIV>
<DIV>duplication).&nbsp;Simply&nbsp;leave&nbsp;the&nbsp;message&nbsp;unack'ed&nbsp;and&nbsp;let&nbsp;the&nbsp;automatic&nbsp;</DIV>
<DIV>requeue&nbsp;on&nbsp;connection&nbsp;closure/failure&nbsp;take&nbsp;care&nbsp;of&nbsp;the&nbsp;rest.</DIV>
<DIV>&nbsp;</DIV>
<DIV>Matthias.</DIV>
<DIV>&nbsp;</DIV></DIV></BODY></HTML>