简体   繁体   English

RabbitMQ/Spring AMQP - 将消息留在队列中

[英]RabbitMQ/Spring AMQP - Leave message in a queue

I created a SpringBoot/Spring AMQP project where I configured a listener on a RabbitMQ queue.我创建了一个 SpringBoot/Spring AMQP 项目,在其中我在 RabbitMQ 队列上配置了一个侦听器。 Question: Is there any way to leave the message in the queue?问题:有什么方法可以将消息留在队列中? Let me explain: I consume the message and do some things (eg save on db), if something goes wrong I would like to be able to reconsume the message.让我解释一下:我使用消息并做一些事情(例如保存在数据库上),如果出现问题,我希望能够重新使用消息。

Thanks in advance提前致谢

You need to think about configuring your listener container with transactions, so when DB call fails, the transaction is going to be rolled back and an AMQP message will not be acked on RabbitMQ.您需要考虑使用事务配置您的侦听器容器,因此当 DB 调用失败时,事务将回滚,并且不会在 RabbitMQ 上确认 AMQP 消息。

See docs for more info: https://docs.spring.io/spring-amqp/docs/current/reference/html/#transactions有关更多信息,请参阅文档: https://docs.spring.io/spring-amqp/docs/current/reference/html/#transactions

I don't know about the "Spring" way of accomplishing this, but what you describe is the normal behavior for AMQP consumers that do not automatically acknowledge.我不知道完成此操作的“Spring”方式,但您所描述的是 AMQP 消费者不会自动确认的正常行为。

From the documentation :文档中

In automatic acknowledgement mode, a message is considered to be successfully delivered immediately after it is sent.在自动确认模式下,消息在发送后立即被视为成功传递。

When you turn off automatic acknowledgment, your consumer must explicitly acknowledge the message, otherwise it will not be dequeued (or as you put it, it will be left "in the queue").当您关闭自动确认时,您的消费者必须明确地确认消息,否则它不会出队(或者如您所说,它将被留在“队列中”)。 You will then need to simply ACK the message at the very end of your operation, when you are certain that your operation succeeded (and perhaps coordinated with your database transaction).然后,当您确定您的操作成功(并且可能与您的数据库事务协调)时,您只需在操作结束时简单地确认消息。

There is always the question of what to do first;总是有先做什么的问题; acknowledge first or commit your database transaction first?先确认还是先提交数据库事务? Without adding complexity, you must choose what's best depending on what failure mode is less problematic for you, ie Would you rather tolerate a duplicated message or a missing message?在不增加复杂性的情况下,您必须根据对您来说问题较少的故障模式来选择最好的,即您宁愿容忍重复消息还是丢失消息?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM