[英]Rabbit MQ amqplib error “No channels left to allocate”
在 pub sub 模式中使用 rabbit mq 工人一段时间后,我在创建频道时遇到错误。
Error: No channels left to allocate
The maximum number of channels you can allocate is negotiated with rabbitmp server.您可以分配的最大通道数与rabbitmp 服务器协商。 In my server, this value is 2047. You can debug you amqplib/lib/connection.js to get this.
在我的服务器中,这个值是 2047。你可以调试你 amqplib/lib/connection.js 来得到这个。
If you are using https://www.npmjs.com/package/amqplib , you can use Promise to share a channel while publishing multiple messages如果您使用的是https://www.npmjs.com/package/amqplib ,您可以使用 Promise 在发布多条消息的同时共享一个频道
in message-queue.js
在
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.