0

While requeuing the message we want the message to be placed at the start/front of the queue. This means if I have in queue as "D,C,B,A" and process A and then want to put back in Queue at start, my queue should looks like this:- "A,D,C,B". So, I should be able to process B, and since A moved at start of the queue should process it at end.

Interestingly, when tried with native AMQP library of rabbitMQ it wworks as expected above.

However, when we do it through spring AMQP library , the message still remains whereever it was, does not go to the front of the queue.

Here is the code which we tried:

public void onMessage(Message message, com.rabbitmq.client.Channel channel) throws Exception {
    if(new String(message.getBody()).equalsIgnoreCase("A")){
        System.out.println("Message ===" + new String(message.getBody()));
       channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
    } else{
        System.out.println("Message ===" + new String(message.getBody()));
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
    }       
}

Any idea why it does not work in Spring but works in the rabbimq amqp native library ?

Spring AMQP version : spring-amqp-1.4.5.RELEASE Rabbitmq amqp client version : 3.5.1

Younes El Ouarti
  • 2,200
  • 2
  • 20
  • 31

1 Answers1

0

RabbitMQ was changed in version 2.7 to requeue messages at the head of the queue. Previous versions requeued at the tail of the queue - see here.

Your observation is because Spring AMQP sets the prefetch to 1 by default (by calling basicQos) - which only allows 1 message to be outstanding at the client. If basicQos is not called, the broker will send all four messages to the client so it will appear that the rejected message went to the back of the queue if the queue is empty due to prefetch.

If you set the prefetchCount to at least 4, you will see the same behavior.

EDIT

If you really want to queue at the beginning you can use retry together with a RepublishMessageRecoverer.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179