在 pub sub 模式中使用rabbit mq 工作人员工作一段时间后,我在创建 channel 时遇到错误。
错误:没有剩余 channel 可供分配
最佳答案
如果您使用https://www.npmjs.com/package/amqplib , 您可以使用 Promise 在发布多条消息的同时共享一个 channel
在message-queue.js
const q = 'tasks';
const open = require('amqplib').connect('amqp://localhost');
const channelPromise = open.then((conn) => conn.createChannel());
// Publisher
function publishMessage(message) {
channelPromise.then((ch) => ch.assertQueue(q)
.then((ok) => ch.sendToQueue(q, Buffer.from(message))))
.catch(console.warn);
}
// Consumer
open.then((conn) => conn.createChannel())
.then((ch) => ch.assertQueue(q).then((ok) => ch.consume(q, (msg) => {
if (msg !== null) {
console.log(msg.content.toString());
ch.ack(msg);
}
}))).catch(console.warn);
module.exports = {
publishMessage,
};
some-where.js
messageQueue.publishMessage('hello world')
关于node.js - Rabbit MQ amqplib错误 "No channels left to allocate",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51336128/