简体   繁体   中英

RabbitMQ no ack in nodejs

I'm trying to get acknowledges of delivered and processed messages using RabbitMQ. Unfortunately no success.

Look at my simplified code:

Server:

  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?

Client:

  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}"

Server Output:

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

Client Output:

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

As you can see Delivered is false. Any idea what's wrong with this code?

This is the way you confirm (acknowledge) the publish

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

  console.info('published');
}).addListener('ack', function() {
  console.info('ack received');
});

PS: I do not use coffeescript so, excuse me for posting normal javascript :), it's hard for me to convert, if you are having trouble with this I will give it a shot

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM