RabbitMQ 监听器在 MessageListener 抛出异常时停止监听消息 [英] RabbitMQ listener stops listening messages when MessageListener throws exception

查看:492
本文介绍了RabbitMQ 监听器在 MessageListener 抛出异常时停止监听消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 Spring AMQP 来处理 RabbitMQ 中的消息.

以下是问题:
1.(比如说)RabbitMQ
中有3条处于就绪状态的消息2. 第一个被 MessageListener 拾取并开始处理.(说)它最终抛出一个异常
3. 在这种情况下,容器保持运行状态,但在我重新启动容器之前不会处理剩余的 2 条消息.此外,第一条消息保持未确认状态.

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.

这是预期的行为吗?如果没有,如何确保无论第一个处理失败,其他 2 个消息都会被处理?

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

MQ 配置:

<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 类:

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

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

推荐答案

消息一遍遍地重新传递;为了拒绝它(并丢弃或路由到死信队列),您需要抛出 AmqpRejectAndDontRequeueException 或将容器的 requeue-rejected 属性设置为 false.使用 Java 进行配置时,它是 defaultRequeueRejected.

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.

这都是参考手册中的解释.

这篇关于RabbitMQ 监听器在 MessageListener 抛出异常时停止监听消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆