<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;"><DIV>Good evening.</DIV>
<DIV>After consuming few published messages, if I bring down the RabbitMQ broker (hard failure) and bring it back, the number of messages saved to queue is incorrect.</DIV>
<DIV>For example, I publish 100 messages. After consuming 10 messages, I bring down the broker and bring it back. Run: rabbitmqctl list_queues messages_ready, I see the count of messages_ready as 100 instead of 90. </DIV>
<DIV>Could you please let me know if there is anything wrong with the following Producer and consumer code:</DIV>
<DIV> </DIV>
<DIV>Thanks</DIV>
<DIV>Venkat</DIV>
<DIV>===========================</DIV>
<DIV>TestProducer.java</DIV>
<DIV>-------------------------</DIV><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>
<P align=left>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>class</B></FONT></FONT><FONT size=2> TestProducer {</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>static</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>void</B></FONT></FONT><FONT size=2> main(String[] args) {</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>for</B></FONT></FONT><FONT size=2>(</FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>int</B></FONT></FONT><FONT size=2> i=0; i < 100; i++) {</DIV>
<P align=left>String msg = </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"M"</FONT></FONT><FONT size=2> + i;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>try</B></FONT></FONT><FONT size=2> {</DIV>
<P align=left>RabbitMQProducer.</FONT><I><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>INSTANCE</I></FONT></FONT><FONT size=2>.run(msg);</DIV>
<P align=left>} </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>catch</B></FONT></FONT><FONT size=2>(Exception e) {</DIV>
<P align=left>System.</FONT><I><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>out</I></FONT></FONT><FONT size=2>.println(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"exception occurred"</FONT></FONT><FONT size=2>);</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>break</B></FONT></FONT><FONT size=2>;</DIV>
<P align=left>}</DIV>
<P align=left>}</DIV>
<P align=left>}</DIV>
<P align=left>}</DIV>
<P align=left>=============================================================</DIV>
<P align=left>RabbitMQProducer.java:</DIV>
<P align=left>--------------------------------</DIV><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>
<P align=left>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>class</B></FONT></FONT><FONT size=2> RabbitMQProducer {</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>static</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>final</B></FONT></FONT><FONT size=2> RabbitMQProducer </FONT><I><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>INSTANCE</I></FONT></FONT><FONT size=2> = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>new</B></FONT></FONT><FONT size=2> RabbitMQProducer();</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>private</B></FONT></FONT><FONT size=2> RabbitMQProducer() {}</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>void</B></FONT></FONT><FONT size=2> run(String msg) {</DIV>
<P align=left>String hostName = </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"localhost"</FONT></FONT><FONT size=2>;</DIV>
<P align=left>String port = </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"5672"</FONT></FONT><FONT size=2>;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>int</B></FONT></FONT><FONT size=2> portNumber = Integer.<I>valueOf</I>(port);;</DIV>
<P align=left>Connection conn = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>null</B></FONT></FONT><FONT size=2>;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>try</B></FONT></FONT><FONT size=2> {</DIV>
<P align=left></DIV>
<P align=left>ConnectionFactory cf = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>new</B></FONT></FONT><FONT size=2> ConnectionFactory(); </DIV>
<P align=left>cf.setHost(hostName);</DIV>
<P align=left>cf.setPort(portNumber);</DIV>
<P align=left>cf.setUsername(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"guest"</FONT></FONT><FONT size=2>);</DIV>
<P align=left>cf.setPassword(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"guest"</FONT></FONT><FONT size=2>);</DIV>
<P align=left>conn = cf.newConnection();</DIV>
<P align=left>Channel ch = conn.createChannel();</DIV>
<P align=left>ch.exchangeDeclare(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.exchange"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"topic"</FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>true</B></FONT></FONT><FONT size=2>);</DIV>
<P align=left>ch.queueDeclare(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.queue"</FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>true</B></FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>false</B></FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>false</B></FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>null</B></FONT></FONT><FONT size=2>);</DIV>
<P align=left>ch.txSelect();</DIV>
<P align=left>ch.basicPublish(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.exchange"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.queue"</FONT></FONT><FONT size=2>, MessageProperties.</FONT><I><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>PERSISTENT_BASIC</I></FONT></FONT><FONT size=2>, msg.getBytes());</DIV>
<P align=left>ch.txCommit();</DIV>
<P align=left>ch.close();</DIV>
<P align=left>conn.close();</DIV>
<P align=left></DIV>
<P align=left>} </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>catch</B></FONT></FONT><FONT size=2> (IOException e) {</DIV>
<P align=left>e.printStackTrace();</DIV>
<P align=left>}</DIV>
<P align=left></DIV>
<P align=left>}</DIV>
<P align=left></DIV>
<P align=left>}</DIV>
<P align=left>=================================================================</DIV><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>
<P align=left>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>class</B></FONT></FONT><FONT size=2> RabbitConsumer {</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>private</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>int</B></FONT></FONT><FONT size=2> </FONT><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>count</FONT></FONT><FONT size=2> = 0;</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>private</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>void</B></FONT></FONT><FONT size=2> runConsumer() {</DIV>
<P align=left>Connection conn = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>null</B></FONT></FONT><FONT size=2>;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>try</B></FONT></FONT><FONT size=2> {</DIV>
<P align=left></DIV>
<P align=left>String hostName = </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"localhost"</FONT></FONT><FONT size=2>;</DIV>
<P align=left>String port = </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"5672"</FONT></FONT><FONT size=2>;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>int</B></FONT></FONT><FONT size=2> portNumber = Integer.<I>valueOf</I>(port);</DIV>
<P align=left>ConnectionFactory cf = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>new</B></FONT></FONT><FONT size=2> ConnectionFactory(); </DIV>
<P align=left>cf.setHost(hostName);</DIV>
<P align=left>cf.setPort(portNumber);</DIV>
<P align=left>cf.setUsername(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"guest"</FONT></FONT><FONT size=2>);</DIV>
<P align=left>cf.setPassword(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"guest"</FONT></FONT><FONT size=2>);</DIV>
<P align=left>conn = cf.newConnection();</DIV>
<P align=left>Channel channel = conn.createChannel();</DIV>
<P align=left>channel.exchangeDeclare(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.exchange"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"topic"</FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>true</B></FONT></FONT><FONT size=2>);</DIV>
<P align=left>channel.queueBind(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.queue"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.exchange"</FONT></FONT><FONT size=2>, </FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"#"</FONT></FONT><FONT size=2>);</DIV>
<P align=left>Consumer consumer = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>new</B></FONT></FONT><FONT size=2> QueueingConsumer(channel);</DIV>
<P align=left>channel.basicQos(1);</DIV>
<P align=left>channel.basicConsume(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"mdb.testq.queue"</FONT></FONT><FONT size=2>, </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>false</B></FONT></FONT><FONT size=2>, consumer);</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>boolean</B></FONT></FONT><FONT size=2> running = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>true</B></FONT></FONT><FONT size=2>;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>while</B></FONT></FONT><FONT size=2>(running) {</DIV>
<P align=left></DIV>
<P align=left>QueueingConsumer.Delivery delivery;</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>try</B></FONT></FONT><FONT size=2> {</DIV>
<P align=left>delivery = ((QueueingConsumer) consumer).nextDelivery();</DIV>
<P align=left><U>Thread.<I>currentThread</I>().<I>sleep</I>(500)</U>;</DIV>
<P align=left></FONT><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>count</FONT></FONT><FONT size=2>++;</DIV>
<P align=left>System.</FONT><I><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>out</I></FONT></FONT><FONT size=2>.println(</FONT><FONT color=#2a00ff size=2><FONT color=#2a00ff size=2>"count: "</FONT></FONT><FONT size=2>+</FONT><FONT color=#0000c0 size=2><FONT color=#0000c0 size=2>count</FONT></FONT><FONT size=2>);</DIV>
<P align=left>channel.basicAck(delivery.getEnvelope().getDeliveryTag(), </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>false</B></FONT></FONT><FONT size=2>);</DIV>
<P align=left>} </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>catch</B></FONT></FONT><FONT size=2> (Exception e) {</DIV>
<P align=left>e.printStackTrace();</DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>break</B></FONT></FONT><FONT size=2>;</DIV>
<P align=left>} </DIV>
<P align=left>}</DIV>
<P align=left>} </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>catch</B></FONT></FONT><FONT size=2> (IOException e) {</DIV>
<P align=left>e.printStackTrace();</DIV>
<P align=left>}</DIV>
<P align=left></DIV>
<P align=left>}</DIV>
<P align=left></DIV>
<P align=left></FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>public</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>static</B></FONT></FONT><FONT size=2> </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>void</B></FONT></FONT><FONT size=2> main(String[] args) {</DIV>
<P align=left>RabbitConsumer rc = </FONT><B><FONT color=#7f0055 size=2><FONT color=#7f0055 size=2>new</B></FONT></FONT><FONT size=2> RabbitConsumer();</DIV>
<P align=left>rc.runConsumer();</DIV>
<P align=left>}</DIV>
<P align=left>}</DIV></FONT></FONT></FONT></td></tr></table><br>