Rabbitmqが提供する公式のNET消費者サンプルコードは以下の通りですが、使用中に接続切断の問題に直面します。切断されるとこのコードがエラーを報告し、消費者や生産者が切断する原因となります。
次の写真はプロデューサーがメッセージを送信している様子で、私は手動でrabbitmqを停止し、その後再起動しました。おそらく起動が成功した後で、サービスが完全に始まらないようにさらに10秒待っていました
サービスが完全に始動した後、以下のエラー付きメッセージを再送信しようとしています:
珍しいテキスト**************
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) rabbitMQ_Publish.Form1.button1_Click(Object sender, EventArgs e)の位置 C:\project\my\RabbitMQ-demo\rabbitMQ-Publish\Form1.cs: 行番号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) System.Windows.Forms.Control.PreProcessMessage(Message& msg) System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg) System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
では、どうして異常に回復できるのでしょうか? それとも切断されてから再接続されるのでしょうか?
RabbitMQ NET クライアントのソースコードは、研究で次のように見つかりましたAutomaticRecoveryEnabled = true使い方はこちらです
具体的な回復メカニズムは以下の通りです
1. AutoRecoveringConnectionが初期化された際、リンククローズイベントのデリゲートに切断処理を追加します
BeginAutomaticRecoveryの呼び出しを観察すると、PerformAutomaticRecoveryメソッドが内部で呼ばれていることがわかります。 このメソッドの内容を直接見てみましょう。最初のものはRecoverConnectionDelegateメソッドです
このメソッドの呼び出しは次の通りです:
ご覧の通り、接続が再開されるまでデッドループを実行し、例外に遭遇するとThread.Sleepを呼び出してしばらく待機させ、その後再度接続復旧を実行します。
|