兔子 - 通道和连接未关闭

发布于 2025-02-12 15:29:32 字数 846 浏览 0 评论 0原文

我正在使用以下Nodejs脚本在我的RabbitMQ队列中发布消息:

const amqp = require("amqplib");
[...]

const publishMsgToQueue = async (job, res) => {
  const queue = job.queueName;
  const msg = { url: job.msg };

  try {
    const connection = await amqp.connect(Rabbitmq.host);
    const channel = await connection.createChannel();
    await channel.assertQueue(queue, { durable: true });
    await channel.sendToQueue(queue, Buffer.from(JSON.stringify(msg)));

    await channel.close();
    await connection.close();

  } catch (error) {
    logger.error(
      "[AMQP] Error publishing to RabbitMQ",
      job.msg,
      "queue name:",
      job.queueName
    );
  }
};

此功能在循环中以为单位来发布消息数组。

当所有消息发布时,我都有许多连接和频道打开...虽然应该通过Channel.Class.Close和Connection.close.Close ...但不是...

我在其他帖子上进行了很多检查,但是我没有成功……

我在哪里错了?

I'm using the following Nodejs script to publish messages in my RabbitMq queue:

const amqp = require("amqplib");
[...]

const publishMsgToQueue = async (job, res) => {
  const queue = job.queueName;
  const msg = { url: job.msg };

  try {
    const connection = await amqp.connect(Rabbitmq.host);
    const channel = await connection.createChannel();
    await channel.assertQueue(queue, { durable: true });
    await channel.sendToQueue(queue, Buffer.from(JSON.stringify(msg)));

    await channel.close();
    await connection.close();

  } catch (error) {
    logger.error(
      "[AMQP] Error publishing to RabbitMQ",
      job.msg,
      "queue name:",
      job.queueName
    );
  }
};

This function is called in a for loop to publish an array of messages.

When all the messages are published, I have many many connections and channels open... while it should be closed thanks to channel.close and connection.close ... but it's not...

I check a lot on other posts, but I didn't succeed so far...

Where am I wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文