RabbitMQ学习笔记[4]-RabbitMQ消息模型-消息持久性

前一篇【消息自动确认机制】学会了如何确保即使消费者死亡,任务也不会丢失。但是,如果RabbitMQ服务器停止,我们的任务仍然会丢失。

When RabbitMQ quits or crashes it will forget the queues and messages unless you tell it not to. Two things are required to make sure that messages aren't lost: we need to mark both the queue and messages as durable.

RabbitMQ退出或崩溃时,它将忘记队列和消息,除非您告知不要这样做。要确保消息不会丢失,需要做两件事:我们需要将队列和消息都标记为持久性。

1. 队列持久化

// 通道绑定对应消息队列
// 参数1 name:队列名称,如果不存在则自动创建
// 参数2 durable:是否持久化
// 参数3 exclusive:是否独占队列(其他连接无法访问)
// 参数4 autoDelete:是否再消费完成后自动删除
// 参数5 额外参数
channel.queueDeclare("helloword", true, false, false, null);

2. 消息持久化

// 发布消息
// 参数1 exchange:交换机名称,点对点模式,没有交换机,所以 “”
// 参数2 routingKey: 队列名称
// 参数3 props:消息额外参数
// 参数4 消息内容
channel.basicPublish("", "helloword", MessageProperties.PERSISTENT_TEXT_PLAIN, "hello world rabbitmq!!!".getBytes());

Note on message persistence

Marking messages as persistent doesn't fully guarantee that a message won't be lost. Although it tells RabbitMQ to save the message to disk, there is still a short time window when RabbitMQ has accepted a message and hasn't saved it yet. Also,

RabbitMQ doesn't do fsync(2) for every message -- it may be just saved to cache and not really written to the disk. The persistence guarantees aren't strong, but it's more than enough for our simple task queue. If you need a stronger guarantee then you can

use publisher confirms.

关于消息持久性的注意事项

将消息标记为持久性并不能完全保证不会丢失消息。尽管它告诉RabbitMQ将消息保存到磁盘,但是RabbitMQ接受消息但尚未保存消息时,还有很短的时间。而且,RabbitMQ不会对每条消息都执行fsync(2)-它可能只是保存到缓存中,而没有真正写到磁盘上。

持久性保证并不强,但是对于我们的简单任务队列而言,这已经绰绰有余了。如果您需要更强有力的保证,则可以使用 发布者确认

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值