首页 > 解决方案 > AMQP RabbitMQ 获取错过的已发布消息

问题描述

当在 rabbitmq 上使用 1:1 的 send/listen 时,服务会保留尚未被侦听器侦听或确认的消息,以便当侦听器启动时,它会收到积压并被清除。如何或需要什么配置才能使其也适用于 1:many(类似扇出)的发布/订阅?

我正在为 nodejs 使用 amqplib

标签: node.jsrabbitmqamqp

解决方案


  1. Rabbitmq 队列应该是持久的

  2. 这确保在尝试使用队列之前声明队列

durable: true
});

**sample code**

var amqp = require('amqplib/callback_api');

amqp.connect('amqp://localhost', function(error0, connection) {
  if (error0) {
    throw error0;
  }
 connection.createChannel(function(error1, channel) {
    if (error1) {
      throw error1;
    }
   var queue = 'task_queue';

   var msg = process.argv.slice(2).join(' ') || "Hello World!";

    channel.assertQueue(queue, {
      durable: true
    });
    channel.sendToQueue(queue, Buffer.from(msg), {
      persistent: true
    });
   console.log(" [x] Sent '%s'", msg);
  });
  setTimeout(function() {
    connection.close();
    process.exit(0)
  }, 500);
});


----------


推荐阅读