在nodejs中使用RabbitMQ(七)实现生产者确认

  • 生产者 :批量发送消息(每批10条),每条消息附带唯一 correlationId,并监听确认队列(ackQueue)。

  • 消费者 :处理消息后,通过 ackQueue 返回确认消息(携带原 correlationId)。

  • 超时重试:若某批消息在指定时间内未全部确认,未确认的消息会重新加入待发送队列。

producer.ts

javascript 复制代码
import amqp from 'amqplib';


async function start() {
  const connection = await amqp.connect('amqp://admin:admin1234@localhost:5672//mirror?heartbeat=60');
  const channel = await connection.createChannel();
  const queue = 'queue11';
  const ackQueue = 'queue11_ack';

  await channel.assertQueue(queue, { durable: true });
  await channel.assertQueue(ackQueue, { durable: true });

  async function produce(limit: number, data: string[], timeout: number = 10000) {
    let message = [...data];
    if (message.length > limit) {
      message = message.slice(0, limit);
    } else if (message.length < limit) {
      limit = message.length;
    }

    // 消息确认
    let cache: Array<{
      correlationId: string,
      message: string,
      isDelete: boolean,
    }> = new Array(limit)
      .fill(null)
      .map((_, index) => {
        return {
          correlationId: Math.random().toString().slice(2, -1),
          message: message[index],
          isDelete: false,
        };
      });

    for (let i = 0; i < limit; ++i) {
      channel.sendToQueue(queue, Buffer.from(cache[i].message), {
        correlationId: cache[i].correlationId,
        replyTo: ackQueue
      });
    }

    const consume = await channel.consume(ackQueue, (message) => {
      if (!message) {
        console.error('message is null', message);
        return;
      }

      let index = cache.findIndex((item) => item.correlationId === message.properties.correlationId);

      if (index !== -1) {
        cache[index].isDelete = true;
        console.log('confirmed success:', `"${message.content.toString()}"`, cache.every(item => item.isDelete));
      } else {
        console.log('confirmed fail:', `"${message.content.toString()}"`, cache, cache.every(item => item.isDelete), message.properties.correlationId);
      }

      channel.ack(message);
    });

    const sleep = (time: number) => {
      return new Promise<void>(resolve => setTimeout(() => resolve(), time));
    }

    let stop = false;
    const interval = async () => {
      await sleep(0);
      if (cache.every(item => item.isDelete) || stop) {
        return;
      } else {
        await interval();
      }
    }

    await Promise.race([
      interval(), // 监听本批次消息是否已经处理完成
      sleep(timeout), // 本批次消息最长处理时间
    ]);

    stop = true;

    await channel.cancel(consume.consumerTag);

    // 没有收到确认的消息返回下一批处理继续处理
    return cache.filter(item => !item.isDelete).map(item => item.message);
  }

  // 发送1000条数据,分100批,每批10个
  let msg = new Array(100).fill(null).map((_, index) => `${index} message ${Math.random().toString().slice(2, -1)}`);

  while (msg.length) {
    let res = await produce(10, msg.slice(0, 10), 6000);
    msg = [...res, ...msg.slice(10, msg.length)];
    console.log('完成一批:', msg.length, '发送结果:', res.length, res);
  }
}

start();

consumer.ts

javascript 复制代码
import amqp from 'amqplib';


async function produce() {
  const connection = await amqp.connect('amqp://admin:admin1234@localhost:5672//mirror?heartbeat=60');
  const channel = await connection.createChannel();
  const queue = 'queue11';
  const ackQueue = 'queue11_ack';

  await channel.assertQueue(queue, { durable: true });
  await channel.assertQueue(ackQueue, { durable: true });

  
  channel.consume(queue, (message) => {
    if (message) {
      console.log(message?.content.toString(), message?.properties?.replyTo, message?.properties?.correlationId);

      // 消息处理完后,向 ackQueue 发送确认消息
      channel.sendToQueue(ackQueue, message?.content, {
        // 使用相同的 correlationId 来标识确认消息
        correlationId: message?.properties?.correlationId,
        // 将原 replyTo 信息传递回来
        // replyTo: queue,
      });

      // 确认 queue11 中的消息
      channel.ack(message);
    } else {
      console.error('message is null', message);
    }
  }, { noAck: false });
}

produce();
相关推荐
zandy101141 分钟前
高并发场景下的BI架构设计:衡石分布式查询引擎与缓存分级策略
分布式·缓存·高并发架构·弹性扩展·分布式查询·缓存分级·mpp引擎
猪猪果泡酒1 小时前
Spark,RDD中的转换算子
大数据·分布式·spark
山猪打不过家猪5 小时前
(五)毛子整洁架构(分布式日志/Redis缓存/OutBox Pattern)
分布式·缓存
jstart千语10 小时前
【Redis】分布式锁的实现
数据库·redis·分布式
小马爱打代码12 小时前
面试题 - Kafka、RabbitMQ、RocketMQ如何选型?
kafka·rabbitmq·rocketmq
Bruk.Liu13 小时前
Kafka、RabbitMQ 和 RocketMQ区别及上手难度
kafka·rabbitmq·rocketmq
martian66513 小时前
信创生态核心技术栈:数据库与中间件
开发语言·中间件·系统架构·系统安全·创业创新
Bruk.Liu13 小时前
Linux 上安装RabbitMQ
linux·服务器·rabbitmq
掘金-我是哪吒14 小时前
分布式微服务系统架构第125集:AI大模型
分布式
言小乔.15 小时前
202534 | KafKa简介+应用场景+集群搭建+快速入门
分布式·kafka