|
|
Posted on 3/29/2018 4:30:04 PM
|
|
|
|

The official NET consumer sample code given by Rabbitmq is as follows, but during the use process, you will encounter the problem of connection disconnection, once disconnected, this code will report an error, which will cause consumers or producers to hang up.
The following picture shows the producer sending a message, I manually stopped rabbitmq, and then restarted rabbitmq, probably after the start was successful, I waited for another 10 seconds to prevent the service from starting completely
After the service is fully started, I try to resend some messages with errors as follows:
Unusual Text **************
RabbitMQ.Client.Exceptions.AlreadyClosedException: Already closed: The AMQP operation was interrupted: AMQP close-reason, initiated by Peer, code=320, text="CONNECTION_FORCED - broker forced connection closure with reason 'shutdown'", classId=0, methodId=0, cause= In RabbitMQ.Client.Impl.SessionBase.Transmit(Command cmd) at RabbitMQ.Client.Impl.ModelBase.ModelSend(MethodBase method, ContentHeaderBase header, Byte[] body) at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, Boolean mandatory, IBasicProperties basicProperties, Byte[] body) at RabbitMQ.Client.Impl.ModelBase.BasicPublish(String exchange, String routingKey, IBasicProperties basicProperties, Byte[] body) At rabbitMQ_Publish.Form1.button1_Click(Object sender, EventArgs e) position C:\project\my\RabbitMQ-demo\rabbitMQ-Publish\Form1.cs: line number 37 In System.Windows.Forms.Control.OnClick(EventArgs e) In System.Windows.Forms.Button.OnClick(EventArgs e) In System.Windows.Forms.Button.PerformClick() at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData) at System.Windows.Forms.TextBoxBase.ProcessDialogKey(Keys keyData) In System.Windows.Forms.Control.PreProcessMessage(Message& msg) In System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) At System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
So how can it be abnormally recovered? Or is it disconnected and reconnected?
The source code of RabbitMQ NET Client, the research found oneAutomaticRecoveryEnabled = trueHere's how to use it
The specific recovery mechanism is as follows
1. When AutoRecoveringConnection is initialized, add disconnection handling to the link close event delegate
Observe the BeginAutomaticRecovery call, and you can see that the PerformAutomaticRecovery method is called internally. Let's look directly at the contents of this method, the first of which is the method RecoverConnectionDelegate
The call in this method is:
As you can see, it executes a dead loop until the connection reopens, and of course, if it encounters an exception, it calls Thread.Sleep to wait a while and then performs connection recovery again.
|
Previous:Introduction to the three exchange modes (fanout, direct, and topic) of RabbitMQNext:Teach you how to set up your own DNS server!
|