SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理

一丶简介

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

 

在@Queue和@Exchange注解中都有autoDelete属性,值是布尔类型的字符串。如:autoDelete=“false”。

@Queue:当所有消费客户端断开连接后,是否自动删除队列: true:删除,false:不删除。

@Exchange:当所有绑定队列都不在使用时,是否自动删除交换器: true:删除,false:不删除。

当所有消费客户端断开连接时,而我们对RabbitMQ消息进行了持久化,那么这时未被消费的消息存于RabbitMQ服务器的内存中,如果RabbitMQ服务器都关闭了,那么未被消费的数据也都会丢失了。

下面编写代码试试RabbitMQ的消息持久化处理。

二丶配置文件

这里使用的是前面博客里写的error日志消息队列来测试的。这里还是创建两个项目,一个作为生产者,一个作为消费者。

生产者配置:

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》
《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

server.port=8883

spring.application.name=hello-world
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.thymeleaf.cache=false

#设置交换器名称
mq.config.exchange=log.direct
#设置error队列的路由键
mq.config.queue.error.routing.key=log.error.routing.key

View Code

消费者配置

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》
《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

server.port=8884

spring.application.name=lesson1

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest

#设置交换器名称
mq.config.exchange=log.direct
#设置error队列名称
mq.config.queue.error=log.error
#设置error路由键
mq.config.queue.error.routing.key=log.error.routing.key

View Code

三丶编写生产者

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》
《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

package com.example.rabbitdurableprovider;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:发送消息
 */
@Component
public class DurableSender {

    @Autowired
    private AmqpTemplate amqpTemplate;

    //exChange 交换器
    @Value("${mq.config.exchange}")
    private String exChange;

    //routingkey 路由键
    @Value("${mq.config.queue.error.routing.key}")
    private String routingKey;
    /**
     * 发送消息的方法
     * @param msg
     */
    public void send(String msg){
        //向消息队列发送消息
        //参数1:交换器名称
        //参数2:路由键
        //参数3:消息
        this.amqpTemplate.convertAndSend(exChange,routingKey,msg);

    }
}

View Code

四丶编写消费者

这里消费者服务配置中@Queue中的autoDelete属性设置的是true,即未持久化,一会儿测试下看没有持久化的消息队列在所有的消费者服务器断开后是怎样的。

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》
《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

package com.ant.rabbitdurableconsumer;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * Author:aijiaxiang
 * Date:2020/4/26
 * Description:消息接收者
 * @RabbitListener bindings:绑定队列
 * @QueueBinding  value:绑定队列的名称
 *                  exchange:配置交换器
 * @Queue : value:配置队列名称
 *          autoDelete:是否是一个可删除的临时队列
 * @Exchange value:为交换器起个名称
 *           type:指定具体的交换器类型
 */
@Component
@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}",autoDelete = "true"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.error.routing.key}"
        )
)
public class DurableErrorReceiver {

    /**
     * 接收消息的方法,采用消息队列监听机制
     * @param msg
     */
    @RabbitHandler
    public void process(String msg){
        System.out.println("error-receiver:"+msg);
    }
}

View Code

 

五丶编写测试类

这里写了一个死循环持续向消息队列中发送消息,用变量falg来记录发送编号。

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》
《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

package com.example.amqp;

import com.example.helloworld.HelloworldApplication;
import com.example.rabbitdurableprovider.DurableSender;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**
 * Author:aijiaxiang% #000000;">@RabbitListener(
        bindings = @QueueBinding(
                value = @Queue(value = "${mq.config.queue.error}",autoDelete = "false"),
                exchange = @Exchange(value = "${mq.config.exchange}", type = ExchangeTypes.DIRECT),
                key = "${mq.config.queue.error.routing.key}"
        )
)

修改后重启消费者服务器,再次调用测试方法。然后关闭消费者服务器,模拟“故障”。这时看到消息接收到第15条服务器就“故障”了。

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

 

重启消费者服务器。可以看到服务器一启动,消费者就从消息队列中读取到了服务器“故障”时缓存在RabbitMQ中的消息,消息并未丢失,RabbitMQ消息持久化处理成功。

《SpringBoot+RabbitMQ学习笔记(五)RabbitMQ消息持久化处理》

 

OK!以上就是今天学习的RabbitMQ消息持久化处理,如有不对之处,欢迎指正!

 

点赞