【问题标题】:How to confirm that the message has been sent to the RabbitMQ queue?如何确认消息已发送到 RabbitMQ 队列?
【发布时间】:2020-03-14 18:01:37
【问题描述】:

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

channel.assertQueue(config.amqp.queue

表演

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

消费者什么也没显示。管理面板什么都不显示。 如何确认消息已发送到 RabbitMQ 队列? 我在路上 常量 ok = 等待 channel.assertQueue(config.amqp.queue, { 耐用:真实 });

我发送:

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

【问题讨论】:

    标签: node.js rabbitmq


    【解决方案1】:

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

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

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

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

    【讨论】:

    【解决方案2】:

    请

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


    回答

    在amqplib documentation(我猜这是您正在使用的包)中,有一个specific type of channel 使用确认 表示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 的更多详细信息,请查看documentation。

    【讨论】:

      猜你喜欢
      • 2021-07-21
      • 2021-09-26
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 2023-04-10
      • 2016-08-13
      • 1970-01-01
      相关资源
      最近更新 更多