Use of rabbitmq in. Net (7)-message transmission control

Source: Internet
Author: User

 

The previous article also mentioned several concepts of message transmission. This article provides a comprehensive introduction and some additional content.

 

Message response

Rabbitmq has two response modes: automatic and manual. This is also recommended by amqp protocol. This is the same in point-to-point and broadcast.

Automatic response-When rabbitmq sends a message to the receiving end and the receiving end sends the message out of the queue, it will automatically send you a Response Message to the service.

Manual response-we need our developers to manually call the ACK method to inform the service that the Service has received the response.

In big data transmission, we recommend that you use automatic response if you are not very sensitive to the loss of individual messages. For scenarios where one message cannot be lost, you need to use manual response, that is to say, the response is not answered until the correct processing is complete. If automatic response is selected, the message resend function is unavailable.

 

Message rejection

Rejection is a command that the receiving end responds to the rabbitmq service when receiving a message, telling the server that it should not be handled by me, or rejecting it and throwing it away. When sending the reject command, the acceptor can choose whether to put it back in the queue. If no other receiver monitors this queue, pay attention to the danger of infinite loop transmission.

 
Basicdelivereventargs Ea = (basicdelivereventargs) Consumer. queue. dequeue ();
 
Channel. basicreject (EA. deliverytag,False);

The first parameter of the basicreject method is the message's deliverytag. For each channel, each message has a deliverytag, which is generally expressed in the order of receiving messages: 1, 2, 3, 4, and so on. The second parameter is whether to put it back in the queue, requeue.

The basicreject method can only reject one message at a time, while the basicnack method can reject zero or multiple messages at a time, and you can also set whether to requeue.

 
Channel. basicnack (3,True,False);

If 3 is input in the first deliverytag parameter, the message deliverytag is smaller than or equal to 3, and the channel is rejected.

 

Message QoS

QoS = Quality-of-Service, as the name implies, the quality of the service. Generally, when designing a system, we should design a complete Exception Handling Mechanism instead of completely eliminating the fault or ensuring that there is no fault. When an error occurs, you must know where the error looks like, why, and how to recover or handle it. When a message receiving failure occurs, we can use the rabbitmq resend mechanism to handle the problem. Resend has a limit on the number of resends. In some cases, you cannot resend the message for an unlimited number of times, depending on the Message Size, importance, and processing method.

QoS is even set at the receiver. The sender does not change. The receiver'sCodeIt is also relatively simple, just add the following code:

 
Channel. basicqos (0, 1,False);

The first parameter of the Code is the size of the message that can be received, but it seems that it must be 0 in client 2.8.6, even if: unlimited. If you do not enter 0,ProgramWhen this line is run, an error is reported, indicating that the implementation is not 0. The second parameter is the maximum number of messages processed. For example, if input 1 is used, if a message is received but no response is received, the client will not receive the next message and the message will only be blocked in the queue. If 3 is input, a maximum of three messages are not answered. If 3 messages are received, the sender sends the message to the recipient only in the queue, however, the receiver does not receive messages. In conclusion, the maximum number of messages that the client can receive before the next response message is sent. The third parameter sets whether it is for the entire connection, because a connection can have multiple channels. If it is false, it indicates that it is only for this channel.

This quantity setting also provides more options for multiple clients to monitor the same queue in this load balancing environment.

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.