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

Kamal kamal.nandan at gmail.com
Wed Apr 20 10:30:18 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;
            }
      }

}

******************************************************************************************************************************************************

For more detailed code, you can access the following link:
http://pastebin.com/7N9ksQkk

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


More information about the rabbitmq-discuss mailing list