<!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> </DIV>
<DIV>Hi Matthias,</DIV>
<DIV> </DIV>
<DIV>Thanks for your reply. I wrote some test code and found a wired
problem.</DIV>
<DIV> </DIV>
<DIV>
<DIV>The init code is like</DIV>
<DIV> </DIV>
<DIV> factory = new ConnectionFactory();</DIV>
<DIV> connection = factory.newConnection(address);</DIV>
<DIV> channel = connection.createChannel();</DIV>
<DIV> channel.basicQos(1);</DIV>
<DIV> </DIV>
<DIV>And the receive code is like</DIV>
<DIV> </DIV>
<DIV> QueueingConsumer consumer = new QueueingConsumer(channel);</DIV>
<DIV> boolean autoAck = false; </DIV>
<DIV> </DIV>
<DIV> Queue.DeclareOk declare = channel.queueDeclarePassive("LBCQueue");</DIV>
<DIV> int consumerCount = declare.getConsumerCount();</DIV>
<DIV> if (consumerCount > 0)</DIV>
<DIV> return false;</DIV>
<DIV> </DIV>
<DIV> channel.basicConsume("LBCQueue", autoAck, consumer);</DIV></DIV>
<DIV> </DIV>
<DIV>First I started the first consumer and didn't publish any message.
</DIV>
<DIV><IMG src="cid:_Foxmail.0@E5021B12-DF76-4489-B95D-44A01851CE5E"></DIV>
<DIV> </DIV>
<DIV>
<DIV>Then I started the second consumer and the value of the variable declare is #method<queue.declare-ok>(queue=LBCQueue, message-count=0, consumer-count=1).</DIV>
<DIV>So far everything is OK.</DIV></DIV>
<DIV> </DIV>
<DIV>
<DIV>Now I publish 1 messge into the queue and restart the second consumer. The value of the variable declare is #method<queue.declare-ok>(queue=LBCQueue, message-count=0, consumer-count=1).</DIV>
<DIV>So far everything is still OK.</DIV></DIV>
<DIV><IMG src="cid:_Foxmail.1@F9AF6563-F0B6-497D-B6D2-96E6852953D1"></DIV>
<DIV> </DIV>
<DIV>
<DIV>Then I publish another message into the queue and restart the second consumer again. </DIV>
<DIV> </DIV>
<DIV><IMG src="cid:_Foxmail.2@751C887C-8CEB-41D7-94A1-9A17CA968CBA"></DIV>
<DIV>This time the value of declare is #method<queue.declare-ok>(queue=LBCQueue, message-count=1, consumer-count=0).</DIV>
<DIV>The message-count is correct but the consumer-count is changed from 1 to 0. I don't know why.
Is it a bug?</DIV>
<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-09-05 15:48</DIV>
<DIV><B>To:</B> <A href="mailto:johnson@edocom.cn">johnson</A></DIV>
<DIV><B>CC:</B> <A
href="mailto:rabbitmq-discuss@lists.rabbitmq.com">rabbitmq-discuss</A>; <A
href="mailto:francesco@rabbitmq.com">Francesco Mazzoli</A></DIV>
<DIV><B>Subject:</B> Re: [rabbitmq-discuss] How to achieve HA
consumers?</DIV></DIV></DIV>
<DIV>
<DIV>Johnson,</DIV>
<DIV> </DIV>
<DIV>On 05/09/12 05:32, johnson wrote:</DIV>
<DIV>> The channel.queueDeclarePassive method returns the consumer count of</DIV>
<DIV>> the specified channel,</DIV>
<DIV> </DIV>
<DIV>No it doesn't. queue.declarePassive returns the total number of active</DIV>
<DIV>consumers of the queue at that point in time. Regardless of what</DIV>
<DIV>connection/channel they were created on.</DIV>
<DIV> </DIV>
<DIV>That should be pretty clear from the docs -</DIV>
<DIV>http://www.rabbitmq.com/amqp-0-9-1-reference.html#queue.declare-ok.consumer-count</DIV>
<DIV>- which make no mention of the count being connection/channel specific.</DIV>
<DIV> </DIV>
<DIV>> So if I start two consumers [...] in two different machines [...] the</DIV>
<DIV>> channel.queueDeclarePassive method always return the consumer count</DIV>
<DIV>> == 1, not 2.</DIV>
<DIV> </DIV>
<DIV>I am not sure what you are expecting to see here. If you start two</DIV>
<DIV>consumers, the count they will see prior to consuming will be 0 or 1.</DIV>
<DIV> </DIV>
<DIV>As mentioned "You'd need to be mindful of races when consumers start </DIV>
<DIV>up.", i.e. if both consumers start up near simultaneously then they will </DIV>
<DIV>both see a count of 0.</DIV>
<DIV> </DIV>
<DIV>> I also try to find a API that can retrieve the existing connection or</DIV>
<DIV>> channel. Then the second consumer can reuse the connection or channel</DIV>
<DIV>> the first consumer created. But it seems no such APIs.</DIV>
<DIV> </DIV>
<DIV>A connection represents a point-to-point TCP connection. You can't </DIV>
<DIV>"reuse" that from a third point.</DIV>
<DIV> </DIV>
<DIV>Regards,</DIV>
<DIV> </DIV>
<DIV>Matthias.</DIV>
<DIV> </DIV></DIV></BODY></HTML>