This article is a mirror article of machine translation, please click here to jump to the original article.

View: 24043|Reply: 0

[WinForm] .net/c# RabbitMQ connection disconnection - disconnection and reconnection

[Copy link]
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 RabbitMQ
Next:Teach you how to set up your own DNS server!
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com