在RabbitMq中assertQueue和send To Queue之间有什么区别

时间:2018-05-14 05:58:35

标签: node.js rabbitmq amqp

我找到了rabittMq中发送字符串和从队列接收的示例但我不清楚这些方法 - assertQueu sendToQueue

send.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {
  conn.createChannel(function(err, ch) {

              var q = 'hello';
                var msg = 'Hello World! - '+i;

                ch.assertQueue(q, {durable: false});                    
                ch.sendToQueue(q, new Buffer(msg));
                console.log(" [x] Sent %s", msg);
  });
  setTimeout(function() { conn.close(); process.exit(0) }, 1000);
});

receive.js

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(err, conn) {          //amqp://localhost
  conn.createChannel(function(err, ch) {
    var q = 'hello';

    ch.assertQueue(q, {durable: false});
    console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q);
    ch.consume(q, function(msg) {
      console.log(" [x] Received %s", msg.content.toString());
    }, {noAck: true});
  });
});

1 个答案:

答案 0 :(得分:6)

从您的示例中

assertQueu检查"你好"队列,如果它不存在,那么它将创建一个。

sendToQueue将消息发送到"你好"队列中。