RABBITMQ------ACK mechanism for message reply

Source: Internet
Author: User
Tags ack manual rabbitmq
message Acknowledgment (messages reply)It may take a few seconds to perform a task, and you may be worried if a consumer hangs up during the execution of a task. Once RABBITMQ has distributed the message to the consumer, it is removed from memory. In this case, if the consumer that is performing the task goes down, it loses the message being processed and the message that is distributed to the consumer but has not yet been processed.

However, we do not want to lose any tasks, and if one consumer hangs up, then we should deliver the tasks distributed to it to another consumer to deal with.

To ensure that messages are not lost, RABBITMQ supports message answering. The consumer sends a message answering, telling RABBITMQ that the message has been received and processed. RABBITMQ can delete it.

If a consumer hangs up without sending an answer, RABBITMQ will understand that the message is not processed completely and then handed over to another consumer to re-process it. This way, you can be sure that even if the consumer hangs up occasionally, it won't lose any news.

There is no message timeout limit, and RABBITMQ is only re-delivered when the consumer is hung up. Even a message can take a long time to process.

Message answering is turned on by default. We turn this mechanism off by displaying the settings autoask=true. It is now automatically answered, and once we complete the task, the consumer will automatically send an answer. Notifies the RABBITMQ that a message has been processed and can be removed from memory. If the consumer does not send an ACK (unlike ACTIVEMQ, in RABBITMQ, where the message does not expire) due to downtime or link failure, RABBITMQ will resend the message to another consumer listening to the queue.

code example:

The producer-side code is the same, and the consumer-side code is used to turn on the manual answer mode.

Listen queue, manually return complete
channel.basicconsume (queue_name, false, consumer);

Note: The second parameter value of FALSE represents the auto-answer mechanism for turning off RABBITMQ, instead of a manual answer.

Returns the reply status when the message is processed.

Returns the confirmation status
Channel.basicack (Delivery.getenvelope (). Getdeliverytag (), false);

Returns the reply status when the message is processed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.