Detect Lost Connection In Publish And Change Host RabbitMQ












0















I develop a very simple app using RabbitMQ. One machine, multiple queues and exchanges, one publisher and one consumer. After reading further about Clustering and HA I connect a second machine to create a cluster, besides I mirrored queues to have at least one replica. Now when I want to publish some data into a queue, I use the first machine as my host and it works fine, but if RabbitMQ service of the first machine not running my app crashed.
My question is how to know which machine is up for creating connection and how to change the host while publishing messages?

UPDATE
I use one of CreateConnection overloads to pass all my hosts for creating a connection. OK, this will solve the problem of finding an available machine to create a connection. But the second question is still there, look at the code below:



for(int i = 0, i < 300, i++){
var message = string.Format("Message #{0}: {1}", i, Guid.NewGuid());
var messageBodyTypes = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(ExchangeName, "123456", null, messageBodyBytes);
}


These lines of code is work perfect when the connection is OK, but assume that in the middle of publishing messages to an exchange, the service stopped unexpectedly, then in this case first System.IO.FileLoadException raised and if I continue the executation RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is saying:




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, methidId=0, cause=




I think there must be a way to change the host when the connection closed during publishing messages, but how, no IDEA!










share|improve this question





























    0















    I develop a very simple app using RabbitMQ. One machine, multiple queues and exchanges, one publisher and one consumer. After reading further about Clustering and HA I connect a second machine to create a cluster, besides I mirrored queues to have at least one replica. Now when I want to publish some data into a queue, I use the first machine as my host and it works fine, but if RabbitMQ service of the first machine not running my app crashed.
    My question is how to know which machine is up for creating connection and how to change the host while publishing messages?

    UPDATE
    I use one of CreateConnection overloads to pass all my hosts for creating a connection. OK, this will solve the problem of finding an available machine to create a connection. But the second question is still there, look at the code below:



    for(int i = 0, i < 300, i++){
    var message = string.Format("Message #{0}: {1}", i, Guid.NewGuid());
    var messageBodyTypes = Encoding.UTF8.GetBytes(message);
    channel.BasicPublish(ExchangeName, "123456", null, messageBodyBytes);
    }


    These lines of code is work perfect when the connection is OK, but assume that in the middle of publishing messages to an exchange, the service stopped unexpectedly, then in this case first System.IO.FileLoadException raised and if I continue the executation RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is saying:




    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, methidId=0, cause=




    I think there must be a way to change the host when the connection closed during publishing messages, but how, no IDEA!










    share|improve this question



























      0












      0








      0








      I develop a very simple app using RabbitMQ. One machine, multiple queues and exchanges, one publisher and one consumer. After reading further about Clustering and HA I connect a second machine to create a cluster, besides I mirrored queues to have at least one replica. Now when I want to publish some data into a queue, I use the first machine as my host and it works fine, but if RabbitMQ service of the first machine not running my app crashed.
      My question is how to know which machine is up for creating connection and how to change the host while publishing messages?

      UPDATE
      I use one of CreateConnection overloads to pass all my hosts for creating a connection. OK, this will solve the problem of finding an available machine to create a connection. But the second question is still there, look at the code below:



      for(int i = 0, i < 300, i++){
      var message = string.Format("Message #{0}: {1}", i, Guid.NewGuid());
      var messageBodyTypes = Encoding.UTF8.GetBytes(message);
      channel.BasicPublish(ExchangeName, "123456", null, messageBodyBytes);
      }


      These lines of code is work perfect when the connection is OK, but assume that in the middle of publishing messages to an exchange, the service stopped unexpectedly, then in this case first System.IO.FileLoadException raised and if I continue the executation RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is saying:




      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, methidId=0, cause=




      I think there must be a way to change the host when the connection closed during publishing messages, but how, no IDEA!










      share|improve this question
















      I develop a very simple app using RabbitMQ. One machine, multiple queues and exchanges, one publisher and one consumer. After reading further about Clustering and HA I connect a second machine to create a cluster, besides I mirrored queues to have at least one replica. Now when I want to publish some data into a queue, I use the first machine as my host and it works fine, but if RabbitMQ service of the first machine not running my app crashed.
      My question is how to know which machine is up for creating connection and how to change the host while publishing messages?

      UPDATE
      I use one of CreateConnection overloads to pass all my hosts for creating a connection. OK, this will solve the problem of finding an available machine to create a connection. But the second question is still there, look at the code below:



      for(int i = 0, i < 300, i++){
      var message = string.Format("Message #{0}: {1}", i, Guid.NewGuid());
      var messageBodyTypes = Encoding.UTF8.GetBytes(message);
      channel.BasicPublish(ExchangeName, "123456", null, messageBodyBytes);
      }


      These lines of code is work perfect when the connection is OK, but assume that in the middle of publishing messages to an exchange, the service stopped unexpectedly, then in this case first System.IO.FileLoadException raised and if I continue the executation RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is saying:




      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, methidId=0, cause=




      I think there must be a way to change the host when the connection closed during publishing messages, but how, no IDEA!







      c# rabbitmq






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 2 at 7:29







      Shahroozevsky

















      asked Jan 1 at 14:48









      ShahroozevskyShahroozevsky

      16412




      16412
























          1 Answer
          1






          active

          oldest

          votes


















          0















          These lines of code is work perfect when the connection is OK, but
          assume that in the middle of publishing messages to an exchange, the
          service stopped unexpectedly, then in this case first
          System.IO.FileLoadException raised and if I continue the executation
          RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is
          saying:




          You must close the channel and current connection and open a new one of each mid-loop. That should use a different connection. You only have to do this when the exception is caught, not on every iteration of the loop.





          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






          share|improve this answer


























          • You mean for every basicPublish I have to open a new connection???

            – Shahroozevsky
            Jan 2 at 15:02













          • No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

            – Luke Bakken
            Jan 2 at 15:16











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996405%2fdetect-lost-connection-in-publish-and-change-host-rabbitmq%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0















          These lines of code is work perfect when the connection is OK, but
          assume that in the middle of publishing messages to an exchange, the
          service stopped unexpectedly, then in this case first
          System.IO.FileLoadException raised and if I continue the executation
          RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is
          saying:




          You must close the channel and current connection and open a new one of each mid-loop. That should use a different connection. You only have to do this when the exception is caught, not on every iteration of the loop.





          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






          share|improve this answer


























          • You mean for every basicPublish I have to open a new connection???

            – Shahroozevsky
            Jan 2 at 15:02













          • No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

            – Luke Bakken
            Jan 2 at 15:16
















          0















          These lines of code is work perfect when the connection is OK, but
          assume that in the middle of publishing messages to an exchange, the
          service stopped unexpectedly, then in this case first
          System.IO.FileLoadException raised and if I continue the executation
          RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is
          saying:




          You must close the channel and current connection and open a new one of each mid-loop. That should use a different connection. You only have to do this when the exception is caught, not on every iteration of the loop.





          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






          share|improve this answer


























          • You mean for every basicPublish I have to open a new connection???

            – Shahroozevsky
            Jan 2 at 15:02













          • No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

            – Luke Bakken
            Jan 2 at 15:16














          0












          0








          0








          These lines of code is work perfect when the connection is OK, but
          assume that in the middle of publishing messages to an exchange, the
          service stopped unexpectedly, then in this case first
          System.IO.FileLoadException raised and if I continue the executation
          RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is
          saying:




          You must close the channel and current connection and open a new one of each mid-loop. That should use a different connection. You only have to do this when the exception is caught, not on every iteration of the loop.





          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.






          share|improve this answer
















          These lines of code is work perfect when the connection is OK, but
          assume that in the middle of publishing messages to an exchange, the
          service stopped unexpectedly, then in this case first
          System.IO.FileLoadException raised and if I continue the executation
          RabbitMQ.Client.Exceptions.AlreadyClosedException raised which is
          saying:




          You must close the channel and current connection and open a new one of each mid-loop. That should use a different connection. You only have to do this when the exception is caught, not on every iteration of the loop.





          NOTE: the RabbitMQ team monitors the rabbitmq-users mailing list and only sometimes answers questions on StackOverflow.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jan 2 at 15:16

























          answered Jan 2 at 14:43









          Luke BakkenLuke Bakken

          3,30221016




          3,30221016













          • You mean for every basicPublish I have to open a new connection???

            – Shahroozevsky
            Jan 2 at 15:02













          • No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

            – Luke Bakken
            Jan 2 at 15:16



















          • You mean for every basicPublish I have to open a new connection???

            – Shahroozevsky
            Jan 2 at 15:02













          • No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

            – Luke Bakken
            Jan 2 at 15:16

















          You mean for every basicPublish I have to open a new connection???

          – Shahroozevsky
          Jan 2 at 15:02







          You mean for every basicPublish I have to open a new connection???

          – Shahroozevsky
          Jan 2 at 15:02















          No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

          – Luke Bakken
          Jan 2 at 15:16





          No, I mean that when you catch the AlreadyClosedException you should close the current channel and connection and re-open a new connection and channel from the connection factory. You only have to do this when an exception is thrown.

          – Luke Bakken
          Jan 2 at 15:16




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53996405%2fdetect-lost-connection-in-publish-and-change-host-rabbitmq%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          MongoDB - Not Authorized To Execute Command

          How to fix TextFormField cause rebuild widget in Flutter

          Npm cannot find a required file even through it is in the searched directory