rabbitmq持久化
队列的持久化
队列的持久化需要我们在声明的时候指定其持久化
- 使用
durable=true
来持久化队列
/**
* 队列的持久化
*/
public class QueuePersistence {
public static void main(String[] args) throws IOException {
Channel channel = RabbitUtil.getChannel();
//队列的持久化
boolean durable = true;
channel.queueDeclare(QueueNames.ACK_QUEUE,durable,false,false,null);
}
}
持久化队列之后,发现管理台中这个队列
重启rabbitmq server
之后发现队列依然存在
消息的持久化
消息的持久化需要我们在消息生产的时候指定其持久化
- 使用
MessageProperties.PERSISTENT_TEXT_PLAIN
/**
* 消息的持久化
*/
public class MsgPersistence {
public static void main(String[] args) throws IOException {
Channel channel = RabbitUtil.getChannel();
//队列的持久化
boolean durable = true;
channel.queueDeclare(QueueNames.ACK_QUEUE,durable,false,false,null);
String message = "this is a message";
/**
* MessageProperties.PERSISTENT_TEXT_PLAIN :消息持久化
*/
channel.basicPublish("",QueueNames.ACK_QUEUE, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes(StandardCharsets.UTF_8));
}
}