RabbitMQ listener stops listening messages when MessageListener throws exception

微笑、不失礼 提交于 2020-01-07 05:20:27

问题


I am using Spring AMQP for processing the messages in RabbitMQ.

Below is the issue:
1. (say) there are 3 messages in ready state inside RabbitMQ
2. First one is picked up by MessageListener and starts processing. (say) It ends up throwing an exception
3. In this case, the container remains up but the remaining 2 messages are not processed until i restart the container. Also the first messages stays in unacknowledged state.

Is it the expected behavior? If not, how to make sure that other 2 messages will be processed irrespective first one failed processing?

MQ configuraion:

<rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" /> 

<rabbit:admin connection-factory="connectionFactory" />

<rabbit:listener-container
    connection-factory="connectionFactory" 
    concurrency="1"
    acknowledge="auto">
    <rabbit:listener queue-names="testQueue" ref="myProcessorListener " />
</rabbit:listener-container>

MessageListener class:

public class MyProcessorListener implements MessageListener{
....
    @Override
public void onMessage(Message message) {
try{
...Some logic...

} catch (Exception e) {
  throw new RuntimeException(e.getMessage(), e);
}

回答1:


The message is redelivered over and over again; in order to reject it (and discard or route to a dead letter queue), you need to throw AmqpRejectAndDontRequeueException or set the container's requeue-rejected property to false. When configuring with Java it's defaultRequeueRejected.

You can also use a custom error handler.

This is all explained in the reference manual.



来源:https://stackoverflow.com/questions/43466571/rabbitmq-listener-stops-listening-messages-when-messagelistener-throws-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!