我使用rabbitMq 来管理和使用队列。我有多个队列。它们的数量并不具体。我使用直接交换来发布消息。
我怎样才能仅使用一个队列来消费每个队列的所有消息(基于routing_key)
渠道?
此时我假设我有 5 个队列。我使用了 for 循环并为每个队列创建一个通道。像这样:
stuff=["shoes","pants","hats","jewels","glasses"];
stuff.forEach(cnt =>
{
var ex = 'stuff';
var cq=cnt;
amqp
.connect('amqp://localhost')
.then(conn => conn.createChannel())
.then(ch => {
ch.assertExchange(ex, 'x-delayed-message', { durable: true,
arguments: { 'x-delayed-type': 'direct' } })
return ch
.assertQueue(cq, { durable: true })
.then(() => { ch.bindQueue(cq, ex, cq) /*second cq is routing*/
})
.then(() => {
ch.consume(cq, (msg) =>
{
console.log("['%s'] '%s'",cq, msg.content.toString());
if( msg.content.toString()!=null)
console.log(cq);
reciveMSG=JSON.parse(msg.content.toString());
}, { noAck: true });
});
})
});
但我只想用一个频道来做到这一点。因为它更乐观并且使用更少的内存(我不知道这是真的还是假的!)。有没有办法处理不特定的队列数量?
You can使用一个通道从多个队列中消费,但您将一一接收消息,即使它们来自不同的队列。我非常确定一个队列上的通道异常将停止所有队列的消耗。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)