RabbitMQ只从队列中获取消息计数

时间:2010-11-18 11:54:36

标签: scala rabbitmq actor

我正在尝试从RabbitMq的队列中获取消息,但只是第一次它给了我消息计数,然后它没有给我任何计数。我正在使用scala语言并使用它的actor.Any的帮助将是赞赏。

代码如下

       def act {
loop {
  FACTORY = new ConnectionFactory()
  //       println("loop")
  FACTORY.setUsername("guest")
  FACTORY.setPassword("guest")
  FACTORY.setVirtualHost("/")
  FACTORY.setPort(5672)
  FACTORY.setHost("localhost")
  **conn = FACTORY.newConnection**
  QUEUE_CHANNEL = conn.createChannel
  QUEUE_CHANNEL.exchangeDeclare(EXCHANGE_NAME, "direct", durable)

  var no = QUEUE_CHANNEL.queueDeclare(QUEUE_NAME, durable,     false,false,null).getMessageCount
  println("calling to main Q" + no)
  QUEUE_CHANNEL.queueBind(QUEUE_NAME, EXCHANGE_NAME, QUEUE_ROUTING_KEY)
  if (no > 0) {
    println("calling to main Q" + no)
    getQ
  }
}
}

     def getQ {
    try {
     println("gettng main q")
  val consumer = new QueueingConsumer(QUEUE_CHANNEL)
  QUEUE_CHANNEL basicConsume (QUEUE_NAME, false, consumer)
  val delivery = consumer.nextDelivery
  val msg = new java.io.ObjectInputStream(
    new java.io.ByteArrayInputStream(delivery.getBody)).readObject
  var obj = msg.asInstanceOf[QueueObject]
  QUEUE_CHANNEL.basicAck(delivery.getEnvelope().getDeliveryTag, false)
  //println(obj.status)
  if (obj != null)
    add(obj) //add to particular queue
} catch {
  case e: InterruptedException => println(e)
}
}

我的代码第二次进入 conn = FACTORY.newConnection 时会进入无限循环。

1 个答案:

答案 0 :(得分:1)

免责声明:我是Akka的PO

我建议你看看Akka的演员,在AMQP上有一个光滑的DSL,我认为你可以从中受益:

http://doc.akkasource.org/amqp 另一种方法是通过Camel消费消息(如果它漂浮在您的船上):

http://doc.akkasource.org/camel