[rabbitmq-discuss] Data is feteched from the queue in random order

Kamal Nandan kamal.nandan at impetus.co.in
Wed Apr 20 10:25:12 BST 2011


Hi,

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?

I see that the data is dequeued in a random order. Is it an expected behavior?

Following is the block of code that I am using to dequeue:


      public int init(String hostName, int port, String userName, String password, String queueName) throws OceRecoverableException
      {
            if(true == LOG.isDebugEnabled()) {
                  LOG.debug("Executing RabbitMqConsumer::init");
            }

            try {
                  super.initialize(hostName, port, userName, password, queueName);
            } catch (IOException ioe) {
                  String message = "RabbitMQ consumer failed to create connection with the RabbitMQ server.";
                  LOG.error( message + " [ERROR: " + ioe.getMessage() + "]");
                  throw new OceRecoverableException(ioe);
            }

            this.queueingConsumer = new QueueingConsumer(this.channel); //channel is the member of parent class

            try {
                  this.channel.basicConsume(queueName, false, this.queueingConsumer);
            } catch(IOException ioe) {
                  String message = "RabbitMQ consumer failed to set the QueueingConsumer callback for consuming data.";
                  LOG.error( message + " [ERROR: " + ioe.getMessage() + "]");
                  throw new OceRecoverableException(ioe);
            }

            return 0;
      }


      public byte[] fetchMessage(int timeout) throws OceRecoverableException
      {
            LOG.debug("RabbitMqConsumer::fetchMessage");

            if(this.channel == null)
            {
                  throw new OceRecoverableException("The channel is not initialized; Initilize the channel by calling init method");
            }

            QueueingConsumer.Delivery delivery;

            try {
                  delivery = this.queueingConsumer.nextDelivery(timeout);
            } catch(Exception ex) {
                  String message = "Error occurred while fetching data from the RabbitMQ server.";
                  LOG.error( message + " [ERROR: " + ex.getMessage() + "]");
                  throw new OceRecoverableException(ex);
            }

            if(delivery != null)
            {
                  try {
                        this.channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
                  } catch (IOException ioe) {
                        String message = "Error occurred while sending acknowledgement to the RabbitMQ server for the fetched message.";
                        LOG.error( message + " [ERROR: " + ioe.getMessage() + "]");
                        throw new OceRecoverableException(ioe);
                  }

                  byte[] body = delivery.getBody();
                  return body;
            }
            else
            {
                  return null;
            }
      }

}


It would be great, if someone could guide me regarding this?  I want the data to be fetched in the order it was enqueued.

Thanks & Regards,
Kamal

________________________________

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

Follow us on www.twitter.com/impetuscalling or visit www.impetus.com to know more.


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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.rabbitmq.com/pipermail/rabbitmq-discuss/attachments/20110420/e73f3bb8/attachment.htm>


More information about the rabbitmq-discuss mailing list