【问题标题】:Message persistence in RabbitMQRabbitMQ 中的消息持久化
【发布时间】:2015-12-19 14:14:09
【问题描述】:

我正在编写一个小应用程序,我在其中使用 RabbitMQ 发送/接收消息。一切正常,但我正在努力解决消息持久性问题。

即使在服务器重新启动时,我也希望消息保留在队列中。我了解交换和队列级别的持久性概念,并将它们设置为 true(而不是默认为 true)。因此,当我重新启动 RabbitMQ 服务器时,exchange 和 Queue 保持不变,但队列中的消息被删除。

我正在使用 EasyNetQ.IBus 接口发送消息。

谢谢

【问题讨论】:

  • 你能显示一些代码吗?比如你在哪里设置你的频道/队列等等。
  • 您是否使用 delivery_mode 消息属性使消息本身持久化?
  • @Evk:我想弄清楚,我在哪里设置delivery_mode。我已经读过它,但无法弄清楚在哪里/如何做
  • @Jay 我看到除非您在连接字符串中明确设置“persistentMessages=false”,否则默认情况下应该为 true,并且应该使用正确的 delivery_mode (=2)。因此,我们需要您提供更多信息。

标签: c# rabbitmq easynetq


【解决方案1】:

使用RabbitMQ.Client,可以使用IBasicProperties设置下发方式,可以通过IModel.CreateBasicProperties()方法获取。

using (IConnection conn = factory.CreateConnection())
using (IModel channel = conn.CreateModel())
{
    channel.ExchangeDeclare(exchange, ExchangeType.Direct, durable: true);
    channel.QueueDeclare(queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
    channel.QueueBind(queue, exchange, routingKey, null);

    var props = channel.CreateBasicProperties();
    props.Persistent = true; // or props.DeliveryMode = 2;

    channel.BasicPublish(exchange, routingKey, props, Encoding.Default.GetBytes(message));
}

【讨论】:

  • 注意IBasicProperties 有一个Persistent 属性。所以你可以做props.Persistent = true;
【解决方案2】:

要使您的消息在 RabbitMQ 中持久化,您需要在代码中添加 MessageProperties.PERSISTENT_TEXT_PLAIN

import com.rabbitmq.client.MessageProperties;

channel.basicPublish("", "task_queue",
        MessageProperties.PERSISTENT_TEXT_PLAIN,
        message.getBytes());

【讨论】:

  • 我确定它可以工作,但问题是关于 C#,而不是 Java。
【解决方案3】:

您是否尝试过启用延迟队列? “惰性队列 - 尽可能早地将其内容移动到磁盘的队列”

它可以在策略级别(我的偏好)或特定队列上启用。

完整的解释在这里https://www.rabbitmq.com/lazy-queues.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-26
    • 2011-06-30
    • 2020-02-23
    • 2017-09-14
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    相关资源
    最近更新 更多