RabbitMQ在nodejs中没有ack

时间:2014-09-08 16:12:32

标签: javascript node.js coffeescript rabbitmq

我试图使用RabbitMQ获得已发送和已处理邮件的确认。不幸的是没有成功。

查看我的简化代码:

服务器

  amqp = require("amqp")

  # Open a connection
  conn = amqp.createConnection( {url: "amqp://localhost"} , {reconnect: false})

  conn.on "ready", ->
    console.log "Conn Ready"

    conn.queue "queueX", {ack:true}, (queue) ->
      console.log "Subscribed #{queue.name}"

      # subscribe to that queue
      queue.subscribe { ack: true }, (message, headers, deliveryInfo, ack) ->
        console.log message
        queue.shift() if queue.shift?

客户端:

  amqp = require("amqp")

  # Open a connection
  conn = amqp.createConnection( url: "amqp://localhost" , reconnect: false)

  conn.on "ready", ->
      console.log "Conn Ready"

      # declare the default exchange
      conn.exchange "exchX", {confirm:true, type:"fanout", autoDelete:true}, (exchange) ->

        # create a queue
        conn.queue "queueX", (queue) ->
          console.log "Subscribed #{queue.name}"

          queue.bind exchange, queue.name, ->

            # publish a message
            console.log "Sending  CloudAMQP 1"
            exchange.publish "", body:"Hello CloudAMQP 1", {}, (out1)->
              console.log "Callback called for msg CloudAMQP 1. Delivered: #{out1}"

            # publish a message
            console.log "Sending  CloudAMQP 2"
            exchange.publish "", body:"Hello CloudAMQP 2", {}, (out1)->
              console.log "Callback called for msg  CloudAMQP 2. Delivered: #{out1}"

服务器输出:

Conn Ready
Subscribed queueX
{ body: 'Hello CloudAMQP 1' }
{ body: 'Hello CloudAMQP 2' }

客户输出:

Conn Ready
Subscribed queueX
Sending  CloudAMQP 1
Sending  CloudAMQP 2
Callback called for msg CloudAMQP 1. Delivered: false
Callback called for msg  CloudAMQP 2. Delivered: false

正如您所看到的, Delivered 是错误的。知道这段代码有什么问题吗?

1 个答案:

答案 0 :(得分:0)

这是您确认(确认)发布

的方式
exchange.publish('', {body: 'your body'}, {}, function(err){
  if (err) return console.error(err);

  console.info('published');
}).addListener('ack', function() {
  console.info('ack received');
});
PS:我不使用coffeescript,请原谅我发布正常的javascript :),我很难转换,如果你遇到麻烦我会试一试