RabbitTemplate receive and requeue

2019-07-06 19:57发布

问题:

I would like to receive messages from a queue an immediately dequeue them, in fact I would like to mimic the behaviour of the rabbitMQ admin console which can receive a message and requeue it.

So my question is how to do this? At first I was trying to make a clone of the message and re-send them, but it seems that the rabbitTemplate is not able to send messages directly to a queue and sending them to an exchange is not an option because it could be possible that multiple queues will receive the message again.

Then I started thinking that I could receive a message and then somehow NACK it so that it ends up back on the queue. The only question there is how should I do that?

回答1:

I think you can achieve that with basicReject:

public class MyListener implements ChannelAwareMessageListener {

   public void onMessage(Message message, Channel channel) throws Exception {
        //Do something with message
        channel.basicReject(message.getMessageProperties().getDeliveryTag(), true);
   }

}