3

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.

Pradeep
  • 850
  • 2
  • 14
  • 27

1 Answers1

5

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.

Gary Russell
  • 166,535
  • 14
  • 146
  • 179
  • Thanks for the quick reply. Is there a property or a way to ignore the message or send the message to dead letter queue after "n" number of tries. (assuming n will be specified) – Pradeep Jul 08 '15 at 18:47
  • Add a `retry interceptor` to the listener's `advice-chain`. Configure the interceptor with an `RejectAndDontRequeueRecoverer` which will be invoked when retries are exhausted; the message will be sent to the DLX/DLQ if the original queue is so configured. Alternatively you can republish directly to another queue with additional information (such as the stack trace of the exception) by using a `RepublishMessageRecoverer`. See [the documentation](http://docs.spring.io/spring-amqp/docs/latest-ga/reference/html/amqp.html#async-listeners) for more information. – Gary Russell Jul 08 '15 at 18:55
  • The default recover just logs and exits normally so the message is just logged and dropped. – Gary Russell Jul 08 '15 at 19:01