title: Play framework - RabbitMQ Caused by: java.util.concurrent.TimeoutException
body: <p>I am using java-rabbitmq-client in an Play Framework scala application.</p>
  
  <pre><code>"com.rabbitmq" % "amqp-client" % "4.0.0"
  </code></pre>
  
  <p>Very often I get this exception and after few hours without any code change or settings change it connects and works properly.</p>
  
  <pre><code>[error] (run-main-0) java.lang.ExceptionInInitializerError
  [warn] c.r.c.i.ForgivingExceptionHandler - An unexpected connection driver error occured (Exception message: Socket closed)
  java.lang.ExceptionInInitializerError
          at utils.mq.email.MQEmailConsumer$.start(MQEmailConsumer.scala:17)
          at utils.App$.main(App.scala:20)
          at utils.App.main(App.scala)
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
          at java.lang.reflect.Method.invoke(Method.java:498)
  Caused by: java.util.concurrent.TimeoutException
          at com.rabbitmq.utility.BlockingCell.get(BlockingCell.java:76)
          at com.rabbitmq.utility.BlockingCell.uninterruptibleGet(BlockingCell.java:110)
          at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
          at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:372)
          at com.rabbitmq.client.impl.AMQConnection.start(AMQConnection.java:297)
          at com.rabbitmq.client.impl.recovery.RecoveryAwareAMQConnectionFactory.newConnection(RecoveryAwareAMQConnectionFactory.java:62)
          at com.rabbitmq.client.impl.recovery.AutorecoveringConnection.init(AutorecoveringConnection.java:99)
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:900)
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:859)
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:817)
          at com.rabbitmq.client.ConnectionFactory.newConnection(ConnectionFactory.java:954)
  </code></pre>
  
  <p>The code to connect to RabbitMQ is as follows:</p>
  
  <pre><code>object MQConnection {
  
    val factory: ConnectionFactory = new ConnectionFactory()
  
    factory.setUri(MQConfig.host)
    factory.setUsername(MQConfig.username)
    factory.setPassword(MQConfig.password)
    factory.setVirtualHost(MQConfig.virtualHost)
  
    factory.setConnectionTimeout(MQConfig.connectionTimeout)
  
    val conn: Connection = factory.newConnection()
  
    def getChannel(queueName: String, exchangeName: String, routingKey: String): Channel = {
      Logger.info(s"Creating channel with queue ${ queueName }, exchangeName - ${exchangeName}, routingKey - ${routingKey}")
      val channel: Channel = conn.createChannel()
      channel.exchangeDeclare(
        exchangeName,
        MQConfig.exchangeType,
        MQConfig.durable
      )
      channel.queueDeclare(queueName, MQConfig.durable, false, false, null)
      channel.queueBind(queueName, exchangeName, routingKey)
  
      channel
    }
  
  }
  </code></pre>
  
  <p>The <code>/usr/local/var/log/rabbitmq/rabbit@localhost.log</code> has the following details logged when trying to start a consumer:</p>
  
  <pre><code>=INFO REPORT==== 11-Feb-2017::18:54:43 ===
  accepting AMQP connection &lt;0.22922.0&gt; (127.0.0.1:63334 -&gt; 127.0.0.1:5672)
  
  =WARNING REPORT==== 11-Feb-2017::18:54:51 ===
  closing AMQP connection &lt;0.22922.0&gt; (127.0.0.1:63334 -&gt; 127.0.0.1:5672):
  client unexpectedly closed TCP connection
  </code></pre>
  
  <p>The logs in <code>/usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log</code>:</p>
  
  <pre><code>=PROGRESS REPORT==== 11-Feb-2017::18:54:43 ===
            supervisor: {&lt;0.22920.0&gt;,rabbit_connection_sup}
               started: [{pid,&lt;0.22921.0&gt;},
                         {name,helper_sup},
                         {mfargs,{rabbit_connection_helper_sup,start_link,[]}},
                         {restart_type,intrinsic},
                         {shutdown,infinity},
                         {child_type,supervisor}]
  
  =PROGRESS REPORT==== 11-Feb-2017::18:54:43 ===
            supervisor: {&lt;0.22920.0&gt;,rabbit_connection_sup}
               started: [{pid,&lt;0.22922.0&gt;},
                         {name,reader},
                         {mfargs,
                             {rabbit_reader,start_link,
                                 [&lt;0.22921.0&gt;,
                                  {acceptor,{127,0,0,1},5672},
                                  #Port&lt;0.30086&gt;]}},
                         {restart_type,intrinsic},
                         {shutdown,30000},
                         {child_type,worker}]
  </code></pre>
  
  <p>Can you please help. This has been very annoying and sometimes it takes a day or two before it makes a successful connection.</p>