Play框架 - RabbitMQ引起:java.util.concurrent.TimeoutException

时间:2017-02-11 13:30:59

标签: scala playframework rabbitmq

我在Play Framework scala应用程序中使用java-rabbitmq-client。

"com.rabbitmq" % "amqp-client" % "4.0.0"

我经常遇到此异常,几小时后没有任何代码更改或设置更改,它连接并正常工作。

[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)

连接RabbitMQ的代码如下:

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
  }

}

尝试启动消费者时,/usr/local/var/log/rabbitmq/rabbit@localhost.log会记录以下详细信息:

=INFO REPORT==== 11-Feb-2017::18:54:43 ===
accepting AMQP connection <0.22922.0> (127.0.0.1:63334 -> 127.0.0.1:5672)

=WARNING REPORT==== 11-Feb-2017::18:54:51 ===
closing AMQP connection <0.22922.0> (127.0.0.1:63334 -> 127.0.0.1:5672):
client unexpectedly closed TCP connection

/usr/local/var/log/rabbitmq/rabbit@localhost-sasl.log中的日志:

=PROGRESS REPORT==== 11-Feb-2017::18:54:43 ===
          supervisor: {<0.22920.0>,rabbit_connection_sup}
             started: [{pid,<0.22921.0>},
                       {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: {<0.22920.0>,rabbit_connection_sup}
             started: [{pid,<0.22922.0>},
                       {name,reader},
                       {mfargs,
                           {rabbit_reader,start_link,
                               [<0.22921.0>,
                                {acceptor,{127,0,0,1},5672},
                                #Port<0.30086>]}},
                       {restart_type,intrinsic},
                       {shutdown,30000},
                       {child_type,worker}]
你能帮忙吗?这非常令人讨厌,有时需要一两天才能成功连接。

0 个答案:

没有答案