title: How do you handle recovering from a faulty connection using RabbitMQ java client library?
body: <p>I'm interested in knowing how other people handle recovering from a faulty connection using the official RabbitMQ java client library. We are using it to connect our application servers to our RabbitMQ cluster and we have implemented a few different ways to recover from a connection failure, but non of them feel quite right. </p>
  
  <p>Imagine this pseudo application:</p>
  
  <pre><code>public class OurClassThatStartsConsumers {
      Connection conn;
  
      public void start() {
          ConnectionFactory factory = new ConnectionFactory();
          factory.setUsername("someusername");
          factory.setPassword("somepassword");
          factory.setHost("somehost");
          conn = factory.newConnection();
  
          new Thread(new Consumer(conn.createChannel())).start();
      }
    }
  
  class Consumer1 implements Runnable {
      public Consumer1(Channel channel) {
           this.channel = channel;
      }
  
      @Override
      public void run() {
          while (true) {
               ... consume incoming messages on the channel...
              // How do we handle that the connection dies?
          }
      }
  }
  </code></pre>
  
  <p>In the real world we have several hundreds of consumers. So what happens if the connection dies? In the above example Consumer1 can not recover, when the connection closes, the Channel also closes, a state from which we can not recover. So lets look at some ways to solve this:</p>
  
  <p>Solution A) </p>
  
  <p>Let every consumer have their own connection and register the events that trigger when the connection dies and then handle reconnecting. </p>
  
  <p>Pros: It works</p>
  
  <p>Cons: </p>
  
  <ul>
  <li>Since we have a lot of consumers, we probably do not want that many
  connections.  </li>
  <li>We might possibly have a lot of duplicated code for
  reconnecting to rabbit and handle reconnecting</li>
  </ul>
  
  <p>Solution B)</p>
  
  <p>Have each consumer use the same connection and subscribe to it's connection failure events. </p>
  
  <p>Pros: Less connections than in Solution A</p>
  
  <p>Cons: Since the connection is closed we need to reopen/replace it. The java client library doesn't seem to provide a way to reopen the connection, so we would have to replace it with a new connection and then somehow notify all the consumers about this new connection and they would have to recreate the channels and the consumers. Once again, a lot of logic that I don't want to see in the consumer ends up there. </p>
  
  <p>Solution C)</p>
  
  <p>Wrap <code>Connection</code> and <code>Channel</code> classes is classes that handle the re-connection logic, the consumer only needs to know about the <code>WrappedChannel</code> class. On a connection failure the <code>WrappedConnection</code> will deal with re-establishing the connection and once connected the <code>WrappedConnection</code> will automatically create new Channels and register consumers.</p>
  
  <p>Pros: It works - this is actually the solution we are using today.</p>
  
  <p>Cons: It feels like a hack, I think this is something that should be handled more elegantly by the underlying library. </p>
  
  <p>Maybe there is a much better way? The API documentation does not talk that much about recovering from a faulty connection. Any input is appreciated :)</p>