gpt4 book ai didi

javascript - 在rabbitmq中获取路由/绑定(bind) key

转载 作者:行者123 更新时间:2023-12-03 00:32:42 24 4
gpt4 key购买 nike

我有两个队列,两个绑定(bind)/路由键,我想根据路由/绑定(bind)键在consumer中编写一个条件语句,如何获取它们的值?

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


var args = ['user','nouser'];

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

ch.assertExchange(ex, 'direct', {durable: false});

ch.assertQueue('', {exclusive: true}, function(err, q) {
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C");

args.forEach(function(type) {
ch.bindQueue(q.queue, ex, type);
});

ch.consume(q.queue, function(msg, res) {
if(msg.content) {
console.log(" Received %s", msg.content,q.queue);

}
}, {noAck: true});
});
});
});

最佳答案

channel.consume 处理程序中的 msg 对象包含一个 fields 对象,该对象本身包含一个 routingKey 字段。简而言之:

ch.consume(q.queue, function(msg, res) {
if(msg.content) {
console.log(" Received %s", msg.content,q.queue);
if (msg.fields.routingKey === <your condition>) {
// Handle condition
}
}
}, {noAck: true});

关于javascript - 在rabbitmq中获取路由/绑定(bind) key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53789042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com