Search code examples
springrabbitmqrabbitmq-exchangespring-rabbit

requeue the message in rabbitmq using Spring ampq


I am new to rabbitmq and am trying the following scenario

--> producer sends message
--> consumer receives the message
-- Execute my own logic

if the logic fails - requeue

--> requeue the message if the consumer fails(machine goes down)

I have implemented the basic sender using Spring rabbitTemplate

rabbitTemplate.convertAndSend(.....);

and for consumer i implemented a message listener

public class CustomMessageListener implements MessageListener {
@Override
    public void onMessage(Message message) {
       //** my own logic**
   }
}

and added it to the container through spring

  <bean id="aListener" class="com.sample.CustomMessageListener" autowire="byName"/>

 <rabbit:listener-container id="myListenerContainer" connection-factory="connectionFactory"  acknowledge="auto" prefetch="750" concurrency="5" >
    <rabbit:listener ref="aListener" queues="reportQueue"/>
</rabbit:listener-container>

Its working fine till this part.

now if ** my own logic** mentioned in the listener fails. i want to requeue the message. how can i implement this. From the blogs that i have gone through it looks like returnedMessage needs to overridden. But am not sure how it can be done through listener.


Solution

  • With acknowledge="auto", the message won't be ack'd until the listener exits normally, so there's nothing extra that you need to do; if your listener throws an exception or the server crashes, the message will remain in the queue.