繁体   English   中英

如何确认消息已发送到 RabbitMQ 队列?

[英]How to confirm that the message has been sent to the RabbitMQ queue?

我向 RabbitMQ 队列发送消息,但消费者什么也没收到。

channel.assertQueue(config.amqp.queue

节目

{ queue: 'tasks', messageCount: 0, consumerCount: 1 }

消费者什么也没显示。 管理面板什么都不显示。 如何确认消息已发送到 RabbitMQ 队列? 我发送的方式是 const ok = await channel.assertQueue(config.amqp.queue, {durable: true });

我通过以下方式发送:

  await channel.sendToQueue(config.asteriskAmi.queue, Buffer.from('string'));

不要犹豫,提供任何可能有助于回答您关于 SO 的问题的环境细节:例如,您正在使用的包、您如何初始化您正在使用的变量等。


回答

amqplib 文档中(我猜这是您正在使用的 package),有一种特定类型的通道使用确认来指示 RabbitMQ 服务器已成功接收到消息:

import * as amqp from "amqplib";

// Initialise `AMQP_OPTIONS` and `message` here, e.g.:
/* const AMQP_OPTIONS = {
    frameMax: 0,
    heartbeat: 0,
    hostname: "localhost",
    locale: "en_GB",
    password: "guest",
    port: 5672,
    protocol: "amqp",
    username: "guest",
    vhost: "/",
};

const message = "Hello, world!"; */

const connection = await amqp.connect(AMQP_OPTIONS);
const confirmChannel = await connection.createConfirmChannel();
await confirmChannel.assertQueue(config.amqp.queue);
confirmChannel.sendToQueue(
  config.amqp.queue, // Perhaps you wanted 'config.asteriskAmi.queue' here ?
  Buffer.from(message),
  {},
  (err, ok) => {
    if (err !== null) {
      console.error(err);
      // Error handling
    }
    else {
      console.info("Message successfully acked!");
      // Success handling
    }
  }
);

注意:您的问题也可能是您的消费者在发布到config.amqp.queue时订阅了config.asteriskAmi.queue

希望能帮助到你。 如果您需要有关confirmChannel.sendToQueue的更多详细信息,请参阅文档

使用标准 AMQP 0-9-1,保证消息不丢失的唯一方法是使用事务——使通道具有事务性,然后为每条消息或一组消息发布、提交。 在这种情况下,交易是不必要的重量级,并将吞吐量降低了 250 倍。为了解决这个问题,引入了确认机制。 它模仿了协议中已经存在的消费者确认机制。

为了启用确认,客户端发送 confirm.select 方法。 根据是否设置了 no-wait,代理可能会使用confirm.select-ok进行响应。 一旦在通道上使用了 confirm.select 方法,就说它处于确认模式。 交易通道不能进入确认模式,一旦通道处于确认模式,就不能进行交易。

一旦通道处于确认模式,代理和客户端都会对消息进行计数(在第一个 confirm.select 上计数从 1 开始)。 然后,代理在处理消息时通过在同一通道上发送basic.ack来确认消息。 delivery-tag字段包含已确认消息的序列号。 代理还可以在basic.ack中设置 multiple 字段,以指示所有消息(包括具有序列号的消息)都已被处理。

进一步阅读可以在这里完成https://www.rabbitmq.com/confirms.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM