RabbitMQ 监听器在 MessageListener 抛出异常时停止监听消息

编程入门 行业动态 更新时间:2024-10-25 18:22:46
本文介绍了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 抛出异常时停止监听消息

本文发布于:2023-11-12 18:48:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1582251.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:监听器   抛出   异常   消息   RabbitMQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!