SpringBoot integrates RabbitMQ
1. Description
We must ensure that switches, queues, and messages are persistent, otherwise they will be lost when restarting MQ or in other cases, unless you think the message to be sent is really unimportant, even if it is lost.
2. Switch Persistence
Click to view the source code of the created switch, the switch is persistent by default
The first way to create
I have mentioned several types of switches before, let me take fanout type switches as an example
//定义持久化交换机
@Bean
public FanoutExchange fanoutExchange() {
//第三个参数就是持久化的意思
return new FanoutExchange("fanout.exchange",true,false);
}
The second way to create
//定义direct类型交换机
@Bean
public DirectExchange directExchange() {
return ExchangeBuilder.directExchange("direct.exchange").build();
}
3. Queue Persistence
The first way to create
//定义持久化队列
@Bean
public Queue directQueue1() {
//第二个参数就是持久化的意思
return new Queue("direct.queue1",true,false,false);
}
The second way to create
//定义持久化队列
@Bean
public Queue topicQueue1(){
return QueueBuilder.durable("topic.queue1").build();
}
4. Message Persistence
//消息持久化
Message message = MessageBuilder
.withBody("你好".getBytes())
//设置为持久化消息
.setDeliveryMode(MessageDeliveryMode.PERSISTENT)
.build();
rabbitTemplate.convertAndSend("fanout.exchange", "", message, correlationData);