【发布时间】:2019-11-21 02:07:08
【问题描述】:
我正在使用amqplib 在我的 node.js 服务器中传输消息。我从RabbitMQ official website看到了一个例子:
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!';
ch.assertQueue(q, {durable: false});
// Note: on Node 6 Buffer.from(msg) should be used
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent %s", msg);
});
setTimeout(function() { conn.close(); process.exit(0) }, 500);
});
在这种情况下,连接在超时函数中关闭。我不认为这是一种可持续的方式。但是,ch.sendToQueue 没有允许我在发送消息后关闭连接的回调函数。关闭连接有什么好处?
【问题讨论】:
-
也许,但这是你需要测试的东西,
sendToQueue函数在将消息发送到 RabbitMQ 之前在内部排队,conn.close()将仅在内部队列耗尽时释放连接(即所有消息都发送到服务器并由服务器接收)。 编辑:我可能错了。阅读:squaremobius.net/amqp.node/channel_api.html#overview -
将ConfirmChannel 与 sendToQueue 结合使用会为您提供一个回调,一旦服务器确认发布,该回调可用于关闭连接
标签: javascript node.js rabbitmq