【发布时间】:2015-08-14 18:00:49
【问题描述】:
我在 Node 代码中使用 ampq.node 进行 RabbitMQ 访问。我正在尝试使用publish 或sendToQueue 方法在我发布的消息(即时间戳和内容类型)中包含一些元数据,使用options 参数。
但是我传递给options 的任何内容都被完全忽略了。我想我缺少一些格式或字段名称,但我找不到任何可靠的文档(除了提供的 here 似乎没有做这项工作的文档)。
下面是我的publish功能码:
var publish = function(queueName, message) {
let content;
let options = {
persistent: true,
noAck: false,
timestamp: Date.now(),
contentEncoding: 'utf-8'
};
if(typeof message === 'object') {
content = new Buffer(JSON.stringify(message));
options.contentType = 'application/json';
}
else if(typeof message === 'string') {
content = new Buffer(message);
options.contentType = 'text/plain';
}
else { //message is already a buffer?
content = message;
}
return Channel.sendToQueue(queueName, content, options); //Channel defined and opened elsewhere
};
我错过了什么?
更新:
事实证明,如果您选择使用 ConfirmChannel,您必须提供回调函数作为最后一个参数,否则,选项对象将被忽略。因此,一旦我将代码更改为以下内容,我就开始正确地看到选项:
Channel.sendToQueue(queueName, content, options, (err, result) => {...});
【问题讨论】: