<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<style>
<!--
@font-face
        {font-family:Calibri}
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri","sans-serif"}
a:link, span.MsoHyperlink
        {color:blue;
        text-decoration:underline}
a:visited, span.MsoHyperlinkFollowed
        {color:purple;
        text-decoration:underline}
span.EmailStyle17
        {font-family:"Calibri","sans-serif";
        color:windowtext}
.MsoChpDefault
        {}
@page Section1
        {margin:1.0in 1.0in 1.0in 1.0in}
div.Section1
        {}
-->
</style>
</head>
<body lang="EN-US" link="blue" vlink="purple">
<div class="Section1">
<p class="MsoNormal">Hi,</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I am trying to dequeue data from the RabbitMQ. Why is it so that the data is not dequeued in the same order in which it was enqueued into the queue?</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">I see that the data is dequeued in a random order. Is it an expected behavior?</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Following is the block of code that I am using to dequeue:</p>
<div style="border:none; border-bottom:dotted windowtext 3.0pt; padding:0in 0in 1.0pt 0in">
<p class="MsoNormal" style="border:none; padding:0in"> </p>
</div>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> </span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>public</b> <b>int</b> init(String hostName, <b>int</b> port, String userName, String password, String queueName)
<b>throws</b> OceRecoverableException</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>if</b>(<b>true</b> == <i>LOG</i>.isDebugEnabled()) {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.debug("Executing RabbitMqConsumer::init");</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>try</b> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>super</b>.initialize(hostName, port, userName, password, queueName);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }
<b>catch</b> (IOException ioe) {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> String message = "RabbitMQ consumer failed to create connection with the RabbitMQ server.";</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.error( message + " [ERROR: " + ioe.getMessage() + "]");</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>throw</b> <b>new</b> OceRecoverableException(ioe); </span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>this</b>.queueingConsumer = <b>new</b> QueueingConsumer(<b>this</b>.channel); <b>
<span style="color:red">//channel is the member of parent class</span></b></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>try</b> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>this</b>.channel.basicConsume(queueName, <b>false</b>, <b>this</b>.queueingConsumer);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }
<b>catch</b>(IOException ioe) {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> String message = "RabbitMQ consumer failed to set the QueueingConsumer callback for consuming data.";</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.error( message + " [ERROR: " + ioe.getMessage() + "]");</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>throw</b> <b>new</b> OceRecoverableException(ioe); </span>
</p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>return</b> 0;</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> </span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>public</b> <b>byte</b>[] fetchMessage(<b>int</b> timeout) <b>throws</b> OceRecoverableException</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.debug("RabbitMqConsumer::fetchMessage"); </span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>if</b>(<b>this</b>.channel == <b>null</b>)</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>throw</b> <b>new</b> OceRecoverableException("The channel is not initialized; Initilize the channel by calling init method");
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> QueueingConsumer.Delivery delivery;</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>try</b> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> delivery =
<b>this</b>.queueingConsumer.nextDelivery(timeout);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }
<b>catch</b>(Exception ex) {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> String message = "Error occurred while fetching data from the RabbitMQ server.";</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.error( message + " [ERROR: " + ex.getMessage() + "]");</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>throw</b> <b>new</b> OceRecoverableException(ex);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>if</b>(delivery != <b>null</b>)</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>try</b> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>this</b>.channel.basicAck(delivery.getEnvelope().getDeliveryTag(), <b>false</b>);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }
<b>catch</b> (IOException ioe) {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> String message = "Error occurred while sending acknowledgement to the RabbitMQ server for the fetched message.";</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<i>LOG</i>.error( message + " [ERROR: " + ioe.getMessage() + "]");</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>throw</b> <b>new</b> OceRecoverableException(ioe);</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>byte</b>[] body = delivery.getBody();</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>return</b> body;</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>else</b></span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> {</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">
<b>return</b> <b>null</b>;</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> }</span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New""> </span></p>
<p class="MsoNormal" style="text-autospace:none"><span style="font-size:10.0pt; font-family:"Courier New"">}</span></p>
<div style="border:none; border-bottom:solid windowtext 1.0pt; padding:0in 0in 1.0pt 0in">
<p class="MsoNormal" style="border:none; padding:0in"> </p>
</div>
<p class="MsoNormal"> </p>
<p class="MsoNormal">It would be great, if someone could guide me regarding this? I want the data to be fetched in the order it was enqueued.</p>
<p class="MsoNormal"> </p>
<p class="MsoNormal">Thanks & Regards,</p>
<p class="MsoNormal">Kamal</p>
</div>
<br>
<hr>
<font face="Verdana" color="Gray" size="1"><br>
Are you exploring a Big Data Strategy ? Listen to this recorded webinar on Planning your Hadoop/ NoSQL projects for 2011 at www.impetus.com/featured_webinar?eventid=37
<br>
<br>
Follow us on www.twitter.com/impetuscalling or visit www.impetus.com to know more.
<br>
<br>
<br>
NOTE: This message may contain information that is confidential, proprietary, privileged or otherwise protected by law. The message is intended solely for the named addressee. If received in error, please destroy and notify the sender. Any use of this email
is prohibited when received in error. Impetus does not represent, warrant and/or guarantee, that the integrity of this communication has been maintained nor that the communication is free of errors, virus, interception or interference.<br>
</font>
</body>
</html>